mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-24 00:31:33 +00:00
9c873d63f4
This commit improves Python 3 compatibility and fixes two multidict issues: 1. Headers.items(multi=True) now decodes fields 2. MultiDict.clear(item) has been removed, as Python's MutableMapping already defines .clear() with different semantics. This is confusing for everyone who expects a dict-like object. `.pop("attr", None)` is not fantastic, but it's the Python way to do it.
38 lines
1.0 KiB
Python
38 lines
1.0 KiB
Python
from netlib.http import url
|
|
|
|
|
|
def parse_headers(headers):
|
|
authority = headers.get(':authority', '').encode()
|
|
method = headers.get(':method', 'GET').encode()
|
|
scheme = headers.get(':scheme', 'https').encode()
|
|
path = headers.get(':path', '/').encode()
|
|
|
|
headers.pop(":method", None)
|
|
headers.pop(":scheme", None)
|
|
headers.pop(":path", None)
|
|
|
|
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
|