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:
parent
4c6e8088f5
commit
8cabfa352b
|
@ -328,8 +328,12 @@ class HTTPResponse:
|
|||
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 @@ class HTTPResponse:
|
|||
# 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 @@ class HTTPResponse:
|
|||
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 @@ class HTTPResponse:
|
|||
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))
|
||||
|
|
Loading…
Reference in New Issue