From 8bfb97a3a8bfe1a1db66560a9dba21277b0c006e Mon Sep 17 00:00:00 2001 From: Tim Burke Date: Wed, 27 Jun 2018 15:54:01 -0700 Subject: [PATCH] Content-Length enforcement fixups Change-Id: Ice8144700cf447ecf5c6175dc64aa7662013fba5 Related-Change: I74d8c13eba2a4917b5a116875b51a781b33a7abf --- swift/common/middleware/catch_errors.py | 10 +++++----- swift/common/middleware/dlo.py | 1 - swift/common/swob.py | 3 +++ 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/swift/common/middleware/catch_errors.py b/swift/common/middleware/catch_errors.py index 106238daab..35303358ab 100644 --- a/swift/common/middleware/catch_errors.py +++ b/swift/common/middleware/catch_errors.py @@ -69,7 +69,6 @@ class CatchErrorsContext(WSGIContext): trans_id = generate_trans_id(trans_id_suffix) env['swift.trans_id'] = trans_id - method = env['REQUEST_METHOD'] self.logger.txn_id = trans_id try: # catch any errors in the pipeline @@ -101,7 +100,10 @@ class CatchErrorsContext(WSGIContext): # the socket. In that case, we truncate the response body at N bytes # and raise an exception to stop any more bytes from being # generated and also to kill the TCP connection. - if self._response_headers: + if env['REQUEST_METHOD'] == 'HEAD': + resp = enforce_byte_count(resp, 0) + + elif self._response_headers: content_lengths = [val for header, val in self._response_headers if header.lower() == "content-length"] if len(content_lengths) == 1: @@ -110,9 +112,7 @@ class CatchErrorsContext(WSGIContext): except ValueError: pass else: - resp = enforce_byte_count( - resp, - 0 if method == 'HEAD' else content_length) + resp = enforce_byte_count(resp, content_length) # make sure the response has the trans_id if self._response_headers is None: diff --git a/swift/common/middleware/dlo.py b/swift/common/middleware/dlo.py index 5c1ba9aeed..07cfc92d7c 100644 --- a/swift/common/middleware/dlo.py +++ b/swift/common/middleware/dlo.py @@ -155,7 +155,6 @@ class GetContext(WSGIContext): con_resp = con_req.get_response(self.dlo.app) if not is_success(con_resp.status_int): if req.method == 'HEAD': - close_if_possible(con_resp.app_iter) con_resp.body = '' return con_resp, None with closing_if_possible(con_resp.app_iter): diff --git a/swift/common/swob.py b/swift/common/swob.py index b7163065d2..953b93fc8d 100644 --- a/swift/common/swob.py +++ b/swift/common/swob.py @@ -316,6 +316,7 @@ def _resp_body_property(): value = value.encode('utf-8') if isinstance(value, str): self.content_length = len(value) + close_if_possible(self._app_iter) self._app_iter = None self._body = value @@ -400,6 +401,7 @@ def _resp_app_iter_property(): elif value is not None: self.content_length = None self._body = None + close_if_possible(self._app_iter) self._app_iter = value return property(getter, setter, @@ -1115,6 +1117,7 @@ class Response(object): self.conditional_response = conditional_response self._conditional_etag = conditional_etag self.request = request + self._app_iter = None self.body = body self.app_iter = app_iter self.response_iter = None