]> granicus.if.org Git - python/commitdiff
SF bug 622042: Don't expect response body from HEAD request.
authorJeremy Hylton <jeremy@alum.mit.edu>
Mon, 5 May 2003 16:13:58 +0000 (16:13 +0000)
committerJeremy Hylton <jeremy@alum.mit.edu>
Mon, 5 May 2003 16:13:58 +0000 (16:13 +0000)
Bug fix candidate.

Lib/httplib.py
Lib/test/test_httplib.py
Misc/ACKS

index caf6ccd3e77590e51979013a340db99eb880a7fd..9527e29288ed0b9dee4a793af52ac20b716c1ced 100644 (file)
@@ -208,10 +208,11 @@ class HTTPResponse:
 
     # See RFC 2616 sec 19.6 and RFC 1945 sec 6 for details.
 
-    def __init__(self, sock, debuglevel=0, strict=0):
+    def __init__(self, sock, debuglevel=0, strict=0, method=None):
         self.fp = sock.makefile('rb', 0)
         self.debuglevel = debuglevel
         self.strict = strict
+        self._method = method
 
         self.msg = None
 
@@ -326,7 +327,8 @@ class HTTPResponse:
         # does the body have a fixed length? (of zero)
         if (status == 204 or            # No Content
             status == 304 or            # Not Modified
-            100 <= status < 200):       # 1xx codes
+            100 <= status < 200 or      # 1xx codes
+            self._method == 'HEAD'):
             self.length = 0
 
         # if the connection remains open, and we aren't using chunked, and
@@ -497,6 +499,7 @@ class HTTPConnection:
         self._buffer = []
         self.__response = None
         self.__state = _CS_IDLE
+        self._method = None
 
         self._set_hostport(host, port)
         if strict is not None:
@@ -626,6 +629,8 @@ class HTTPConnection:
         else:
             raise CannotSendRequest()
 
+        # Save the method we use, we need it later in the response phase
+        self._method = method
         if not url:
             url = '/'
         str = '%s %s %s' % (method, url, self._http_vsn_str)
@@ -763,9 +768,11 @@ class HTTPConnection:
 
         if self.debuglevel > 0:
             response = self.response_class(self.sock, self.debuglevel,
-                                           strict=self.strict)
+                                           strict=self.strict, 
+                                           method=self._method)
         else:
-            response = self.response_class(self.sock, strict=self.strict)
+            response = self.response_class(self.sock, strict=self.strict,
+                                           method=self._method)
 
         response.begin()
         assert response.will_close != _UNKNOWN
index 8764455ccfd07f1583fded1c5c6ccd4080d3798c..29f4503d47cb9cd90760b5c8de3052613f30f1b4 100644 (file)
@@ -78,4 +78,17 @@ def _test():
     if cookies != hdr:
         raise AssertionError, "multiple headers not combined properly"
 
+    # test that the library doesn't attempt to read any data
+    # from a head request
+    conn = httplib.HTTPConnection("www.python.org")
+    conn.connect()
+    conn.request("HEAD", "/", headers={"Connection" : "keep-alive"})
+    resp = conn.getresponse()
+    if resp.status != 200:
+        raise AssertionError, "Expected status 200, got %d" % resp.status
+    if resp.read() != "":
+        raise AssertionError, "Did not expect response from HEAD request"
+    resp.close()
+    conn.close()
+
 test()
index 2fbe9af5c0b4a9e964c6126b3e10ada02ac78439..a8dbb76ac76dbe3a643a315c3a22e5193de0449f 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -261,6 +261,7 @@ Michael Hudson
 Jim Hugunin
 Greg Humphreys
 Jeremy Hylton
+Mihai Ibanescu
 Juan David Ibáñez Palomar
 Tony Ingraldi
 John Interrante