13
13
pass
14
14
15
15
16
- class HTTPRequest : # pylint: disable=too-few-public-methods
16
+ class HTTPRequest :
17
17
"""
18
18
Incoming request, constructed from raw incoming bytes.
19
19
It is passed as first argument to route handlers.
@@ -42,9 +42,6 @@ class HTTPRequest: # pylint: disable=too-few-public-methods
42
42
headers : Dict [str , str ]
43
43
"""Headers from the request."""
44
44
45
- body : bytes
46
- """Body of the request, as bytes."""
47
-
48
45
raw_request : bytes
49
46
"""Raw bytes passed to the constructor."""
50
47
@@ -54,10 +51,7 @@ def __init__(self, raw_request: bytes = None) -> None:
54
51
if raw_request is None :
55
52
raise ValueError ("raw_request cannot be None" )
56
53
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 ]
61
55
62
56
try :
63
57
(
@@ -67,10 +61,28 @@ def __init__(self, raw_request: bytes = None) -> None:
67
61
self .http_version ,
68
62
) = self ._parse_start_line (header_bytes )
69
63
self .headers = self ._parse_headers (header_bytes )
70
- self .body = body_bytes
71
64
except Exception as error :
72
65
raise ValueError ("Unparseable raw_request: " , raw_request ) from error
73
66
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
+
74
86
@staticmethod
75
87
def _parse_start_line (header_bytes : bytes ) -> Tuple [str , str , Dict [str , str ], str ]:
76
88
"""Parse HTTP Start line to method, path, query_params and http_version."""
0 commit comments