| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
"""This does HTTP requests (GET by default) given a host:port and path and |
| 4 |
returns a subset of the headers plus the body of the result.""" |
| 5 |
|
| 6 |
|
| 7 |
import argparse |
| 8 |
import json |
| 9 |
import os |
| 10 |
import sys |
| 11 |
|
| 12 |
from mercurial import ( |
| 13 |
pycompat, |
| 14 |
util, |
| 15 |
) |
| 16 |
|
| 17 |
httplib = util.httplib |
| 18 |
|
| 19 |
try: |
| 20 |
import msvcrt |
| 21 |
|
| 22 |
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) |
| 23 |
msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY) |
| 24 |
except ImportError: |
| 25 |
pass |
| 26 |
|
| 27 |
stdout = getattr(sys.stdout, 'buffer', sys.stdout) |
| 28 |
|
| 29 |
parser = argparse.ArgumentParser() |
| 30 |
parser.add_argument('--twice', action='store_true') |
| 31 |
parser.add_argument('--headeronly', action='store_true') |
| 32 |
parser.add_argument('--json', action='store_true') |
| 33 |
parser.add_argument('--hgproto') |
| 34 |
parser.add_argument( |
| 35 |
'--requestheader', |
| 36 |
nargs='*', |
| 37 |
default=[], |
| 38 |
help='Send an additional HTTP request header. Argument ' |
| 39 |
'value is <header>=<value>', |
| 40 |
) |
| 41 |
parser.add_argument('--bodyfile', help='Write HTTP response body to a file') |
| 42 |
parser.add_argument('--method', default='GET', help='HTTP method to use') |
| 43 |
parser.add_argument('host') |
| 44 |
parser.add_argument('path') |
| 45 |
parser.add_argument('show', nargs='*') |
| 46 |
|
| 47 |
args = parser.parse_args() |
| 48 |
|
| 49 |
twice = args.twice |
| 50 |
headeronly = args.headeronly |
| 51 |
formatjson = args.json |
| 52 |
hgproto = args.hgproto |
| 53 |
requestheaders = args.requestheader |
| 54 |
|
| 55 |
tag = None |
| 56 |
|
| 57 |
|
| 58 |
def request(method, host, path, show): |
| 59 |
assert not path.startswith('/'), path |
| 60 |
global tag |
| 61 |
headers = {} |
| 62 |
if tag: |
| 63 |
headers['If-None-Match'] = tag |
| 64 |
if hgproto: |
| 65 |
headers['X-HgProto-1'] = hgproto |
| 66 |
|
| 67 |
for header in requestheaders: |
| 68 |
key, value = header.split('=', 1) |
| 69 |
headers[key] = value |
| 70 |
|
| 71 |
conn = httplib.HTTPConnection(host) |
| 72 |
conn.request(method, '/' + path, None, headers) |
| 73 |
response = conn.getresponse() |
| 74 |
stdout.write( |
| 75 |
b'%d %s\n' % (response.status, response.reason.encode('ascii')) |
| 76 |
) |
| 77 |
if show[:1] == ['-']: |
| 78 |
show = sorted( |
| 79 |
h for h, v in response.getheaders() if h.lower() not in show |
| 80 |
) |
| 81 |
for h in [h.lower() for h in show]: |
| 82 |
if response.getheader(h, None) is not None: |
| 83 |
stdout.write( |
| 84 |
b"%s: %s\n" |
| 85 |
% (h.encode('ascii'), response.getheader(h).encode('ascii')) |
| 86 |
) |
| 87 |
if headeronly: |
| 88 |
# still read the body to prevent windows to be unhappy about that |
| 89 |
# (this might some flakyness in test-hgweb-filelog.t on Windows) |
| 90 |
data = response.read() |
| 91 |
else: |
| 92 |
stdout.write(b'\n') |
| 93 |
data = response.read() |
| 94 |
|
| 95 |
if args.bodyfile: |
| 96 |
bodyfh = open(args.bodyfile, 'wb') |
| 97 |
else: |
| 98 |
bodyfh = stdout |
| 99 |
|
| 100 |
# Pretty print JSON. This also has the beneficial side-effect |
| 101 |
# of verifying emitted JSON is well-formed. |
| 102 |
if formatjson: |
| 103 |
# json.dumps() will print trailing newlines. Eliminate them |
| 104 |
# to make tests easier to write. |
| 105 |
data = pycompat.json_loads(data) |
| 106 |
lines = json.dumps(data, sort_keys=True, indent=2).splitlines() |
| 107 |
for line in lines: |
| 108 |
bodyfh.write(pycompat.sysbytes(line.rstrip())) |
| 109 |
bodyfh.write(b'\n') |
| 110 |
else: |
| 111 |
bodyfh.write(data) |
| 112 |
|
| 113 |
if args.bodyfile: |
| 114 |
bodyfh.close() |
| 115 |
|
| 116 |
if twice and response.getheader('ETag', None): |
| 117 |
tag = response.getheader('ETag') |
| 118 |
|
| 119 |
# further try to please the windows-flakyness deity |
| 120 |
conn.close() |
| 121 |
|
| 122 |
return response.status |
| 123 |
|
| 124 |
|
| 125 |
status = request(args.method, args.host, args.path, args.show) |
| 126 |
if twice: |
| 127 |
status = request(args.method, args.host, args.path, args.show) |
| 128 |
|
| 129 |
if 200 <= status <= 305: |
| 130 |
sys.exit(0) |
| 131 |
sys.exit(1) |