experimental: don't interfere with headers
This commit is contained in:
parent
d1904c2f52
commit
551d9f11e5
|
@ -10,7 +10,8 @@ def assemble_request(request):
|
|||
if request.body == CONTENT_MISSING:
|
||||
raise HttpException("Cannot assemble flow with CONTENT_MISSING")
|
||||
head = assemble_request_head(request)
|
||||
return head + request.body
|
||||
body = b"".join(assemble_body(request.headers, [request.body]))
|
||||
return head + body
|
||||
|
||||
|
||||
def assemble_request_head(request):
|
||||
|
@ -23,7 +24,8 @@ def assemble_response(response):
|
|||
if response.body == CONTENT_MISSING:
|
||||
raise HttpException("Cannot assemble flow with CONTENT_MISSING")
|
||||
head = assemble_response_head(response)
|
||||
return head + response.body
|
||||
body = b"".join(assemble_body(response.headers, [response.body]))
|
||||
return head + body
|
||||
|
||||
|
||||
def assemble_response_head(response):
|
||||
|
@ -74,20 +76,12 @@ def _assemble_request_line(request, form=None):
|
|||
|
||||
def _assemble_request_headers(request):
|
||||
headers = request.headers.copy()
|
||||
for k in request._headers_to_strip_off:
|
||||
headers.pop(k, None)
|
||||
if b"host" not in headers and request.scheme and request.host and request.port:
|
||||
headers[b"Host"] = utils.hostport(
|
||||
request.scheme,
|
||||
request.host,
|
||||
request.port
|
||||
)
|
||||
|
||||
# If content is defined (i.e. not None or CONTENT_MISSING), we always
|
||||
# add a content-length header.
|
||||
if request.body or request.body == b"":
|
||||
headers[b"Content-Length"] = str(len(request.body)).encode("ascii")
|
||||
|
||||
return bytes(headers)
|
||||
|
||||
|
||||
|
@ -100,8 +94,4 @@ def _assemble_response_line(response):
|
|||
|
||||
|
||||
def _assemble_response_headers(response):
|
||||
headers = response.headers.copy()
|
||||
for k in response._headers_to_strip_off:
|
||||
headers.pop(k, None)
|
||||
|
||||
return bytes(headers)
|
||||
return bytes(response.headers)
|
||||
|
|
|
@ -92,7 +92,10 @@ class Headers(MutableMapping, object):
|
|||
self.update(headers)
|
||||
|
||||
def __bytes__(self):
|
||||
return b"\r\n".join(b": ".join(field) for field in self.fields) + b"\r\n"
|
||||
if self.fields:
|
||||
return b"\r\n".join(b": ".join(field) for field in self.fields) + b"\r\n"
|
||||
else:
|
||||
return b""
|
||||
|
||||
if six.PY2:
|
||||
__str__ = __bytes__
|
||||
|
@ -224,16 +227,6 @@ class Message(object):
|
|||
|
||||
|
||||
class Request(Message):
|
||||
# This list is adopted legacy code.
|
||||
# We probably don't need to strip off keep-alive.
|
||||
_headers_to_strip_off = [
|
||||
'Proxy-Connection',
|
||||
'Keep-Alive',
|
||||
'Connection',
|
||||
'Transfer-Encoding',
|
||||
'Upgrade',
|
||||
]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
form_in,
|
||||
|
@ -467,12 +460,6 @@ class Request(Message):
|
|||
|
||||
|
||||
class Response(Message):
|
||||
_headers_to_strip_off = [
|
||||
'Proxy-Connection',
|
||||
'Alternate-Protocol',
|
||||
'Alt-Svc',
|
||||
]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
http_version,
|
||||
|
|
|
@ -79,15 +79,10 @@ def test_assemble_request_headers():
|
|||
r = treq(body=b"")
|
||||
r.headers[b"Transfer-Encoding"] = b"chunked"
|
||||
c = _assemble_request_headers(r)
|
||||
assert b"Content-Length" in c
|
||||
assert b"Transfer-Encoding" not in c
|
||||
assert b"Transfer-Encoding" in c
|
||||
|
||||
assert b"Host" in _assemble_request_headers(treq(headers=Headers()))
|
||||
|
||||
assert b"Proxy-Connection" not in _assemble_request_headers(
|
||||
treq(headers=Headers(Proxy_Connection="42"))
|
||||
)
|
||||
|
||||
|
||||
def test_assemble_response_headers():
|
||||
# https://github.com/mitmproxy/mitmproxy/issues/186
|
||||
|
@ -95,7 +90,3 @@ def test_assemble_response_headers():
|
|||
r.headers["Transfer-Encoding"] = b"chunked"
|
||||
c = _assemble_response_headers(r)
|
||||
assert b"Transfer-Encoding" in c
|
||||
|
||||
assert b"Proxy-Connection" not in _assemble_response_headers(
|
||||
tresp(headers=Headers(Proxy_Connection=b"42"))
|
||||
)
|
||||
|
|
|
@ -442,13 +442,16 @@ class TestHeaders(object):
|
|||
|
||||
def test_str(self):
|
||||
headers = Headers(Host="example.com")
|
||||
assert bytes(headers) == "Host: example.com\r\n"
|
||||
assert bytes(headers) == b"Host: example.com\r\n"
|
||||
|
||||
headers = Headers([
|
||||
["Host", "example.com"],
|
||||
["Accept", "text/plain"]
|
||||
[b"Host", b"example.com"],
|
||||
[b"Accept", b"text/plain"]
|
||||
])
|
||||
assert str(headers) == "Host: example.com\r\nAccept: text/plain\r\n"
|
||||
assert bytes(headers) == b"Host: example.com\r\nAccept: text/plain\r\n"
|
||||
|
||||
headers = Headers()
|
||||
assert bytes(headers) == b""
|
||||
|
||||
def test_setitem(self):
|
||||
headers = Headers()
|
||||
|
|
Loading…
Reference in New Issue