From 8b778c706af47f4804c9c4d72176185142e6127c Mon Sep 17 00:00:00 2001 From: gholt Date: Wed, 13 Jun 2012 00:40:44 +0000 Subject: [PATCH] Make proxy-logging more like eventlet.posthook The old use of Eventlet's posthook process meant that responses that forgot to include content-length or transfer-encoding headers would get one tacked on, if Eventlet could guess what was probably meant. I added a bit of that logic into proxy-logging now as we saw some errors resulting from this. Fixes Bug #1012714 Change-Id: I671453eaf3704eab814ff12c4625ba7d749cc7ed --- swift/common/middleware/proxy_logging.py | 3 +++ .../common/middleware/test_proxy_logging.py | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/swift/common/middleware/proxy_logging.py b/swift/common/middleware/proxy_logging.py index 66c853c1af..83bcd15e7a 100644 --- a/swift/common/middleware/proxy_logging.py +++ b/swift/common/middleware/proxy_logging.py @@ -167,6 +167,9 @@ class ProxyLoggingMiddleware(object): else: if not chunk: start_response_args[0][1].append(('content-length', '0')) + elif isinstance(iterable, list): + start_response_args[0][1].append( + ('content-length', str(sum(len(i) for i in iterable)))) else: raise Exception('WSGI [proxy-logging]: No content-length ' 'or transfer-encoding header sent and there is ' diff --git a/test/unit/common/middleware/test_proxy_logging.py b/test/unit/common/middleware/test_proxy_logging.py index a5dd9511a0..5508ec454b 100644 --- a/test/unit/common/middleware/test_proxy_logging.py +++ b/test/unit/common/middleware/test_proxy_logging.py @@ -34,6 +34,17 @@ class FakeApp(object): return self.body +class FakeAppNoContentLengthNoTransferEncoding(object): + def __init__(self, body=['FAKE APP']): + self.body = body + + def __call__(self, env, start_response): + start_response('200 OK', [('Content-Type', 'text/plain')]) + while env['wsgi.input'].read(5): + pass + return self.body + + class FileLikeExceptor(object): def __init__(self): pass @@ -226,5 +237,14 @@ class TestProxyLogging(unittest.TestCase): self.assertEquals(log_parts[6], '499') self.assertEquals(log_parts[10], '-') # read length + def test_no_content_length_no_transfer_encoding(self): + app = proxy_logging.ProxyLoggingMiddleware( + FakeAppNoContentLengthNoTransferEncoding(), {}) + app.access_logger = FakeLogger() + req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'}) + resp = app(req.environ, start_response) + body = ''.join(resp) + + if __name__ == '__main__': unittest.main()