Merge "Content-Length enforcement fixups"

This commit is contained in:
Zuul 2018-06-29 05:43:39 +00:00 committed by Gerrit Code Review
commit 9f24b27019
3 changed files with 8 additions and 6 deletions

View File

@ -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:

View File

@ -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):

View File

@ -337,6 +337,7 @@ def _resp_body_property():
value = value.encode('utf-8')
if isinstance(value, six.binary_type):
self.content_length = len(value)
close_if_possible(self._app_iter)
self._app_iter = None
self._body = value
@ -421,6 +422,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,
@ -1162,6 +1164,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