get-with-headers.py
124 lines
| 3.2 KiB
| text/x-python
|
PythonLexer
/ tests / get-with-headers.py
Eric Hopper
|
r2532 | #!/usr/bin/env python | ||
Martin Geisler
|
r8447 | """This does HTTP GET requests given a host:port and path and returns | ||
Eric Hopper
|
r2532 | a subset of the headers plus the body of the result.""" | ||
Yuya Nishihara
|
r36594 | from __future__ import absolute_import | ||
Gregory Szorc
|
r27296 | |||
Gregory Szorc
|
r35798 | import argparse | ||
Gregory Szorc
|
r27296 | import json | ||
import os | ||||
import sys | ||||
Patrick Mezard
|
r7054 | |||
Pulkit Goyal
|
r29455 | from mercurial import ( | ||
Gregory Szorc
|
r40190 | pycompat, | ||
Pulkit Goyal
|
r29455 | util, | ||
) | ||||
httplib = util.httplib | ||||
Patrick Mezard
|
r7054 | try: | ||
Gregory Szorc
|
r27296 | import msvcrt | ||
Augie Fackler
|
r43346 | |||
Patrick Mezard
|
r7054 | msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) | ||
msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY) | ||||
except ImportError: | ||||
pass | ||||
Yuya Nishihara
|
r36594 | stdout = getattr(sys.stdout, 'buffer', sys.stdout) | ||
Gregory Szorc
|
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') | ||||
Augie Fackler
|
r43346 | 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') | ||||
Gregory Szorc
|
r35798 | parser.add_argument('host') | ||
parser.add_argument('path') | ||||
parser.add_argument('show', nargs='*') | ||||
Dirkjan Ochtman
|
r12182 | |||
Gregory Szorc
|
r35798 | args = parser.parse_args() | ||
twice = args.twice | ||||
headeronly = args.headeronly | ||||
formatjson = args.json | ||||
hgproto = args.hgproto | ||||
Gregory Szorc
|
r35799 | requestheaders = args.requestheader | ||
Gregory Szorc
|
r30764 | |||
Dirkjan Ochtman
|
r12182 | tag = None | ||
Augie Fackler
|
r43346 | |||
Dirkjan Ochtman
|
r12182 | def request(host, path, show): | ||
Mads Kiilerich
|
r17017 | assert not path.startswith('/'), path | ||
Dirkjan Ochtman
|
r12182 | global tag | ||
headers = {} | ||||
if tag: | ||||
headers['If-None-Match'] = tag | ||||
Gregory Szorc
|
r30764 | if hgproto: | ||
headers['X-HgProto-1'] = hgproto | ||||
Bryan O'Sullivan
|
r5561 | |||
Gregory Szorc
|
r35799 | for header in requestheaders: | ||
key, value = header.split('=', 1) | ||||
headers[key] = value | ||||
Dirkjan Ochtman
|
r12182 | conn = httplib.HTTPConnection(host) | ||
Mads Kiilerich
|
r17017 | conn.request("GET", '/' + path, None, headers) | ||
Dirkjan Ochtman
|
r12182 | response = conn.getresponse() | ||
Augie Fackler
|
r43346 | stdout.write( | ||
b'%d %s\n' % (response.status, response.reason.encode('ascii')) | ||||
) | ||||
Mads Kiilerich
|
r18380 | if show[:1] == ['-']: | ||
Augie Fackler
|
r43346 | show = sorted( | ||
h for h, v in response.getheaders() if h.lower() not in show | ||||
) | ||||
Dirkjan Ochtman
|
r12182 | for h in [h.lower() for h in show]: | ||
if response.getheader(h, None) is not None: | ||||
Augie Fackler
|
r43346 | stdout.write( | ||
b"%s: %s\n" | ||||
% (h.encode('ascii'), response.getheader(h).encode('ascii')) | ||||
) | ||||
Pierre-Yves David
|
r18400 | if not headeronly: | ||
Yuya Nishihara
|
r36594 | stdout.write(b'\n') | ||
Gregory Szorc
|
r23409 | data = response.read() | ||
Gregory Szorc
|
r24543 | |||
Gregory Szorc
|
r35799 | if args.bodyfile: | ||
bodyfh = open(args.bodyfile, 'wb') | ||||
else: | ||||
Yuya Nishihara
|
r36594 | bodyfh = stdout | ||
Gregory Szorc
|
r35799 | |||
Gregory Szorc
|
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. | ||||
Gregory Szorc
|
r43697 | data = pycompat.json_loads(data) | ||
Gregory Szorc
|
r24543 | lines = json.dumps(data, sort_keys=True, indent=2).splitlines() | ||
for line in lines: | ||||
Gregory Szorc
|
r40190 | bodyfh.write(pycompat.sysbytes(line.rstrip())) | ||
Gregory Szorc
|
r35799 | bodyfh.write(b'\n') | ||
Gregory Szorc
|
r24543 | else: | ||
Gregory Szorc
|
r35799 | bodyfh.write(data) | ||
if args.bodyfile: | ||||
bodyfh.close() | ||||
Dirkjan Ochtman
|
r12182 | |||
Gregory Szorc
|
r31791 | if twice and response.getheader('ETag', None): | ||
tag = response.getheader('ETag') | ||||
Dirkjan Ochtman
|
r12182 | |||
return response.status | ||||
Augie Fackler
|
r43346 | |||
Gregory Szorc
|
r35798 | status = request(args.host, args.path, args.show) | ||
Dirkjan Ochtman
|
r12182 | if twice: | ||
Gregory Szorc
|
r35798 | status = request(args.host, args.path, args.show) | ||
Dirkjan Ochtman
|
r12182 | |||
if 200 <= status <= 305: | ||||
Bryan O'Sullivan
|
r5561 | sys.exit(0) | ||
sys.exit(1) | ||||