File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -337,6 +337,13 @@ def parse_request(self):
337337 HTTPStatus .BAD_REQUEST ,
338338 "Line too long" )
339339 return False
340+ except http .client .HTTPException as err :
341+ self .send_error (
342+ HTTPStatus .REQUEST_HEADER_FIELDS_TOO_LARGE ,
343+ "Too many headers" ,
344+ str (err )
345+ )
346+ return False
340347
341348 conntype = self .headers .get ('Connection' , "" )
342349 if conntype .lower () == 'close' :
Original file line number Diff line number Diff line change @@ -858,6 +858,13 @@ def test_header_length(self):
858858 self .assertFalse (self .handler .get_called )
859859 self .assertEqual (self .handler .requestline , 'GET / HTTP/1.1' )
860860
861+ def test_too_many_headers (self ):
862+ result = self .send_typical_request (
863+ b'GET / HTTP/1.1\r \n ' + b'X-Foo: bar\r \n ' * 101 + b'\r \n ' )
864+ self .assertEqual (result [0 ], b'HTTP/1.1 431 Too many headers\r \n ' )
865+ self .assertFalse (self .handler .get_called )
866+ self .assertEqual (self .handler .requestline , 'GET / HTTP/1.1' )
867+
861868 def test_close_connection (self ):
862869 # handle_one_request() should be repeatedly called until
863870 # it sets close_connection
Original file line number Diff line number Diff line change @@ -99,6 +99,10 @@ Core and Builtins
9999Library
100100-------
101101
102+ - Issue #26586: In http.server, respond with "413 Request header fields too
103+ large" if there are too many header fields to parse, rather than killing
104+ the connection and raising an unhandled exception. Patch by Xiang Zhang.
105+
102106- Issue #22854: Change BufferedReader.writable() and
103107 BufferedWriter.readable() to always return False.
104108
You can’t perform that action at this time.
0 commit comments