optional blocking in urllib3 (for now)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kenneth Reitz · committed by Kenneth Reitz · parent aa5e477 · Browse files · 1 file·+31 added−24 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+31 added−24 removed
- other:+31 added−24 removed
Provenance
- Commit
71d3490- Tree
d6c3a46- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent
aa5e477
1 file changed+31 added−24 removed
modified:requests/packages/urllib3/connectionpool.py+31 added-24 removed
74 unmodified lines75 self.reason = reason76 self.strict = strict77 78 @staticmethod79- def from_httplib(r):79+ def from_httplib(r, block=True):80 """81 Given an httplib.HTTPResponse instance, return a corresponding82 urllib3.HTTPResponse object.83 84 NOTE: This method will perform r.read() which will have side effects85 on the original http.HTTPResponse object.86 """87- tmp_data = r.read()88- try:89- if r.getheader('content-encoding') == 'gzip':90- log.debug("Received response with content-encoding: gzip, "91- "decompressing with gzip.")87 93- gzipper = gzip.GzipFile(fileobj=StringIO(tmp_data))94- data = gzipper.read()95- elif r.getheader('content-encoding') == 'deflate':96- log.debug("Received response with content-encoding: deflate, "97- "decompressing with zlib.")98- try:99- data = zlib.decompress(tmp_data)100- except zlib.error, e:101- data = zlib.decompress(tmp_data, -zlib.MAX_WBITS)102- else:103- data = tmp_data88+ if block:89+ tmp_data = r.read()90+ try:91+ if r.getheader('content-encoding') == 'gzip':92+ log.debug("Received response with content-encoding: gzip, "93+ "decompressing with gzip.")94 105- except IOError:106- raise HTTPError("Received response with content-encoding: %s, "107- "but failed to decompress it." %108- (r.getheader('content-encoding')))95+ gzipper = gzip.GzipFile(fileobj=StringIO(tmp_data))96+ data = gzipper.read()97+ elif r.getheader('content-encoding') == 'deflate':98+ log.debug("Received response with content-encoding: deflate, "99+ "decompressing with zlib.")100+ try:101+ data = zlib.decompress(tmp_data)102+ except zlib.error, e:103+ data = zlib.decompress(tmp_data, -zlib.MAX_WBITS)104+ else:105+ data = tmp_data106 107+ except IOError:108+ raise HTTPError("Received response with content-encoding: %s, "109+ "but failed to decompress it." %110+ (r.getheader('content-encoding')))111+ else:112+ data = None113+110- return HTTPResponse(data=data,114+ resp = HTTPResponse(data=data,115 headers=dict(r.getheaders()),116 status=r.status,117 version=r.version,118 reason=r.reason,119 strict=r.strict)120 121+ resp._raw = r122+ return resp123+124 # Backwards-compatibility methods for httplib.HTTPResponse125 def getheaders(self):126 return self.headers127 140 unmodified lines268 return (url.startswith('/') or269 get_host(url) == (self.scheme, self.host, self.port))270 271 def urlopen(self, method, url, body=None, headers=None, retries=3,265- redirect=True, assert_same_host=True):272+ redirect=True, assert_same_host=True, block=True):273 """274 Get a connection from the pool and perform an HTTP request.275 276 method52 unmodified lines329 330 # from_httplib will perform httplib_response.read() which will have331 # the side effect of letting us use this connection for another332 # request.326- response = HTTPResponse.from_httplib(httplib_response)333+ response = HTTPResponse.from_httplib(httplib_response, block=block)334 335 # Put the connection back to be reused336 self._put_conn(conn)337 214 unmodified lines