Skip to content

Commit

Permalink
Force connection close after protocol upgrade
Browse files Browse the repository at this point in the history
A new protocol, such as WebSockets or HTTP/2, may manage the framing of
multiple messages or requests of that protocol over a single connection.

- Close connections after completion of any upgraded response.

- Close connections after a response with status code less than 200. Any
  such response should be only for a protocol upgrade since applications
  never see any `Expect` header.

- Ensure that the `status_code` member always exists on instances of the
  `Response` class (close #1210).
  • Loading branch information
tilgovi committed Dec 28, 2023
1 parent b5d78e8 commit 4991ed9
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions gunicorn/http/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ def __init__(self, req, sock, cfg):
self.sock = sock
self.version = SERVER
self.status = None
self.status_code is None
self.chunked = False
self.must_close = False
self.headers = []
Expand All @@ -218,15 +219,20 @@ def force_close(self):
self.must_close = True

def should_close(self):
if self.must_close or self.req.should_close():
if self.must_close:
# example: worker shutting down
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
if self.req.should_close():
# example: connection close or upgrade header
return True
if self.upgrade:
# close after the new protocol terminates
return True
if self.response_length is None and not self.chunked:
# close if there is a response body of unknown length
# the client cannot otherwise know when the response ends
return self.req.method != 'HEAD' and self.status_code not in (204, 304)
return False

def start_response(self, status, headers, exc_info=None):
if exc_info:
Expand Down

0 comments on commit 4991ed9

Please sign in to comment.