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.
This commit is contained in:
Gregory P. Smith 2009-08-15 22:39:03 +00:00
parent 4c6e8088f5
commit 8cabfa352b
1 changed files with 14 additions and 1 deletions

View File

@ -328,8 +328,12 @@ class HTTPResponse:
def __init__(self, sock, debuglevel=0, strict=0, method=None, buffering=False): def __init__(self, sock, debuglevel=0, strict=0, method=None, buffering=False):
if buffering: if buffering:
# The caller won't be using any sock.recv() calls, so 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') 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: else:
# The buffer size is specified as zero, because the headers of # The buffer size is specified as zero, because the headers of
# the response are read with readline(). If the reads were # the response are read with readline(). If the reads were
@ -337,6 +341,7 @@ class HTTPResponse:
# response, which make be read via a recv() on the underlying # response, which make be read via a recv() on the underlying
# socket. # socket.
self.fp = sock.makefile('rb', 0) self.fp = sock.makefile('rb', 0)
self._must_close = False
self.debuglevel = debuglevel self.debuglevel = debuglevel
self.strict = strict self.strict = strict
self._method = method self._method = method
@ -474,6 +479,9 @@ class HTTPResponse:
self.will_close = 1 self.will_close = 1
def _check_close(self): def _check_close(self):
if self._must_close:
return True
conn = self.msg.getheader('connection') conn = self.msg.getheader('connection')
if self.version == 11: if self.version == 11:
# An HTTP/1.1 proxy is assumed to stay open unless # An HTTP/1.1 proxy is assumed to stay open unless
@ -622,6 +630,11 @@ class HTTPResponse:
reading. If the bytes are truly not available (due to EOF), then the reading. If the bytes are truly not available (due to EOF), then the
IncompleteRead exception can be used to detect the problem. 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 = [] s = []
while amt > 0: while amt > 0:
chunk = self.fp.read(min(amt, MAXAMOUNT)) chunk = self.fp.read(min(amt, MAXAMOUNT))