get-with-headers.py
108 lines
| 3.1 KiB
| text/x-python
|
PythonLexer
/ tests / get-with-headers.py
|
r2532 | #!/usr/bin/env python | ||
|
r8447 | """This does HTTP GET requests given a host:port and path and returns | ||
|
r2532 | a subset of the headers plus the body of the result.""" | ||
|
r28726 | from __future__ import absolute_import, print_function | ||
|
r27296 | |||
|
r35798 | import argparse | ||
|
r27296 | import json | ||
import os | ||||
import sys | ||||
|
r7054 | |||
|
r29455 | from mercurial import ( | ||
util, | ||||
) | ||||
httplib = util.httplib | ||||
|
r7054 | try: | ||
|
r27296 | import msvcrt | ||
|
r7054 | msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) | ||
msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY) | ||||
except ImportError: | ||||
pass | ||||
|
r35798 | parser = argparse.ArgumentParser() | ||
parser.add_argument('--twice', action='store_true') | ||||
parser.add_argument('--headeronly', action='store_true') | ||||
parser.add_argument('--json', action='store_true') | ||||
parser.add_argument('--hgproto') | ||||
|
r35799 | parser.add_argument('--requestheader', nargs='*', default=[], | ||
help='Send an additional HTTP request header. Argument ' | ||||
'value is <header>=<value>') | ||||
parser.add_argument('--bodyfile', | ||||
help='Write HTTP response body to a file') | ||||
|
r35798 | parser.add_argument('host') | ||
parser.add_argument('path') | ||||
parser.add_argument('show', nargs='*') | ||||
|
r12182 | |||
|
r35798 | args = parser.parse_args() | ||
twice = args.twice | ||||
headeronly = args.headeronly | ||||
formatjson = args.json | ||||
hgproto = args.hgproto | ||||
|
r35799 | requestheaders = args.requestheader | ||
|
r30764 | |||
|
r12182 | tag = None | ||
def request(host, path, show): | ||||
|
r17017 | assert not path.startswith('/'), path | ||
|
r12182 | global tag | ||
headers = {} | ||||
if tag: | ||||
headers['If-None-Match'] = tag | ||||
|
r30764 | if hgproto: | ||
headers['X-HgProto-1'] = hgproto | ||||
|
r5561 | |||
|
r35799 | for header in requestheaders: | ||
key, value = header.split('=', 1) | ||||
headers[key] = value | ||||
|
r12182 | conn = httplib.HTTPConnection(host) | ||
|
r17017 | conn.request("GET", '/' + path, None, headers) | ||
|
r12182 | response = conn.getresponse() | ||
|
r28726 | print(response.status, response.reason) | ||
|
r18380 | if show[:1] == ['-']: | ||
|
r18393 | show = sorted(h for h, v in response.getheaders() | ||
if h.lower() not in show) | ||||
|
r12182 | for h in [h.lower() for h in show]: | ||
if response.getheader(h, None) is not None: | ||||
|
r28726 | print("%s: %s" % (h, response.getheader(h))) | ||
|
r18400 | if not headeronly: | ||
|
r28726 | print() | ||
|
r23409 | data = response.read() | ||
|
r24543 | |||
|
r35799 | if args.bodyfile: | ||
bodyfh = open(args.bodyfile, 'wb') | ||||
else: | ||||
|
r36290 | bodyfh = getattr(sys.stdout, 'buffer', sys.stdout) | ||
|
r35799 | |||
|
r24543 | # Pretty print JSON. This also has the beneficial side-effect | ||
# of verifying emitted JSON is well-formed. | ||||
if formatjson: | ||||
# json.dumps() will print trailing newlines. Eliminate them | ||||
# to make tests easier to write. | ||||
data = json.loads(data) | ||||
lines = json.dumps(data, sort_keys=True, indent=2).splitlines() | ||||
for line in lines: | ||||
|
r35799 | bodyfh.write(line.rstrip()) | ||
bodyfh.write(b'\n') | ||||
|
r24543 | else: | ||
|
r35799 | bodyfh.write(data) | ||
if args.bodyfile: | ||||
bodyfh.close() | ||||
|
r12182 | |||
|
r31791 | if twice and response.getheader('ETag', None): | ||
tag = response.getheader('ETag') | ||||
|
r12182 | |||
return response.status | ||||
|
r35798 | status = request(args.host, args.path, args.show) | ||
|
r12182 | if twice: | ||
|
r35798 | status = request(args.host, args.path, args.show) | ||
|
r12182 | |||
if 200 <= status <= 305: | ||||
|
r5561 | sys.exit(0) | ||
sys.exit(1) | ||||