Skip to content

Commit 4a4cdfd

Browse files
committed
Removed body attribute and added as property
1 parent 0480546 commit 4a4cdfd

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

adafruit_httpserver/request.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
pass
1414

1515

16-
class HTTPRequest: # pylint: disable=too-few-public-methods
16+
class HTTPRequest:
1717
"""
1818
Incoming request, constructed from raw incoming bytes.
1919
It is passed as first argument to route handlers.
@@ -42,9 +42,6 @@ class HTTPRequest: # pylint: disable=too-few-public-methods
4242
headers: Dict[str, str]
4343
"""Headers from the request."""
4444

45-
body: bytes
46-
"""Body of the request, as bytes."""
47-
4845
raw_request: bytes
4946
"""Raw bytes passed to the constructor."""
5047

@@ -54,10 +51,7 @@ def __init__(self, raw_request: bytes = None) -> None:
5451
if raw_request is None:
5552
raise ValueError("raw_request cannot be None")
5653

57-
empty_line_index = raw_request.find(b"\r\n\r\n")
58-
59-
header_bytes = raw_request[:empty_line_index]
60-
body_bytes = raw_request[empty_line_index + 4 :]
54+
header_bytes = self.header_body_bytes[0]
6155

6256
try:
6357
(
@@ -67,10 +61,28 @@ def __init__(self, raw_request: bytes = None) -> None:
6761
self.http_version,
6862
) = self._parse_start_line(header_bytes)
6963
self.headers = self._parse_headers(header_bytes)
70-
self.body = body_bytes
7164
except Exception as error:
7265
raise ValueError("Unparseable raw_request: ", raw_request) from error
7366

67+
@property
68+
def body(self) -> bytes:
69+
"""Body of the request, as bytes."""
70+
return self.header_body_bytes[1]
71+
72+
@body.setter
73+
def body(self, body: bytes) -> None:
74+
self.raw_request = self.header_body_bytes[0] + b"\r\n\r\n" + body
75+
76+
@property
77+
def header_body_bytes(self) -> Tuple[bytes, bytes]:
78+
"""Return tuple of header and body bytes."""
79+
80+
empty_line_index = self.raw_request.find(b"\r\n\r\n")
81+
header_bytes = self.raw_request[:empty_line_index]
82+
body_bytes = self.raw_request[empty_line_index + 4 :]
83+
84+
return header_bytes, body_bytes
85+
7486
@staticmethod
7587
def _parse_start_line(header_bytes: bytes) -> Tuple[str, str, Dict[str, str], str]:
7688
"""Parse HTTP Start line to method, path, query_params and http_version."""

0 commit comments

Comments
 (0)