35
35
# RFC6455, Section 4.2.1/6 - Reading the Client's Opening Handshake
36
36
WEBSOCKET_VERSION = b"13"
37
37
38
+ # RFC6455, Section 4.2.1/3 - Value of the Upgrade header
39
+ WEBSOCKET_UPGRADE = b"websocket"
40
+
38
41
39
42
class H11Handshake :
40
43
"""A Handshake implementation for HTTP/1.1 connections."""
@@ -233,9 +236,9 @@ def _process_connection_request( # noqa: MC0001
233
236
raise RemoteProtocolError (
234
237
"Missing header, 'Sec-WebSocket-Key'" , event_hint = RejectConnection ()
235
238
)
236
- if upgrade .lower () != b"websocket" :
239
+ if upgrade .lower () != WEBSOCKET_UPGRADE :
237
240
raise RemoteProtocolError (
238
- "Missing header, 'Upgrade: websocket '" , event_hint = RejectConnection ()
241
+ f "Missing header, 'Upgrade: { WEBSOCKET_UPGRADE . encode () } '" , event_hint = RejectConnection ()
239
242
)
240
243
if host is None :
241
244
raise RemoteProtocolError (
@@ -260,7 +263,7 @@ def _accept(self, event: AcceptConnection) -> bytes:
260
263
accept_token = generate_accept_token (nonce )
261
264
262
265
headers = [
263
- (b"Upgrade" , b"websocket" ),
266
+ (b"Upgrade" , WEBSOCKET_UPGRADE ),
264
267
(b"Connection" , b"Upgrade" ),
265
268
(b"Sec-WebSocket-Accept" , accept_token ),
266
269
]
@@ -327,7 +330,7 @@ def _initiate_connection(self, request: Request) -> bytes:
327
330
328
331
headers = [
329
332
(b"Host" , request .host .encode ("idna" )),
330
- (b"Upgrade" , b"websocket" ),
333
+ (b"Upgrade" , WEBSOCKET_UPGRADE ),
331
334
(b"Connection" , b"Upgrade" ),
332
335
(b"Sec-WebSocket-Key" , self ._nonce ),
333
336
(b"Sec-WebSocket-Version" , WEBSOCKET_VERSION ),
@@ -402,9 +405,9 @@ def _establish_client_connection(
402
405
raise RemoteProtocolError (
403
406
"Missing header, 'Connection: Upgrade'" , event_hint = RejectConnection ()
404
407
)
405
- if upgrade .lower () != b"websocket" :
408
+ if upgrade .lower () != WEBSOCKET_UPGRADE :
406
409
raise RemoteProtocolError (
407
- "Missing header, 'Upgrade: websocket '" , event_hint = RejectConnection ()
410
+ f "Missing header, 'Upgrade: { WEBSOCKET_UPGRADE . encode () } '" , event_hint = RejectConnection ()
408
411
)
409
412
accept_token = generate_accept_token (self ._nonce )
410
413
if accept != accept_token :
0 commit comments