From 551d9f11e571eac495674f1c23cfd0dfa8af2cb7 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Fri, 18 Sep 2015 18:05:50 +0200 Subject: [PATCH] experimental: don't interfere with headers --- netlib/http/http1/assemble.py | 20 +++++--------------- netlib/http/models.py | 21 ++++----------------- test/http/http1/test_assemble.py | 11 +---------- test/http/test_models.py | 11 +++++++---- 4 files changed, 17 insertions(+), 46 deletions(-) diff --git a/netlib/http/http1/assemble.py b/netlib/http/http1/assemble.py index b65a6be03..c2b60a0ff 100644 --- a/netlib/http/http1/assemble.py +++ b/netlib/http/http1/assemble.py @@ -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) diff --git a/netlib/http/models.py b/netlib/http/models.py index bc681de39..ff854b135 100644 --- a/netlib/http/models.py +++ b/netlib/http/models.py @@ -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, diff --git a/test/http/http1/test_assemble.py b/test/http/http1/test_assemble.py index cdc8bda90..2d250909f 100644 --- a/test/http/http1/test_assemble.py +++ b/test/http/http1/test_assemble.py @@ -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")) - ) diff --git a/test/http/test_models.py b/test/http/test_models.py index c3ab4d0fc..6970a6e42 100644 --- a/test/http/test_models.py +++ b/test/http/test_models.py @@ -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()