Skip to content

Commit c8c0a9b

Browse files
committed
Improve error reporting when header is too long.
Refs #1471.
1 parent e10eeba commit c8c0a9b

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/websockets/legacy/server.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,14 @@ async def handler(self) -> None:
199199
elif isinstance(exc, InvalidHandshake):
200200
if self.debug:
201201
self.logger.debug("! invalid handshake", exc_info=True)
202+
exc_str = f"{exc}"
203+
while exc.__cause__ is not None:
204+
exc = exc.__cause__
205+
exc_str += f"; {exc}"
202206
status, headers, body = (
203207
http.HTTPStatus.BAD_REQUEST,
204208
Headers(),
205-
f"Failed to open a WebSocket connection: {exc}.\n".encode(),
209+
f"Failed to open a WebSocket connection: {exc_str}.\n".encode(),
206210
)
207211
else:
208212
self.logger.error("opening handshake failed", exc_info=True)

src/websockets/server.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,13 @@ def accept(self, request: Request) -> Response:
163163
self.handshake_exc = exc
164164
if self.debug:
165165
self.logger.debug("! invalid handshake", exc_info=True)
166+
exc_str = f"{exc}"
167+
while exc.__cause__ is not None:
168+
exc = exc.__cause__
169+
exc_str += f"; {exc}"
166170
return self.reject(
167171
http.HTTPStatus.BAD_REQUEST,
168-
f"Failed to open a WebSocket connection: {exc}.\n",
172+
f"Failed to open a WebSocket connection: {exc_str}.\n",
169173
)
170174
except Exception as exc:
171175
# Handle exceptions raised by user-provided select_subprotocol and

0 commit comments

Comments
 (0)