Show More
@@ -1,92 +1,92 | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | |
|
3 | 3 | """This does HTTP GET requests given a host:port and path and returns |
|
4 | 4 | a subset of the headers plus the body of the result.""" |
|
5 | 5 | |
|
6 | 6 | from __future__ import absolute_import, print_function |
|
7 | 7 | |
|
8 | 8 | import json |
|
9 | 9 | import os |
|
10 | 10 | import sys |
|
11 | 11 | |
|
12 | 12 | from mercurial import ( |
|
13 | 13 | util, |
|
14 | 14 | ) |
|
15 | 15 | |
|
16 | 16 | httplib = util.httplib |
|
17 | 17 | |
|
18 | 18 | try: |
|
19 | 19 | import msvcrt |
|
20 | 20 | msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) |
|
21 | 21 | msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY) |
|
22 | 22 | except ImportError: |
|
23 | 23 | pass |
|
24 | 24 | |
|
25 | 25 | twice = False |
|
26 | 26 | if '--twice' in sys.argv: |
|
27 | 27 | sys.argv.remove('--twice') |
|
28 | 28 | twice = True |
|
29 | 29 | headeronly = False |
|
30 | 30 | if '--headeronly' in sys.argv: |
|
31 | 31 | sys.argv.remove('--headeronly') |
|
32 | 32 | headeronly = True |
|
33 | 33 | formatjson = False |
|
34 | 34 | if '--json' in sys.argv: |
|
35 | 35 | sys.argv.remove('--json') |
|
36 | 36 | formatjson = True |
|
37 | 37 | |
|
38 | 38 | hgproto = None |
|
39 | 39 | if '--hgproto' in sys.argv: |
|
40 | 40 | idx = sys.argv.index('--hgproto') |
|
41 | 41 | hgproto = sys.argv[idx + 1] |
|
42 | 42 | sys.argv.pop(idx) |
|
43 | 43 | sys.argv.pop(idx) |
|
44 | 44 | |
|
45 | 45 | tag = None |
|
46 | 46 | def request(host, path, show): |
|
47 | 47 | assert not path.startswith('/'), path |
|
48 | 48 | global tag |
|
49 | 49 | headers = {} |
|
50 | 50 | if tag: |
|
51 | 51 | headers['If-None-Match'] = tag |
|
52 | 52 | if hgproto: |
|
53 | 53 | headers['X-HgProto-1'] = hgproto |
|
54 | 54 | |
|
55 | 55 | conn = httplib.HTTPConnection(host) |
|
56 | 56 | conn.request("GET", '/' + path, None, headers) |
|
57 | 57 | response = conn.getresponse() |
|
58 | 58 | print(response.status, response.reason) |
|
59 | 59 | if show[:1] == ['-']: |
|
60 | 60 | show = sorted(h for h, v in response.getheaders() |
|
61 | 61 | if h.lower() not in show) |
|
62 | 62 | for h in [h.lower() for h in show]: |
|
63 | 63 | if response.getheader(h, None) is not None: |
|
64 | 64 | print("%s: %s" % (h, response.getheader(h))) |
|
65 | 65 | if not headeronly: |
|
66 | 66 | print() |
|
67 | 67 | data = response.read() |
|
68 | 68 | |
|
69 | 69 | # Pretty print JSON. This also has the beneficial side-effect |
|
70 | 70 | # of verifying emitted JSON is well-formed. |
|
71 | 71 | if formatjson: |
|
72 | 72 | # json.dumps() will print trailing newlines. Eliminate them |
|
73 | 73 | # to make tests easier to write. |
|
74 | 74 | data = json.loads(data) |
|
75 | 75 | lines = json.dumps(data, sort_keys=True, indent=2).splitlines() |
|
76 | 76 | for line in lines: |
|
77 | 77 | print(line.rstrip()) |
|
78 | 78 | else: |
|
79 | 79 | sys.stdout.write(data) |
|
80 | 80 | |
|
81 |
|
|
|
82 |
|
|
|
81 | if twice and response.getheader('ETag', None): | |
|
82 | tag = response.getheader('ETag') | |
|
83 | 83 | |
|
84 | 84 | return response.status |
|
85 | 85 | |
|
86 | 86 | status = request(sys.argv[1], sys.argv[2], sys.argv[3:]) |
|
87 | 87 | if twice: |
|
88 | 88 | status = request(sys.argv[1], sys.argv[2], sys.argv[3:]) |
|
89 | 89 | |
|
90 | 90 | if 200 <= status <= 305: |
|
91 | 91 | sys.exit(0) |
|
92 | 92 | sys.exit(1) |
General Comments 0
You need to be logged in to leave comments.
Login now