merging / reimplementing r68532 from the trunk to Py3k

Enable buffering for HTTPResponse's fp.  read() behaves identically for buffered and non-buffered IO.  read(n) also won't block if n bytes are availble on the socket.  There is therefore no reason not to use buffering.  The reason 2.x disables buffering by default, that some clients may be accessing the underlying socket directly and so bypass the buffering buffer, doesn't apply in 3.x with its redesigned IO library.
See issue 4448 and issue 4879
parent bc186a87
...@@ -265,14 +265,14 @@ class HTTPResponse: ...@@ -265,14 +265,14 @@ class HTTPResponse:
# accepts iso-8859-1. # accepts iso-8859-1.
def __init__(self, sock, debuglevel=0, strict=0, method=None): def __init__(self, sock, debuglevel=0, strict=0, method=None):
# XXX If the response includes a content-length header, we # If the response includes a content-length header, we
# need to make sure that the client doesn't read more than the # need to make sure that the client doesn't read more than the
# specified number of bytes. If it does, it will block until # specified number of bytes. If it does, it will block until
# the server times out and closes the connection. (The only # the server times out and closes the connection. (The only
# applies to HTTP/1.1 connections.) Since some clients access # applies to HTTP/1.1 connections.) This will happen if a self.fp.read()
# self.fp directly rather than calling read(), this is a little # is done (without a size) whether self.fp is buffered or not.
# tricky. # So, no self.fp.read() by clients unless they know what they are doing.
self.fp = sock.makefile("rb", 0) self.fp = sock.makefile("rb")
self.debuglevel = debuglevel self.debuglevel = debuglevel
self.strict = strict self.strict = strict
self._method = method self._method = method
......
...@@ -1129,7 +1129,7 @@ class Transport: ...@@ -1129,7 +1129,7 @@ class Transport:
self.verbose = verbose self.verbose = verbose
return self._parse_response(resp, None) return self.parse_response(resp)
## ##
# Create parser. # Create parser.
...@@ -1212,29 +1212,12 @@ class Transport: ...@@ -1212,29 +1212,12 @@ class Transport:
# @return Response tuple and target method. # @return Response tuple and target method.
def parse_response(self, file): def parse_response(self, file):
# compatibility interface
return self._parse_response(file, None)
##
# Parse response (alternate interface). This is similar to the
# parse_response method, but also provides direct access to the
# underlying socket object (where available).
#
# @param file Stream.
# @param sock Socket handle (or None, if the socket object
# could not be accessed).
# @return Response tuple and target method.
def _parse_response(self, file, sock):
# read response from input file/socket, and parse it # read response from input file/socket, and parse it
p, u = self.getparser() p, u = self.getparser()
while 1: while 1:
if sock: response = file.read(1024)
response = sock.recv(1024)
else:
response = file.read(1024)
if not response: if not response:
break break
if self.verbose: if self.verbose:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment