mitmproxy/netlib/http/http2/utils.py

38 lines
1.0 KiB
Python
Raw Normal View History

2016-07-03 12:33:34 +00:00
from netlib.http import url
def parse_headers(headers):
2016-07-03 17:04:16 +00:00
authority = headers.get(':authority', '').encode()
method = headers.get(':method', 'GET').encode()
scheme = headers.get(':scheme', 'https').encode()
path = headers.get(':path', '/').encode()
2016-07-03 12:33:34 +00:00
headers.clear(":method")
headers.clear(":scheme")
headers.clear(":path")
host = None
port = None
if path == b'*' or path.startswith(b"/"):
first_line_format = "relative"
elif method == b'CONNECT': # pragma: no cover
raise NotImplementedError("CONNECT over HTTP/2 is not implemented.")
else: # pragma: no cover
first_line_format = "absolute"
# FIXME: verify if path or :host contains what we need
scheme, host, port, _ = url.parse(path)
if authority:
host, _, port = authority.partition(b':')
if not host:
host = b'localhost'
if not port:
port = 443 if scheme == b'https' else 80
port = int(port)
return first_line_format, method, scheme, host, port, path