swoole-src icon indicating copy to clipboard operation
swoole-src copied to clipboard

I cannot catch errors raised by swoole before parsing HTTP request

Open godfreybwaira opened this issue 6 months ago • 2 comments

I want to check the maximum request size so that when a user upload a file larger that package_max_length I can reject the request. But swoole does not allow me to check for this error, instead it shows warning [2025-05-07 14:16:32 *15066.2] WARNING Port_onRead_http() (ERRNO 7102): Request Entity Too Large: header-length (290) + content-length (1575386) is greater than the package_max_length(1048576) from session#1 on 0.0.0.0:8008

How Do I handle this kind of error programmatically so that I can send 413 status code to a client?.

I suggest you create

$server->on('error', function(Request $req, Response $res){
//handle payload errors and other errors gracefully
});

Thanks in advance

godfreybwaira avatar May 07 '25 12:05 godfreybwaira

The error indicates that the entire packet exceeds the maximum allowed size defined by package_max_length. To resolve this, you must either: Increase package_max_length to accommodate larger packets, or Implement chunked/streaming transfer (resumable transmission) to handle the data in smaller segments.

NathanFreeman avatar May 08 '25 08:05 NathanFreeman

If this error can be caught, the remaining business logic may lead to unpredictable/uncontrollable issues.

NathanFreeman avatar May 08 '25 09:05 NathanFreeman