Skip to content

[WebServer] Fix routing #968

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions proxy/http/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,9 @@ def _parse_first_request(self, data: memoryview) -> bool:
# memoryview compliant
try:
self.request.parse(data.tobytes())
except Exception as exc:
logger.exception('Error parsing the request', exc_info=exc)
except Exception:
raise HttpProtocolException(
'Error when parsing request: %s' % data.tobytes().decode('utf-8', errors='ignore'),
'Error when parsing request: %r' % data.tobytes(),
)
if not self.request.is_complete:
return False
Expand Down
28 changes: 13 additions & 15 deletions proxy/http/server/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,15 @@ def switch_to_websocket(self) -> None:

def on_request_complete(self) -> Union[socket.socket, bool]:
path = self.request.path or b'/'
# Try route
teardown = self._try_route(path)
if teardown:
# Try route signaled to teardown
# or if it did find a valid route
if teardown or self.route is not None:
return teardown
# No-route found, try static serving if enabled
teardown = self._try_static_file(path)
if teardown:
return teardown
if self.flags.enable_static_server:
self._try_static_or_404(path)
return True
# Catch all unhandled web server requests, return 404
self.client.queue(NOT_FOUND_RESPONSE_PKT)
return True
Expand Down Expand Up @@ -299,13 +300,10 @@ def _try_route(self, path: bytes) -> bool:
return True
return False

def _try_static_file(self, path: bytes) -> bool:
if self.flags.enable_static_server:
path = text_(path).split('?', 1)[0]
self.client.queue(
self.read_and_build_static_file_response(
self.flags.static_server_dir + path,
),
)
return True
return False
def _try_static_or_404(self, path: bytes) -> None:
path = text_(path).split('?', 1)[0]
self.client.queue(
self.read_and_build_static_file_response(
self.flags.static_server_dir + path,
),
)