Skip to content
Closed
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
11 changes: 10 additions & 1 deletion src/websockets/sync/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,14 @@ def __exit__(
) -> None:
self.shutdown()

def is_websocket(connection):
"""
return True if the connection is a websocket

"""
upgrade_header = connection.__dict__.get('request').headers.get('Upgrade', '')
connection_header = connection.__dict__.get('request').headers.get('Connection', '')
return upgrade_header.lower() == 'websocket' and connection_header.lower() == 'upgrade'

def serve(
handler: Callable[[ServerConnection], None],
Expand Down Expand Up @@ -508,7 +516,8 @@ def protocol_select_subprotocol(
return

try:
handler(connection)
if is_websocket(connection):
handler(connection)
except Exception:
protocol.logger.error("connection handler failed", exc_info=True)
connection.close(CloseCode.INTERNAL_ERROR)
Expand Down