@@ -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
373363def _debug_warning_nonencoded_output ():
0 commit comments