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