@@ -265,6 +265,22 @@ def _generate_error_response(
265265 status_code = 200 ,
266266 )
267267
268+ def _check_content_length (self , request : Request ) -> bool :
269+ """Checks if the request content length exceeds the maximum allowed size.
270+
271+ Args:
272+ request: The incoming Starlette Request object.
273+
274+ Returns:
275+ True if the content length is within the allowed limit, False otherwise.
276+ """
277+ if not self ._disable_content_length_check :
278+ with contextlib .suppress (Exception ):
279+ content_length = int (request .headers .get ('content-length' , '0' ))
280+ if content_length and content_length > MAX_CONTENT_LENGTH :
281+ return False
282+ return True
283+
268284 async def _handle_requests (self , request : Request ) -> Response : # noqa: PLR0911
269285 """Handles incoming POST requests to the main A2A endpoint.
270286
@@ -297,20 +313,13 @@ async def _handle_requests(self, request: Request) -> Response: # noqa: PLR0911
297313 request_id = None
298314 # If content length check is not disabled,
299315 # treat very large payloads as invalid request (-32600) before routing
300- if not self ._disable_content_length_check :
301- with contextlib .suppress (Exception ):
302- content_length = int (
303- request .headers .get ('content-length' , '0' )
304- )
305- if content_length and content_length > MAX_CONTENT_LENGTH :
306- return self ._generate_error_response (
307- request_id ,
308- A2AError (
309- root = InvalidRequestError (
310- message = 'Payload too large'
311- )
312- ),
313- )
316+ if not self ._check_content_length (request ):
317+ return self ._generate_error_response (
318+ request_id ,
319+ A2AError (
320+ root = InvalidRequestError (message = 'Payload too large' )
321+ ),
322+ )
314323 logger .debug ('Request body: %s' , body )
315324 # 1) Validate base JSON-RPC structure only (-32600 on failure)
316325 try :
0 commit comments