Skip to content

Commit 8f944c1

Browse files
committed
move the upgrade header value into a constant
1 parent 4abc452 commit 8f944c1

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/wsproto/handshake.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
# RFC6455, Section 4.2.1/6 - Reading the Client's Opening Handshake
3636
WEBSOCKET_VERSION = b"13"
3737

38+
# RFC6455, Section 4.2.1/3 - Value of the Upgrade header
39+
WEBSOCKET_UPGRADE = b"websocket"
40+
3841

3942
class H11Handshake:
4043
"""A Handshake implementation for HTTP/1.1 connections."""
@@ -233,9 +236,9 @@ def _process_connection_request( # noqa: MC0001
233236
raise RemoteProtocolError(
234237
"Missing header, 'Sec-WebSocket-Key'", event_hint=RejectConnection()
235238
)
236-
if upgrade.lower() != b"websocket":
239+
if upgrade.lower() != WEBSOCKET_UPGRADE:
237240
raise RemoteProtocolError(
238-
"Missing header, 'Upgrade: websocket'", event_hint=RejectConnection()
241+
f"Missing header, 'Upgrade: {WEBSOCKET_UPGRADE.encode()}'", event_hint=RejectConnection()
239242
)
240243
if host is None:
241244
raise RemoteProtocolError(
@@ -260,7 +263,7 @@ def _accept(self, event: AcceptConnection) -> bytes:
260263
accept_token = generate_accept_token(nonce)
261264

262265
headers = [
263-
(b"Upgrade", b"websocket"),
266+
(b"Upgrade", WEBSOCKET_UPGRADE),
264267
(b"Connection", b"Upgrade"),
265268
(b"Sec-WebSocket-Accept", accept_token),
266269
]
@@ -327,7 +330,7 @@ def _initiate_connection(self, request: Request) -> bytes:
327330

328331
headers = [
329332
(b"Host", request.host.encode("idna")),
330-
(b"Upgrade", b"websocket"),
333+
(b"Upgrade", WEBSOCKET_UPGRADE),
331334
(b"Connection", b"Upgrade"),
332335
(b"Sec-WebSocket-Key", self._nonce),
333336
(b"Sec-WebSocket-Version", WEBSOCKET_VERSION),
@@ -402,9 +405,9 @@ def _establish_client_connection(
402405
raise RemoteProtocolError(
403406
"Missing header, 'Connection: Upgrade'", event_hint=RejectConnection()
404407
)
405-
if upgrade.lower() != b"websocket":
408+
if upgrade.lower() != WEBSOCKET_UPGRADE:
406409
raise RemoteProtocolError(
407-
"Missing header, 'Upgrade: websocket'", event_hint=RejectConnection()
410+
f"Missing header, 'Upgrade: {WEBSOCKET_UPGRADE.encode()}'", event_hint=RejectConnection()
408411
)
409412
accept_token = generate_accept_token(self._nonce)
410413
if accept != accept_token:

0 commit comments

Comments
 (0)