From 89265659dd89e290dc3abd60faa81d3105e52349 Mon Sep 17 00:00:00 2001 From: Paul Aurich Date: Sun, 12 Jul 2015 13:50:46 -0700 Subject: [PATCH] Ensure response to HEAD request won't have message body Ensure that Gunicorn won't try to use chunked transfer-encoding for responses to a HEAD request, so that `Response.close` will not write a terminating chunk. Responses to a HEAD request MUST NOT have a message-body. The application is still responsible for ensuring no message body is actually generated in response to a HEAD request. --- gunicorn/http/wsgi.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/gunicorn/http/wsgi.py b/gunicorn/http/wsgi.py index 6ca3b4fb7b..0e046ed4db 100644 --- a/gunicorn/http/wsgi.py +++ b/gunicorn/http/wsgi.py @@ -230,6 +230,8 @@ def should_close(self): return True if self.response_length is not None or self.chunked: return False + if self.req.method == 'HEAD': + return False if self.status_code < 200 or self.status_code in (204, 304): return False return True @@ -287,6 +289,9 @@ def is_chunked(self): return False elif self.req.version <= (1, 0): return False + elif self.req.method == 'HEAD': + # Responses to a HEAD request MUST NOT contain a response body. + return False elif self.status_code in (204, 304): # Do not use chunked responses when the response is guaranteed to # not have a response body.