Skip to content

Change request handling to use split instead of regular expressions. #3

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 2 commits into from
May 18, 2022
Merged
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
15 changes: 6 additions & 9 deletions adafruit_httpserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

from errno import EAGAIN, ECONNRESET
import os
import re

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_HTTPServer.git"
Expand Down Expand Up @@ -60,8 +59,6 @@ def __str__(self):


class _HTTPRequest:
_REQUEST_RE = re.compile(r"(\S+)\s+(\S+)\s")

def __init__(
self, path: str = "", method: str = "", raw_request: bytes = None
) -> None:
Expand All @@ -70,12 +67,12 @@ def __init__(
self.method = method
else:
# Parse request data from raw request
match = self._REQUEST_RE.match(raw_request.decode("utf8"))
if match:
self.path = match.group(2)
self.method = match.group(1)
else:
raise ValueError("Unparseable raw_request:", raw_request)
request_text = raw_request.decode("utf8")
first_line = request_text[: request_text.find("\n")]
try:
(self.method, self.path, _httpversion) = first_line.split()
except ValueError as exc:
raise ValueError("Unparseable raw_request: ", raw_request) from exc

def __hash__(self) -> int:
return hash(self.method) ^ hash(self.path)
Expand Down