Skip to content
This repository was archived by the owner on Jan 13, 2021. It is now read-only.

Commit 0ac3471

Browse files
authored
Merge pull request #366 from johejo/fix_http11_head_response
Fix http11 head response
2 parents 55f7ec9 + d293a34 commit 0ac3471

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

hyper/http11/response.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,13 @@ def __init__(self, code, reason, headers, sock, connection=None,
5353
self._expect_close = True
5454

5555
# The expected length of the body.
56-
try:
57-
self._length = int(self.headers[b'content-length'][0])
58-
except KeyError:
59-
self._length = None
56+
if request_method != b'HEAD':
57+
try:
58+
self._length = int(self.headers[b'content-length'][0])
59+
except KeyError:
60+
self._length = None
61+
else:
62+
self._length = 0
6063

6164
# Whether we expect a chunked response.
6265
self._chunked = (

test/test_http11.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,18 @@ def test_response_version(self):
941941
r = HTTP11Response(200, 'OK', headers, d)
942942
assert r.version is HTTPVersion.http11
943943

944+
def test_response_body_length(self):
945+
methods = [b'HEAD', b'GET']
946+
headers = {b'content-length': [b'15']}
947+
d = DummySocket()
948+
for method in methods:
949+
d.queue = []
950+
r = HTTP11Response(200, 'OK', headers, d, request_method=method)
951+
if method == b'HEAD':
952+
assert r._length == 0
953+
else:
954+
assert r._length == int(r.headers[b'content-length'][0])
955+
944956

945957
class DummySocket(object):
946958
def __init__(self):

test_release.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,27 @@ def test_hitting_nghttp2_org_via_h2c_upgrade(self):
168168
assert response.status == 200
169169
assert response.read()
170170
assert response.version == HTTPVersion.http20
171+
172+
def test_http11_response_body_length(self):
173+
"""
174+
This test function uses check the expected length of the HTTP/1.1-response-body.
175+
"""
176+
c = HTTP11Connection('httpbin.org:443')
177+
178+
# Make some HTTP/1.1 requests.
179+
methods = ['GET', 'HEAD']
180+
for method in methods:
181+
c.request(method, '/')
182+
resp = c.get_response()
183+
184+
# Check the expected length of the body.
185+
if method == 'HEAD':
186+
assert resp._length == 0
187+
assert resp.read() == b''
188+
else:
189+
try:
190+
content_length = int(resp.headers[b'Content-Length'][0])
191+
except KeyError:
192+
continue
193+
assert resp._length == content_length
194+
assert resp.read()

0 commit comments

Comments
 (0)