Skip to content

Commit 60a472e

Browse files
committed
Added option to construct Headers from str and refactor of parsing request header part
1 parent 6031bec commit 60a472e

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

adafruit_httpserver/headers.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class Headers:
2323
2424
Examples::
2525
26+
headers = Headers("Content-Type: text/html\\r\\nContent-Length: 1024\\r\\n")
27+
# or
2628
headers = Headers({"Content-Type": "text/html", "Content-Length": "1024"})
2729
2830
len(headers)
@@ -47,8 +49,15 @@ class Headers:
4749

4850
_storage: Dict[str, Tuple[str, str]]
4951

50-
def __init__(self, headers: Dict[str, str] = None) -> None:
51-
headers = headers or {}
52+
def __init__(self, headers: Union[str, Dict[str, str]] = None) -> None:
53+
if isinstance(headers, str):
54+
headers = {
55+
name: value
56+
for header_line in headers.strip().splitlines()
57+
for name, value in [header_line.split(": ", 1)]
58+
}
59+
else:
60+
headers = headers or {}
5261

5362
self._storage = {key.lower(): [key, value] for key, value in headers.items()}
5463

adafruit_httpserver/request.py

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -252,16 +252,14 @@ def __init__(
252252
if raw_request is None:
253253
raise ValueError("raw_request cannot be None")
254254

255-
header_bytes = self._raw_header_bytes
256-
257255
try:
258256
(
259257
self.method,
260258
self.path,
261259
self.query_params,
262260
self.http_version,
263-
) = self._parse_start_line(header_bytes)
264-
self.headers = self._parse_headers(header_bytes)
261+
self.headers,
262+
) = self._parse_request_header(self._raw_header_bytes)
265263
except Exception as error:
266264
raise ValueError("Unparseable raw_request: ", raw_request) from error
267265

@@ -340,34 +338,26 @@ def _raw_body_bytes(self) -> bytes:
340338
return self.raw_request[empty_line_index + 4 :]
341339

342340
@staticmethod
343-
def _parse_start_line(header_bytes: bytes) -> Tuple[str, str, QueryParams, str]:
341+
def _parse_request_header(
342+
header_bytes: bytes,
343+
) -> Tuple[str, str, QueryParams, str, Headers]:
344344
"""Parse HTTP Start line to method, path, query_params and http_version."""
345345

346-
start_line = header_bytes.decode("utf-8").splitlines()[0]
346+
start_line, headers_string = (
347+
header_bytes.decode("utf-8").strip().split("\r\n", 1)
348+
)
347349

348-
method, path, http_version = start_line.split()
350+
method, path, http_version = start_line.strip().split()
349351

350352
if "?" not in path:
351353
path += "?"
352354

353355
path, query_string = path.split("?", 1)
354356

355357
query_params = QueryParams(query_string)
358+
headers = Headers(headers_string)
356359

357-
return method, path, query_params, http_version
358-
359-
@staticmethod
360-
def _parse_headers(header_bytes: bytes) -> Headers:
361-
"""Parse HTTP headers from raw request."""
362-
header_lines = header_bytes.decode("utf-8").splitlines()[1:]
363-
364-
return Headers(
365-
{
366-
name: value
367-
for header_line in header_lines
368-
for name, value in [header_line.split(": ", 1)]
369-
}
370-
)
360+
return method, path, query_params, http_version, headers
371361

372362

373363
def _debug_warning_nonencoded_output():

0 commit comments

Comments
 (0)