summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBert JW Regeer <bertjw@regeer.org>2018-08-31 21:05:59 -0600
committerBert JW Regeer <bertjw@regeer.org>2018-08-31 21:05:59 -0600
commitf9ad8bbb56aab91dd2847e4137b0e94bea1791c8 (patch)
treeb8938eb02a5805eb8927dcddb2d9594d793e0477
parent5ca4aa4c0563f3da3219fb190576431a0b9c395e (diff)
downloadwaitress-body-less-304.tar.gz
Ignore body for status codes that don't have a bodybody-less-304
For responses that should not include a message body, we now explicitly ignore the body from the application and no longer send it on to the requesting client. We also log a warning to let developers/users know.
-rw-r--r--waitress/task.py29
1 files changed, 26 insertions, 3 deletions
diff --git a/waitress/task.py b/waitress/task.py
index 91f4519..6ce1440 100644
--- a/waitress/task.py
+++ b/waitress/task.py
@@ -155,6 +155,7 @@ class Task(object):
content_length = None
content_bytes_written = 0
logged_write_excess = False
+ logged_write_no_body = False
complete = False
chunked_response = False
logger = logger
@@ -210,7 +211,12 @@ class Task(object):
[x.capitalize() for x in headername.split('-')]
)
if headername == 'Content-Length':
- content_length_header = headerval
+ if self.has_body:
+ content_length_header = headerval
+ else:
+ del response_headers[i]
+ continue # pragma: no cover
+
if headername == 'Date':
date_header = headerval
if headername == 'Server':
@@ -220,7 +226,11 @@ class Task(object):
# replace with properly capitalized version
response_headers[i] = (headername, headerval)
- if content_length_header is None and self.content_length is not None:
+ if (
+ content_length_header is None and
+ self.content_length is not None and
+ self.has_body
+ ):
content_length_header = str(self.content_length)
self.response_headers.append(
('Content-Length', content_length_header)
@@ -305,7 +315,8 @@ class Task(object):
rh = self.build_response_header()
channel.write_soon(rh)
self.wrote_header = True
- if data:
+
+ if data and self.has_body:
towrite = data
cl = self.content_length
if self.chunked_response:
@@ -322,6 +333,18 @@ class Task(object):
self.logged_write_excess = True
if towrite:
channel.write_soon(towrite)
+ else:
+ # Cheat, and tell the application we have written all of the bytes,
+ # even though the response shouldn't have a body and we are
+ # ignoring it entirely.
+ self.content_bytes_written += len(data)
+
+ if not self.logged_write_no_body:
+ self.logger.warning(
+ 'application-written content was ignored due to HTTP '
+ 'response that may not contain a message-body: (%s)' % self.status)
+ self.logged_write_no_body = True
+
class ErrorTask(Task):
""" An error task produces an error response