Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 11 additions & 18 deletions mechanize/_gzip.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import _response
import _urllib2_fork
from urllib import addinfourl


# GzipConsumer was taken from Fredrik Lundh's effbot.org-0.1-20041009 library
Expand Down Expand Up @@ -61,31 +62,22 @@ def close(self):
# --------------------------------------------------------------------

# the rest of this module is John Lee's stupid code, not
# Fredrik's nice code :-)
# Fredrik's nice code :-) (later changed by Jason Kotenko)

class stupid_gzip_consumer:
def __init__(self): self.data = []
def feed(self, data): self.data.append(data)

class stupid_gzip_wrapper(_response.closeable_response):
class stupid_gzip_wrapper(addinfourl):
def __init__(self, response):
self._response = response

c = stupid_gzip_consumer()
gzc = GzipConsumer(c)
cc = stupid_gzip_consumer()
gzc = GzipConsumer(cc)
gzc.feed(response.read())
self.__data = StringIO("".join(c.data))

def read(self, size=-1):
return self.__data.read(size)
def readline(self, size=-1):
return self.__data.readline(size)
def readlines(self, sizehint=-1):
return self.__data.readlines(sizehint)
self.__data = StringIO("".join(cc.data))
self.msg = response.msg
addinfourl.__init__(self, self.__data, response.headers,
response.url, response.code)

def __getattr__(self, name):
# delegate unknown methods/attributes
return getattr(self._response, name)

class HTTPGzipProcessor(_urllib2_fork.BaseHandler):
handler_order = 200 # response processing before HTTPEquivProcessor
Expand All @@ -96,9 +88,10 @@ def http_request(self, request):

def http_response(self, request, response):
# post-process response
enc_hdrs = response.info().getheaders("Content-encoding")
enc_hdrs = response.info().getheaders("content-encoding")
for enc_hdr in enc_hdrs:
if ("gzip" in enc_hdr) or ("compress" in enc_hdr):
del response.headers['content-encoding']
return stupid_gzip_wrapper(response)
return response

Expand Down