Created
December 8, 2011 09:06
-
-
Save mdornseif/1446525 to your computer and use it in GitHub Desktop.
Handle GAW GZIP issues
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git a/huTools/http/engine_appengine.py b/huTools/http/engine_appengine.py | |
| index 74cf314..385df5d 100644 | |
| --- a/huTools/http/engine_appengine.py | |
| +++ b/huTools/http/engine_appengine.py | |
| @@ -12,8 +12,11 @@ Copyright (c) 2010, 2011 HUDORA. All rights reserved. | |
| import huTools.http.tools | |
| +import gzip | |
| import logging | |
| import os | |
| +import zlib | |
| + | |
| from google.appengine.api.urlfetch import create_rpc, make_fetch_call | |
| from google.appengine.api import memcache, urlfetch, urlfetch_errors | |
| from huTools.http import exceptions | |
| @@ -61,6 +64,7 @@ def request(url, method, content, headers, timeout=50, caching=None): | |
| raise exceptions.Timeout | |
| else: | |
| raise | |
| + handele_compression(result) | |
| ret = (int(result.status_code), result.headers, result.content) | |
| if caching: | |
| memcache.set(cachekey, ret, caching) | |
| @@ -144,7 +148,21 @@ class AsyncHttpResult(object): | |
| return self._resultcache | |
| # Cache miss or no cache wanted, do wait for the real http fetch | |
| result = self.rpc.get_result() | |
| + handele_compression(result) | |
| self._resultcache = self.returnhandler(result.status_code, result.headers, result.content) | |
| if self._caching: | |
| memcache.set(self._cachekey, self._resultcache, self._caching) | |
| return self._resultcache | |
| + | |
| +def handele_compression(self, result): | |
| + """Sometimes AppEngine does the decompression for us, sometimes not.""" | |
| + encoding = result.headers.get('content-encoding', None) | |
| + if encoding in ['gzip', 'deflate']: | |
| + if encoding == 'gzip': | |
| + result.content = gzip.GzipFile(fileobj=StringIO.StringIO(new_content)).read() | |
| + if encoding == 'deflate': | |
| + result.content = zlib.decompress(content) | |
| + result.headers['content-length'] = str(len(content)) | |
| + # Record the historical presence of the encoding in a way the won't interfere. | |
| + result.headers['-content-encoding'] = result.headers['content-encoding'] | |
| + del result.headers['content-encoding'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment