diff options
author | Kristján Valur Jónsson <kristjan@ccpgames.com> | 2009-01-11 16:23:37 (GMT) |
---|---|---|
committer | Kristján Valur Jónsson <kristjan@ccpgames.com> | 2009-01-11 16:23:37 (GMT) |
commit | 3c43fcba8b67ea0cec4a443c755ce5f25990a6cf (patch) | |
tree | edeacfd07bfbaeb7ea70377b65548447408acf48 /Lib/httplib.py | |
parent | eefda27e97271f61b669410f7f6554466045f52a (diff) | |
download | cpython-3c43fcba8b67ea0cec4a443c755ce5f25990a6cf.zip cpython-3c43fcba8b67ea0cec4a443c755ce5f25990a6cf.tar.gz cpython-3c43fcba8b67ea0cec4a443c755ce5f25990a6cf.tar.bz2 |
Issue 4879: Allow buffering for HTTPResponse
Diffstat (limited to 'Lib/httplib.py')
-rw-r--r-- | Lib/httplib.py | 44 |
1 files changed, 28 insertions, 16 deletions
diff --git a/Lib/httplib.py b/Lib/httplib.py index ba58e20..85e49ab 100644 --- a/Lib/httplib.py +++ b/Lib/httplib.py @@ -325,13 +325,18 @@ class HTTPResponse: # See RFC 2616 sec 19.6 and RFC 1945 sec 6 for details. - def __init__(self, sock, debuglevel=0, strict=0, method=None): - # The buffer size is specified as zero, because the headers of - # the response are read with readline(). If the reads were - # buffered the readline() calls could consume some of the - # response, which make be read via a recv() on the underlying - # socket. - self.fp = sock.makefile('rb', 0) + 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 + self.fp = sock.makefile('rb') + else: + # The buffer size is specified as zero, because the headers of + # the response are read with readline(). If the reads were + # buffered the readline() calls could consume some of the + # response, which make be read via a recv() on the underlying + # socket. + self.fp = sock.makefile('rb', 0) self.debuglevel = debuglevel self.strict = strict self._method = method @@ -935,7 +940,7 @@ class HTTPConnection: self.putheader(hdr, value) self.endheaders(body) - def getresponse(self): + def getresponse(self, buffering=False): "Get the response from the server." # if a prior response has been completed, then forget about it. @@ -961,13 +966,15 @@ class HTTPConnection: if self.__state != _CS_REQ_SENT or self.__response: raise ResponseNotReady() + args = (self.sock,) + kwds = {"strict":self.strict, "method":self._method} if self.debuglevel > 0: - response = self.response_class(self.sock, self.debuglevel, - strict=self.strict, - method=self._method) - else: - response = self.response_class(self.sock, strict=self.strict, - method=self._method) + args += (self.debuglevel,) + if buffering: + #only add this keyword if non-default, for compatibility with + #other response_classes. + kwds["buffering"] = True; + response = self.response_class(*args, **kwds) response.begin() assert response.will_close != _UNKNOWN @@ -1031,7 +1038,7 @@ class HTTP: "Provide a getfile, since the superclass' does not use this concept." return self.file - def getreply(self): + def getreply(self, buffering=False): """Compat definition since superclass does not define it. Returns a tuple consisting of: @@ -1040,7 +1047,12 @@ class HTTP: - any RFC822 headers in the response from the server """ try: - response = self._conn.getresponse() + if not buffering: + response = self._conn.getresponse() + else: + #only add this keyword if non-default for compatibility + #with other connection classes + response = self._conn.getresponse(buffering) except BadStatusLine, e: ### hmm. if getresponse() ever closes the socket on a bad request, ### then we are going to have problems with self.sock |