Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force connection close after protocol upgrade #3126

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = 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:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why changing the logic there ? Also this doesn't take care anymore about status code < 100. Can you elaborate? (As a side note I'm not a fan of these one line boolean combination, decoupling in multiple if helps to trace the code .

# 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