From 557910320a6696ee6a94ea861100a961ea54eeaf Mon Sep 17 00:00:00 2001 From: m_heijo Date: Sat, 11 Nov 2017 07:40:09 +0900 Subject: [PATCH 1/5] fix HEAD request body length --- hyper/http11/response.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/hyper/http11/response.py b/hyper/http11/response.py index 8f3eb985..5875d1e2 100644 --- a/hyper/http11/response.py +++ b/hyper/http11/response.py @@ -53,10 +53,13 @@ def __init__(self, code, reason, headers, sock, connection=None, self._expect_close = True # The expected length of the body. - try: - self._length = int(self.headers[b'content-length'][0]) - except KeyError: - self._length = None + if request_method.upper() != b'HEAD': + try: + self._length = int(self.headers[b'content-length'][0]) + except KeyError: + self._length = None + else: + self._length = 0 # Whether we expect a chunked response. self._chunked = ( From cff5c3ff99837d00da676d161c9ebc99e53aebd8 Mon Sep 17 00:00:00 2001 From: m_heijo Date: Sat, 11 Nov 2017 09:50:16 +0900 Subject: [PATCH 2/5] Add test function for length of the HTTP/1.1 response-body. --- test_release.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test_release.py b/test_release.py index 07c8c9df..38138657 100644 --- a/test_release.py +++ b/test_release.py @@ -168,3 +168,27 @@ def test_hitting_nghttp2_org_via_h2c_upgrade(self): assert response.status == 200 assert response.read() assert response.version == HTTPVersion.http20 + + def test_http11_response_body_length(self): + """ + This test function uses check the expected length of the HTTP/1.1-response-body. + """ + c = HTTP11Connection('httpbin.org:443') + + # Make some HTTP/1.1 requests. + methods = ['GET', 'HEAD'] + for method in methods: + c.request(method, '/') + resp = c.get_response() + + # Check the expected length of the body. + if method == 'HEAD': + assert resp._length == 0 + assert resp.read() == b'' + else: + try: + content_length = int(resp.headers[b'Content-Length'][0]) + except KeyError: + continue + assert resp._length == content_length + assert resp.read() From a3dd27b7e377e509cdffe8220ec7c7bd94deaf6a Mon Sep 17 00:00:00 2001 From: Mitsuo Heijo Date: Sat, 11 Nov 2017 10:18:20 +0900 Subject: [PATCH 3/5] fix some error --- hyper/http11/response.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hyper/http11/response.py b/hyper/http11/response.py index 5875d1e2..31d2266d 100644 --- a/hyper/http11/response.py +++ b/hyper/http11/response.py @@ -53,7 +53,7 @@ def __init__(self, code, reason, headers, sock, connection=None, self._expect_close = True # The expected length of the body. - if request_method.upper() != b'HEAD': + if request_method != b'HEAD': try: self._length = int(self.headers[b'content-length'][0]) except KeyError: From 9fa200a11e33cec6802829239295111db086f2b0 Mon Sep 17 00:00:00 2001 From: Mitsuo Heijo Date: Sat, 11 Nov 2017 15:21:42 +0900 Subject: [PATCH 4/5] fix HTTP/1.1 response body length --- hyper/http11/response.py | 2 +- test/test_http11.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/hyper/http11/response.py b/hyper/http11/response.py index 31d2266d..3aed4352 100644 --- a/hyper/http11/response.py +++ b/hyper/http11/response.py @@ -63,7 +63,7 @@ def __init__(self, code, reason, headers, sock, connection=None, # Whether we expect a chunked response. self._chunked = ( - b'chunked' in self.headers.get(b'transfer-encoding', []) + b'chunked' in self.headers.get(b'transfer-encoding', []) ) # When content-length is absent and response is not chunked, diff --git a/test/test_http11.py b/test/test_http11.py index 954bacd0..4a9280c9 100644 --- a/test/test_http11.py +++ b/test/test_http11.py @@ -941,6 +941,18 @@ def test_response_version(self): r = HTTP11Response(200, 'OK', headers, d) assert r.version is HTTPVersion.http11 + def test_response_body_length(self): + methods = [b'HEAD', b'GET'] + headers = {b'content-length': [b'15']} + d = DummySocket() + for method in methods: + d.queue = [] + r = HTTP11Response(200, 'OK', headers, d, request_method=method) + if method == b'HEAD': + assert r._length == 0 + else: + assert r._length == int(r.headers[b'content-length'][0]) + class DummySocket(object): def __init__(self): From d293a34cd71780d1b3b3ba8bf0bfb2b80e8d00a1 Mon Sep 17 00:00:00 2001 From: Mitsuo Heijo Date: Sat, 11 Nov 2017 15:27:37 +0900 Subject: [PATCH 5/5] fix HTTP/1.1 response body length --- hyper/http11/response.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hyper/http11/response.py b/hyper/http11/response.py index 3aed4352..31d2266d 100644 --- a/hyper/http11/response.py +++ b/hyper/http11/response.py @@ -63,7 +63,7 @@ def __init__(self, code, reason, headers, sock, connection=None, # Whether we expect a chunked response. self._chunked = ( - b'chunked' in self.headers.get(b'transfer-encoding', []) + b'chunked' in self.headers.get(b'transfer-encoding', []) ) # When content-length is absent and response is not chunked,