Skip to content

Commit f53636d

Browse files
authored
[WebServer] Fix routing (#968)
* Fix web server routing * Fix mypy
1 parent 010e8f8 commit f53636d

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

proxy/http/handler.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,9 @@ def _parse_first_request(self, data: memoryview) -> bool:
304304
# memoryview compliant
305305
try:
306306
self.request.parse(data.tobytes())
307-
except Exception as exc:
308-
logger.exception('Error parsing the request', exc_info=exc)
307+
except Exception:
309308
raise HttpProtocolException(
310-
'Error when parsing request: %s' % data.tobytes().decode('utf-8', errors='ignore'),
309+
'Error when parsing request: %r' % data.tobytes(),
311310
)
312311
if not self.request.is_complete:
313312
return False

proxy/http/server/web.py

+13-15
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,15 @@ def switch_to_websocket(self) -> None:
152152

153153
def on_request_complete(self) -> Union[socket.socket, bool]:
154154
path = self.request.path or b'/'
155-
# Try route
156155
teardown = self._try_route(path)
157-
if teardown:
156+
# Try route signaled to teardown
157+
# or if it did find a valid route
158+
if teardown or self.route is not None:
158159
return teardown
159160
# No-route found, try static serving if enabled
160-
teardown = self._try_static_file(path)
161-
if teardown:
162-
return teardown
161+
if self.flags.enable_static_server:
162+
self._try_static_or_404(path)
163+
return True
163164
# Catch all unhandled web server requests, return 404
164165
self.client.queue(NOT_FOUND_RESPONSE_PKT)
165166
return True
@@ -299,13 +300,10 @@ def _try_route(self, path: bytes) -> bool:
299300
return True
300301
return False
301302

302-
def _try_static_file(self, path: bytes) -> bool:
303-
if self.flags.enable_static_server:
304-
path = text_(path).split('?', 1)[0]
305-
self.client.queue(
306-
self.read_and_build_static_file_response(
307-
self.flags.static_server_dir + path,
308-
),
309-
)
310-
return True
311-
return False
303+
def _try_static_or_404(self, path: bytes) -> None:
304+
path = text_(path).split('?', 1)[0]
305+
self.client.queue(
306+
self.read_and_build_static_file_response(
307+
self.flags.static_server_dir + path,
308+
),
309+
)

0 commit comments

Comments
 (0)