@@ -302,6 +302,7 @@ def parse_request(self):
302302 error response has already been sent back.
303303
304304 """
305+ is_http_0_9 = False
305306 self .command = None # set in case of error on the first line
306307 self .request_version = version = self .default_request_version
307308 self .close_connection = True
@@ -359,6 +360,7 @@ def parse_request(self):
359360 HTTPStatus .BAD_REQUEST ,
360361 "Bad HTTP/0.9 request type (%r)" % command )
361362 return False
363+ is_http_0_9 = True
362364 self .command , self .path = command , path
363365
364366 # gh-87389: The purpose of replacing '//' with '/' is to protect
@@ -369,22 +371,26 @@ def parse_request(self):
369371 self .path = '/' + self .path .lstrip ('/' ) # Reduce to a single /
370372
371373 # Examine the headers and look for a Connection directive.
372- try :
373- self .headers = http .client .parse_headers (self .rfile ,
374- _class = self .MessageClass )
375- except http .client .LineTooLong as err :
376- self .send_error (
377- HTTPStatus .REQUEST_HEADER_FIELDS_TOO_LARGE ,
378- "Line too long" ,
379- str (err ))
380- return False
381- except http .client .HTTPException as err :
382- self .send_error (
383- HTTPStatus .REQUEST_HEADER_FIELDS_TOO_LARGE ,
384- "Too many headers" ,
385- str (err )
386- )
387- return False
374+ # For HTTP/0.9, headers are not expected at all.
375+ if is_http_0_9 :
376+ self .headers = {}
377+ else :
378+ try :
379+ self .headers = http .client .parse_headers (self .rfile ,
380+ _class = self .MessageClass )
381+ except http .client .LineTooLong as err :
382+ self .send_error (
383+ HTTPStatus .REQUEST_HEADER_FIELDS_TOO_LARGE ,
384+ "Line too long" ,
385+ str (err ))
386+ return False
387+ except http .client .HTTPException as err :
388+ self .send_error (
389+ HTTPStatus .REQUEST_HEADER_FIELDS_TOO_LARGE ,
390+ "Too many headers" ,
391+ str (err )
392+ )
393+ return False
388394
389395 conntype = self .headers .get ('Connection' , "" )
390396 if conntype .lower () == 'close' :
0 commit comments