From 59d11faa173c7836dad280e40f49db460328515c Mon Sep 17 00:00:00 2001 From: Amadeus Date: Tue, 16 Sep 2014 16:44:14 +0800 Subject: [PATCH] addons processing bugfix --- lib/blackhole/router.py | 22 +++++++++++----------- lib/blackhole/utils.py | 7 +++++++ 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/lib/blackhole/router.py b/lib/blackhole/router.py index 874ad19..dd1f72a 100644 --- a/lib/blackhole/router.py +++ b/lib/blackhole/router.py @@ -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) @@ -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) diff --git a/lib/blackhole/utils.py b/lib/blackhole/utils.py index 7fdf45f..4a07d23 100644 --- a/lib/blackhole/utils.py +++ b/lib/blackhole/utils.py @@ -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