File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -338,6 +338,13 @@ def parse_request(self):
338338 HTTPStatus .BAD_REQUEST ,
339339 "Line too long" )
340340 return False
341+ except http .client .HTTPException as err :
342+ self .send_error (
343+ HTTPStatus .REQUEST_HEADER_FIELDS_TOO_LARGE ,
344+ "Too many headers" ,
345+ str (err )
346+ )
347+ return False
341348
342349 conntype = self .headers .get ('Connection' , "" )
343350 if conntype .lower () == 'close' :
Original file line number Diff line number Diff line change @@ -859,6 +859,13 @@ def test_header_length(self):
859859 self .assertFalse (self .handler .get_called )
860860 self .assertEqual (self .handler .requestline , 'GET / HTTP/1.1' )
861861
862+ def test_too_many_headers (self ):
863+ result = self .send_typical_request (
864+ b'GET / HTTP/1.1\r \n ' + b'X-Foo: bar\r \n ' * 101 + b'\r \n ' )
865+ self .assertEqual (result [0 ], b'HTTP/1.1 431 Too many headers\r \n ' )
866+ self .assertFalse (self .handler .get_called )
867+ self .assertEqual (self .handler .requestline , 'GET / HTTP/1.1' )
868+
862869 def test_close_connection (self ):
863870 # handle_one_request() should be repeatedly called until
864871 # it sets close_connection
Original file line number Diff line number Diff line change @@ -237,6 +237,10 @@ Core and Builtins
237237Library
238238-------
239239
240+ - Issue #26586: In http.server, respond with "413 Request header fields too
241+ large" if there are too many header fields to parse, rather than killing
242+ the connection and raising an unhandled exception. Patch by Xiang Zhang.
243+
240244- Issue #26676: Added missing XMLPullParser to ElementTree.__all__.
241245
242246- Issue #22854: Change BufferedReader.writable() and
You can’t perform that action at this time.
0 commit comments