[Python-checkins] r74463 - python/trunk/Lib/httplib.py

gregory.p.smith python-checkins at python.org
Sun Aug 16 00:39:03 CEST 2009


Author: gregory.p.smith
Date: Sun Aug 16 00:39:03 2009
New Revision: 74463

Log:
Force the http connection to close after any request returned when
buffering=True as our buffered data is not known to the HTTPConnection and may
contain data needed by a future request if the connection were left open.

See http://bugs.python.org/issue2576 and http://bugs.python.org/issue4879.


Modified:
   python/trunk/Lib/httplib.py

Modified: python/trunk/Lib/httplib.py
==============================================================================
--- python/trunk/Lib/httplib.py	(original)
+++ python/trunk/Lib/httplib.py	Sun Aug 16 00:39:03 2009
@@ -328,8 +328,12 @@
     def __init__(self, sock, debuglevel=0, strict=0, method=None, buffering=False):
         if buffering:
             # The caller won't be using any sock.recv() calls, so buffering
-            # is fine and recommendef for performance
+            # is fine and recommended for performance.
             self.fp = sock.makefile('rb')
+            # As our sock.makefile() object may receive data into its buffer
+            # beyond that needed to satisfy this response, we must close
+            # afterwards.
+            self._must_close = True
         else:
             # The buffer size is specified as zero, because the headers of
             # the response are read with readline().  If the reads were
@@ -337,6 +341,7 @@
             # response, which make be read via a recv() on the underlying
             # socket.
             self.fp = sock.makefile('rb', 0)
+            self._must_close = False
         self.debuglevel = debuglevel
         self.strict = strict
         self._method = method
@@ -474,6 +479,9 @@
             self.will_close = 1
 
     def _check_close(self):
+        if self._must_close:
+            return True
+
         conn = self.msg.getheader('connection')
         if self.version == 11:
             # An HTTP/1.1 proxy is assumed to stay open unless
@@ -622,6 +630,11 @@
         reading. If the bytes are truly not available (due to EOF), then the
         IncompleteRead exception can be used to detect the problem.
         """
+        # NOTE(gps): As of svn r74426 socket._fileobject.read(x) will never
+        # return less than x bytes unless EOF is encountered.  It now handles
+        # signal interruptions (socket.error EINTR) internally.  This code
+        # never caught that exception anyways.  It seems largely pointless.
+        # self.fp.read(amt) will work fine.
         s = []
         while amt > 0:
             chunk = self.fp.read(min(amt, MAXAMOUNT))


More information about the Python-checkins mailing list