Merge "Do not require the close method for response iterator"

This commit is contained in:
Zuul 2020-11-03 06:29:01 +00:00 committed by Gerrit Code Review
commit 06460cc8a5
2 changed files with 16 additions and 3 deletions

View File

@ -78,6 +78,7 @@ from swift.common.middleware.catch_errors import enforce_byte_count
from swift.common.swob import Request from swift.common.swob import Request
from swift.common.utils import (get_logger, get_remote_client, from swift.common.utils import (get_logger, get_remote_client,
config_true_value, reiterate, config_true_value, reiterate,
close_if_possible,
InputProxy, list_from_csv, get_policy_index, InputProxy, list_from_csv, get_policy_index,
split_path, StrAnonymizer, StrFormatTime, split_path, StrAnonymizer, StrFormatTime,
LogStringFormatter) LogStringFormatter)
@ -427,7 +428,7 @@ class ProxyLoggingMiddleware(object):
req, status_int, input_proxy.bytes_received, bytes_sent, req, status_int, input_proxy.bytes_received, bytes_sent,
start_time, time.time(), resp_headers=resp_headers, start_time, time.time(), resp_headers=resp_headers,
ttfb=ttfb, wire_status_int=wire_status_int) ttfb=ttfb, wire_status_int=wire_status_int)
iterator.close() close_if_possible(iterator)
try: try:
iterable = self.app(env, my_start_response) iterable = self.app(env, my_start_response)

View File

@ -33,7 +33,8 @@ from test.unit.common.middleware.helpers import FakeAppThatExcepts
class FakeApp(object): 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: if body is None:
body = [b'FAKE APP'] body = [b'FAKE APP']
elif isinstance(body, six.binary_type): elif isinstance(body, six.binary_type):
@ -42,6 +43,7 @@ class FakeApp(object):
self.body = body self.body = body
self.response_str = response_str self.response_str = response_str
self.policy_idx = policy_idx self.policy_idx = policy_idx
self.chunked = chunked
def __call__(self, env, start_response): def __call__(self, env, start_response):
try: try:
@ -52,9 +54,12 @@ class FakeApp(object):
is_container_or_object_req = False is_container_or_object_req = False
headers = [('Content-Type', 'text/plain')] 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)) content_length = sum(map(len, self.body))
headers.append(('Content-Length', str(content_length))) headers.append(('Content-Length', str(content_length)))
if is_container_or_object_req and self.policy_idx is not None: if is_container_or_object_req and self.policy_idx is not None:
headers.append(('X-Backend-Storage-Policy-Index', headers.append(('X-Backend-Storage-Policy-Index',
str(self.policy_idx))) str(self.policy_idx)))
@ -640,6 +645,13 @@ class TestProxyLogging(unittest.TestCase):
[x for x in resp] [x for x in resp]
self.assertTrue(body.closed) 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): def test_proxy_client_logging(self):
app = proxy_logging.ProxyLoggingMiddleware(FakeApp(), {}) app = proxy_logging.ProxyLoggingMiddleware(FakeApp(), {})
app.access_logger = FakeLogger() app.access_logger = FakeLogger()