Skip to content

Commit

Permalink
addons processing bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
gliheng committed Sep 16, 2014
1 parent 0180d45 commit 59d11fa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
22 changes: 11 additions & 11 deletions lib/blackhole/router.py
Expand Up @@ -211,20 +211,22 @@ def postOperations(self, addons, request, response):
''' Post processing when response is received
'''
headers = response[1]
# iterable was changed to bytes here
# iterable is changed to bytes here
body = b''.join(response[2])

# decompress
hasgzip = any(header[0] == 'Content-Encoding' and 'gzip' in header[1] for header in headers)
if hasgzip:
body = response[2] = gzip.GzipFile(fileobj=BytesIO(body)).read()
body = gzip.GzipFile(fileobj=BytesIO(body)).read()

# try to get contenttype and encoding from headers
type, coding = utils.content_type(headers)
coding = coding or 'utf-8'

if type == 'text/html':
response[2] = body.decode(coding, errors='ignore')
body = body.decode(coding, errors='ignore')

response[2] = body

for addon in addons:
logger.info('Post processing with addon: %s' % addon)
Expand All @@ -239,21 +241,19 @@ def postOperations(self, addons, request, response):
if hasattr(addonObj, 'post_edit'):
addonObj.post_edit()

# make it iterable again
if type == 'text/html':
response[2] = [response[2].encode(coding, errors='ignore')]
response[2] = response[2].encode(coding, errors='ignore')

# fix headers
# fix content-length
length = 0
for item in response[2]:
length += len(item)
# body is made iterable again
response[2] = [response[2]]

# fix headers
new_headers = []
for header in headers:
key = header[0]
if key == 'Content-Length':
header[1] = str(length)
# fix content-length
header[1] = str(utils.get_content_length(response[2]))
if not key == 'Content-Encoding':
new_headers.append(header)

Expand Down
7 changes: 7 additions & 0 deletions lib/blackhole/utils.py
Expand Up @@ -126,3 +126,10 @@ def content_type(headers = []):
return m.groups()

return [None, None]

def get_content_length(iterable):
length = 0
for item in iterable:
length += len(item)

return length

0 comments on commit 59d11fa

Please sign in to comment.