Skip to content

Commit

Permalink
move the upgrade header value into a constant
Browse files Browse the repository at this point in the history
  • Loading branch information
palkeo committed Dec 17, 2023
1 parent 4abc452 commit ddb63ae
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/wsproto/handshake.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
# RFC6455, Section 4.2.1/6 - Reading the Client's Opening Handshake
WEBSOCKET_VERSION = b"13"

# RFC6455, Section 4.2.1/3 - Value of the Upgrade header
WEBSOCKET_UPGRADE = b"websocket"


class H11Handshake:
"""A Handshake implementation for HTTP/1.1 connections."""
Expand Down Expand Up @@ -233,9 +236,9 @@ def _process_connection_request( # noqa: MC0001
raise RemoteProtocolError(
"Missing header, 'Sec-WebSocket-Key'", event_hint=RejectConnection()
)
if upgrade.lower() != b"websocket":
if upgrade.lower() != WEBSOCKET_UPGRADE:
raise RemoteProtocolError(
"Missing header, 'Upgrade: websocket'", event_hint=RejectConnection()
f"Missing header, 'Upgrade: {WEBSOCKET_UPGRADE}'", event_hint=RejectConnection()
)
if host is None:
raise RemoteProtocolError(
Expand All @@ -260,7 +263,7 @@ def _accept(self, event: AcceptConnection) -> bytes:
accept_token = generate_accept_token(nonce)

headers = [
(b"Upgrade", b"websocket"),
(b"Upgrade", WEBSOCKET_UPGRADE),
(b"Connection", b"Upgrade"),
(b"Sec-WebSocket-Accept", accept_token),
]
Expand Down Expand Up @@ -327,7 +330,7 @@ def _initiate_connection(self, request: Request) -> bytes:

headers = [
(b"Host", request.host.encode("idna")),
(b"Upgrade", b"websocket"),
(b"Upgrade", WEBSOCKET_UPGRADE),
(b"Connection", b"Upgrade"),
(b"Sec-WebSocket-Key", self._nonce),
(b"Sec-WebSocket-Version", WEBSOCKET_VERSION),
Expand Down Expand Up @@ -402,9 +405,9 @@ def _establish_client_connection(
raise RemoteProtocolError(
"Missing header, 'Connection: Upgrade'", event_hint=RejectConnection()
)
if upgrade.lower() != b"websocket":
if upgrade.lower() != WEBSOCKET_UPGRADE:
raise RemoteProtocolError(
"Missing header, 'Upgrade: websocket'", event_hint=RejectConnection()
f"Missing header, 'Upgrade: {WEBSOCKET_UPGRADE}'", event_hint=RejectConnection()
)
accept_token = generate_accept_token(self._nonce)
if accept != accept_token:
Expand Down

0 comments on commit ddb63ae

Please sign in to comment.