Fixed Issue6312 - httplib fails with HEAD requests to pages with "transfer-encoding: chunked"
diff --git a/Doc/library/httplib.rst b/Doc/library/httplib.rst
index 3725933..8390087 100644
--- a/Doc/library/httplib.rst
+++ b/Doc/library/httplib.rst
@@ -560,6 +560,22 @@
>>> data2 = r2.read()
>>> conn.close()
+Here is an example session that uses ``HEAD`` method. Note that ``HEAD`` method
+never returns any data. ::
+
+
+ >>> import httplib
+ >>> conn = httplib.HTTPConnection("www.python.org")
+ >>> conn.request("HEAD","/index.html")
+ >>> res = conn.getresponse()
+ >>> print res.status, res.reason
+ 200 OK
+ >>> data = res.read()
+ >>> print len(data)
+ 0
+ >>> data == ''
+ True
+
Here is an example session that shows how to ``POST`` requests::
>>> import httplib, urllib
diff --git a/Lib/httplib.py b/Lib/httplib.py
index 43c8797..5eb3f0d 100644
--- a/Lib/httplib.py
+++ b/Lib/httplib.py
@@ -524,6 +524,9 @@
if self.fp is None:
return ''
+ if self._method == 'HEAD':
+ return ''
+
if self.chunked:
return self._read_chunked(amt)
diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
index 107b904..a618c43 100644
--- a/Lib/test/test_httplib.py
+++ b/Lib/test/test_httplib.py
@@ -212,6 +212,23 @@
finally:
resp.close()
+ def test_chunked_head(self):
+ chunked_start = (
+ 'HTTP/1.1 200 OK\r\n'
+ 'Transfer-Encoding: chunked\r\n\r\n'
+ 'a\r\n'
+ 'hello world\r\n'
+ '1\r\n'
+ 'd\r\n'
+ )
+ sock = FakeSocket(chunked_start + '0\r\n')
+ resp = httplib.HTTPResponse(sock, method="HEAD")
+ resp.begin()
+ self.assertEquals(resp.read(), '')
+ self.assertEquals(resp.status, 200)
+ self.assertEquals(resp.reason, 'OK')
+ resp.close()
+
def test_negative_content_length(self):
sock = FakeSocket('HTTP/1.1 200 OK\r\n'
'Content-Length: -1\r\n\r\nHello\r\n')
diff --git a/Misc/NEWS b/Misc/NEWS
index e387017..1f584f4 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -31,6 +31,9 @@
Library
-------
+- Issue #6312: Fixed http HEAD request when the transfer encoding is chunked.
+ It should correctly return an empty response now.
+
- Issue #7490: to facilitate sharing of doctests between 2.x and 3.x test
suites, the IGNORE_EXCEPTION_DETAIL directive now also ignores the module
location of the raised exception. Based on initial patch by Lennart