diff --git a/swift/common/middleware/proxy_logging.py b/swift/common/middleware/proxy_logging.py index f774afb203..53149b5df8 100644 --- a/swift/common/middleware/proxy_logging.py +++ b/swift/common/middleware/proxy_logging.py @@ -78,6 +78,7 @@ from swift.common.middleware.catch_errors import enforce_byte_count from swift.common.swob import Request from swift.common.utils import (get_logger, get_remote_client, config_true_value, reiterate, + close_if_possible, InputProxy, list_from_csv, get_policy_index, split_path, StrAnonymizer, StrFormatTime, LogStringFormatter) @@ -427,7 +428,7 @@ class ProxyLoggingMiddleware(object): req, status_int, input_proxy.bytes_received, bytes_sent, start_time, time.time(), resp_headers=resp_headers, ttfb=ttfb, wire_status_int=wire_status_int) - iterator.close() + close_if_possible(iterator) try: iterable = self.app(env, my_start_response) diff --git a/test/unit/common/middleware/test_proxy_logging.py b/test/unit/common/middleware/test_proxy_logging.py index 6e7b23f504..d33b7bebea 100644 --- a/test/unit/common/middleware/test_proxy_logging.py +++ b/test/unit/common/middleware/test_proxy_logging.py @@ -33,7 +33,8 @@ from test.unit.common.middleware.helpers import FakeAppThatExcepts class FakeApp(object): - def __init__(self, body=None, response_str='200 OK', policy_idx='0'): + def __init__(self, body=None, response_str='200 OK', policy_idx='0', + chunked=False): if body is None: body = [b'FAKE APP'] elif isinstance(body, six.binary_type): @@ -42,6 +43,7 @@ class FakeApp(object): self.body = body self.response_str = response_str self.policy_idx = policy_idx + self.chunked = chunked def __call__(self, env, start_response): try: @@ -52,9 +54,12 @@ class FakeApp(object): is_container_or_object_req = False headers = [('Content-Type', 'text/plain')] - if not hasattr(self.body, 'close'): + if self.chunked: + headers.append(('Transfer-Encoding', 'chunked')) + elif not hasattr(self.body, 'close'): content_length = sum(map(len, self.body)) headers.append(('Content-Length', str(content_length))) + if is_container_or_object_req and self.policy_idx is not None: headers.append(('X-Backend-Storage-Policy-Index', str(self.policy_idx))) @@ -640,6 +645,13 @@ class TestProxyLogging(unittest.TestCase): [x for x in resp] self.assertTrue(body.closed) + def test_chunked_response(self): + app = proxy_logging.ProxyLoggingMiddleware(FakeApp(chunked=True), {}) + req = Request.blank('/') + resp = app(req.environ, start_response) + # exhaust generator + [x for x in resp] + def test_proxy_client_logging(self): app = proxy_logging.ProxyLoggingMiddleware(FakeApp(), {}) app.access_logger = FakeLogger()