@@ -252,16 +252,14 @@ def __init__(
252
252
if raw_request is None :
253
253
raise ValueError ("raw_request cannot be None" )
254
254
255
- header_bytes = self ._raw_header_bytes
256
-
257
255
try :
258
256
(
259
257
self .method ,
260
258
self .path ,
261
259
self .query_params ,
262
260
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 )
265
263
except Exception as error :
266
264
raise ValueError ("Unparseable raw_request: " , raw_request ) from error
267
265
@@ -340,34 +338,26 @@ def _raw_body_bytes(self) -> bytes:
340
338
return self .raw_request [empty_line_index + 4 :]
341
339
342
340
@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 ]:
344
344
"""Parse HTTP Start line to method, path, query_params and http_version."""
345
345
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
+ )
347
349
348
- method , path , http_version = start_line .split ()
350
+ method , path , http_version = start_line .strip (). split ()
349
351
350
352
if "?" not in path :
351
353
path += "?"
352
354
353
355
path , query_string = path .split ("?" , 1 )
354
356
355
357
query_params = QueryParams (query_string )
358
+ headers = Headers (headers_string )
356
359
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
371
361
372
362
373
363
def _debug_warning_nonencoded_output ():
0 commit comments