From: Ben Darnell Date: Thu, 5 Jan 2012 08:27:45 +0000 (-0800) Subject: Handle redirects before processing data and sending it to streaming_callback. X-Git-Tag: v2.2.0~48 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bd034933356f7ce6db44263bed48a0fe626479d5;p=thirdparty%2Ftornado.git Handle redirects before processing data and sending it to streaming_callback. Previously, if you were using follow_redirects and streaming_callback and hit a redirect with a non-empty body, that body would be passed to streaming_callback as well. As a side effect, this improves compatibility with some misconfigured sites that send Content-Encoding: gzip on redirects without actually compressing the body. --- diff --git a/tornado/httpclient.py b/tornado/httpclient.py index 443657c03..a6ad4fa13 100644 --- a/tornado/httpclient.py +++ b/tornado/httpclient.py @@ -396,7 +396,8 @@ def main(): for arg in args: try: response = client.fetch(arg, - follow_redirects=options.follow_redirects) + follow_redirects=options.follow_redirects + ) except HTTPError, e: if e.response is not None: response = e.response diff --git a/tornado/simple_httpclient.py b/tornado/simple_httpclient.py index 15773bb3b..ce2762f1d 100644 --- a/tornado/simple_httpclient.py +++ b/tornado/simple_httpclient.py @@ -355,16 +355,6 @@ class _HTTPConnection(object): if self._timeout is not None: self.io_loop.remove_timeout(self._timeout) self._timeout = None - if self._decompressor: - data = self._decompressor.decompress(data) - if self.request.streaming_callback: - if self.chunks is None: - # if chunks is not None, we already called streaming_callback - # in _on_chunk_data - self.request.streaming_callback(data) - buffer = BytesIO() - else: - buffer = BytesIO(data) # TODO: don't require one big string? original_request = getattr(self.request, "original_request", self.request) if (self.request.follow_redirects and @@ -382,6 +372,16 @@ class _HTTPConnection(object): self.client.fetch(new_request, final_callback) self.stream.close() return + if self._decompressor: + data = self._decompressor.decompress(data) + if self.request.streaming_callback: + if self.chunks is None: + # if chunks is not None, we already called streaming_callback + # in _on_chunk_data + self.request.streaming_callback(data) + buffer = BytesIO() + else: + buffer = BytesIO(data) # TODO: don't require one big string? response = HTTPResponse(original_request, self.code, headers=self.headers, request_time=time.time() - self.start_time,