Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2682638

Browse files
committed
Merged revisions 80587 via svnmerge from
svn+ssh://[email protected]/python/branches/py3k ................ r80587 | senthil.kumaran | 2010-04-28 23:09:48 +0530 (Wed, 28 Apr 2010) | 9 lines Merged revisions 80583 via svnmerge from svn+ssh://[email protected]/python/trunk ........ r80583 | senthil.kumaran | 2010-04-28 22:50:43 +0530 (Wed, 28 Apr 2010) | 3 lines Fixed Issue6312 - httplib fails with HEAD requests to pages with "transfer-encoding: chunked" ........ ................
1 parent 95ff524 commit 2682638

4 files changed

Lines changed: 39 additions & 0 deletions

File tree

Doc/library/http.client.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,22 @@ Here is an example session that uses the ``GET`` method::
504504
>>> data2 = r2.read()
505505
>>> conn.close()
506506

507+
Here is an example session that uses ``HEAD`` method. Note that ``HEAD`` method
508+
never returns any data. ::
509+
510+
511+
>>> import http.client
512+
>>> conn = http.client.HTTPConnection("www.python.org")
513+
>>> conn.request("HEAD","/index.html")
514+
>>> res = conn.getresponse()
515+
>>> print(res.status, res.reason)
516+
200 OK
517+
>>> data = res.read()
518+
>>> print(len(data))
519+
0
520+
>>> data == b''
521+
True
522+
507523
Here is an example session that shows how to ``POST`` requests::
508524

509525
>>> import http.client, urllib.parse

Lib/http/client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,9 @@ def read(self, amt=None):
487487
if self.fp is None:
488488
return b""
489489

490+
if self._method == "HEAD":
491+
return b""
492+
490493
if self.chunked:
491494
return self._read_chunked(amt)
492495

Lib/test/test_httplib.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,23 @@ def test_chunked(self):
219219
finally:
220220
resp.close()
221221

222+
def test_chunked_head(self):
223+
chunked_start = (
224+
'HTTP/1.1 200 OK\r\n'
225+
'Transfer-Encoding: chunked\r\n\r\n'
226+
'a\r\n'
227+
'hello world\r\n'
228+
'1\r\n'
229+
'd\r\n'
230+
)
231+
sock = FakeSocket(chunked_start + '0\r\n')
232+
resp = client.HTTPResponse(sock, method="HEAD")
233+
resp.begin()
234+
self.assertEquals(resp.read(), b'')
235+
self.assertEquals(resp.status, 200)
236+
self.assertEquals(resp.reason, 'OK')
237+
resp.close()
238+
222239
def test_negative_content_length(self):
223240
sock = FakeSocket(
224241
'HTTP/1.1 200 OK\r\nContent-Length: -1\r\n\r\nHello\r\n')

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ Core and Builtins
4040
Library
4141
-------
4242

43+
- Issue #6312: Fixed http HEAD request when the transfer encoding is chunked.
44+
It should correctly return an empty response now.
45+
4346
- Issue #8549: Fix compiling the _ssl extension under AIX. Patch by
4447
Sridhar Ratnakumar.
4548

0 commit comments

Comments
 (0)