-
-
Notifications
You must be signed in to change notification settings - Fork 753
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
Send HTTP 400 response for invalid request #1352
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -133,7 +133,8 @@ def data_received(self, data): | |
except httptools.HttpParserError as exc: | ||
msg = "Invalid HTTP request received." | ||
self.logger.warning(msg, exc_info=exc) | ||
self.transport.close() | ||
self.send_400_response(msg) | ||
return | ||
except httptools.HttpParserUpgrade: | ||
self.handle_upgrade() | ||
if data == b"" and not self.transport.is_closing(): | ||
|
@@ -148,27 +149,7 @@ def handle_upgrade(self): | |
if upgrade_value != b"websocket" or self.ws_protocol_class is None: | ||
msg = "Unsupported upgrade request." | ||
self.logger.warning(msg) | ||
|
||
from uvicorn.protocols.websockets.auto import AutoWebSocketsProtocol | ||
|
||
if AutoWebSocketsProtocol is None: | ||
msg = "No supported WebSocket library detected. Please use 'pip install uvicorn[standard]', or install 'websockets' or 'wsproto' manually." # noqa: E501 | ||
self.logger.warning(msg) | ||
|
||
content = [STATUS_LINE[400]] | ||
for name, value in self.default_headers: | ||
content.extend([name, b": ", value, b"\r\n"]) | ||
content.extend( | ||
[ | ||
b"content-type: text/plain; charset=utf-8\r\n", | ||
b"content-length: " + str(len(msg)).encode("ascii") + b"\r\n", | ||
b"connection: close\r\n", | ||
b"\r\n", | ||
msg.encode("ascii"), | ||
] | ||
) | ||
self.transport.write(b"".join(content)) | ||
self.transport.close() | ||
self.send_400_response(msg) | ||
return | ||
|
||
if self.logger.level <= TRACE_LOG_LEVEL: | ||
|
@@ -190,6 +171,29 @@ def handle_upgrade(self): | |
protocol.data_received(b"".join(output)) | ||
self.transport.set_protocol(protocol) | ||
|
||
def send_400_response(self, msg: str): | ||
|
||
from uvicorn.protocols.websockets.auto import AutoWebSocketsProtocol | ||
|
||
if AutoWebSocketsProtocol is None: | ||
msg = "No supported WebSocket library detected. Please use 'pip install uvicorn[standard]', or install 'websockets' or 'wsproto' manually." # noqa: E501 | ||
self.logger.warning(msg) | ||
Comment on lines
+176
to
+180
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like it needs to be deleted. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was part of improving user experience in #926 @tomchristie There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps it just needs to stay within the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ha yes you're right I didn't notice if was sent also in that case, I misread thinking it was just for the upgrade case |
||
|
||
content = [STATUS_LINE[400]] | ||
for name, value in self.default_headers: | ||
content.extend([name, b": ", value, b"\r\n"]) | ||
content.extend( | ||
[ | ||
b"content-type: text/plain; charset=utf-8\r\n", | ||
b"content-length: " + str(len(msg)).encode("ascii") + b"\r\n", | ||
b"connection: close\r\n", | ||
b"\r\n", | ||
msg.encode("ascii"), | ||
] | ||
) | ||
self.transport.write(b"".join(content)) | ||
self.transport.close() | ||
|
||
# Parser callbacks | ||
def on_url(self, url): | ||
method = self.parser.get_method() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like it needs to be deleted.