Skip to content

Commit

Permalink
Improve error reporting when header is too long.
Browse files Browse the repository at this point in the history
Refs #1471.
  • Loading branch information
aaugustin committed Jul 21, 2024
1 parent e10eeba commit c8c0a9b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/websockets/legacy/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,14 @@ async def handler(self) -> None:
elif isinstance(exc, InvalidHandshake):
if self.debug:
self.logger.debug("! invalid handshake", exc_info=True)
exc_str = f"{exc}"
while exc.__cause__ is not None:
exc = exc.__cause__
exc_str += f"; {exc}"
status, headers, body = (
http.HTTPStatus.BAD_REQUEST,
Headers(),
f"Failed to open a WebSocket connection: {exc}.\n".encode(),
f"Failed to open a WebSocket connection: {exc_str}.\n".encode(),
)
else:
self.logger.error("opening handshake failed", exc_info=True)
Expand Down
6 changes: 5 additions & 1 deletion src/websockets/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,13 @@ def accept(self, request: Request) -> Response:
self.handshake_exc = exc
if self.debug:
self.logger.debug("! invalid handshake", exc_info=True)
exc_str = f"{exc}"
while exc.__cause__ is not None:
exc = exc.__cause__
exc_str += f"; {exc}"
return self.reject(
http.HTTPStatus.BAD_REQUEST,
f"Failed to open a WebSocket connection: {exc}.\n",
f"Failed to open a WebSocket connection: {exc_str}.\n",
)
except Exception as exc:
# Handle exceptions raised by user-provided select_subprotocol and
Expand Down

0 comments on commit c8c0a9b

Please sign in to comment.