Skip to content

Commit 00758ea

Browse files
committed
Switch to WS
1 parent 474cce1 commit 00758ea

File tree

2 files changed

+20
-24
lines changed

2 files changed

+20
-24
lines changed

proxy/http/parser/parser.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,13 @@ def is_http_1_1_keep_alive(self) -> bool:
172172
self.header(b'Connection').lower() == b'keep-alive'
173173
)
174174

175+
@property
176+
def is_websocket_upgrade(self) -> bool:
177+
return self.has_header(b'connection') and \
178+
self.header(b'connection').lower() == b'upgrade' and \
179+
self.has_header(b'upgrade') and \
180+
self.header(b'upgrade').lower() == b'websocket'
181+
175182
@property
176183
def is_connection_upgrade(self) -> bool:
177184
"""Returns true for websocket upgrade requests."""

proxy/http/server/web.py

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from ..websocket import WebsocketFrame, websocketOpcodes
2929
from ..parser import HttpParser, httpParserTypes
3030
from ..protocols import httpProtocols
31-
from ..responses import NOT_FOUND_RESPONSE_PKT, NOT_IMPLEMENTED_RESPONSE_PKT, okResponse
31+
from ..responses import NOT_FOUND_RESPONSE_PKT, okResponse
3232

3333
from .plugin import HttpWebServerBasePlugin
3434
from .protocols import httpProtocolTypes
@@ -138,25 +138,17 @@ def read_and_build_static_file_response(path: str) -> memoryview:
138138
except FileNotFoundError:
139139
return NOT_FOUND_RESPONSE_PKT
140140

141-
def try_upgrade(self) -> bool:
142-
if self.request.has_header(b'connection') and \
143-
self.request.header(b'connection').lower() == b'upgrade':
144-
if self.request.has_header(b'upgrade') and \
145-
self.request.header(b'upgrade').lower() == b'websocket':
146-
self.client.queue(
147-
memoryview(
148-
build_websocket_handshake_response(
149-
WebsocketFrame.key_to_accept(
150-
self.request.header(b'Sec-WebSocket-Key'),
151-
),
152-
),
141+
def switch_to_websocket(self) -> None:
142+
self.client.queue(
143+
memoryview(
144+
build_websocket_handshake_response(
145+
WebsocketFrame.key_to_accept(
146+
self.request.header(b'Sec-WebSocket-Key'),
153147
),
154-
)
155-
self.switched_protocol = httpProtocolTypes.WEBSOCKET
156-
else:
157-
self.client.queue(NOT_IMPLEMENTED_RESPONSE_PKT)
158-
return True
159-
return False
148+
),
149+
),
150+
)
151+
self.switched_protocol = httpProtocolTypes.WEBSOCKET
160152

161153
def on_request_complete(self) -> Union[socket.socket, bool]:
162154
path = self.request.path or b'/'
@@ -178,11 +170,8 @@ def on_request_complete(self) -> Union[socket.socket, bool]:
178170
if route.match(text_(path)):
179171
self.route = self.routes[httpProtocolTypes.WEBSOCKET][route]
180172
# Connection upgrade
181-
teardown = self.try_upgrade()
182-
if teardown:
183-
return True
184-
# For upgraded connections, nothing more to do
185-
if self.switched_protocol:
173+
if self.request.is_websocket_upgrade:
174+
self.switch_to_websocket()
186175
# Invoke plugin.on_websocket_open
187176
assert self.route
188177
self.route.on_websocket_open()

0 commit comments

Comments
 (0)