From b2e99500670ad65eed85681381bf17b06242cc0b Mon Sep 17 00:00:00 2001 From: walkor Date: Sat, 28 Dec 2024 16:30:49 +0800 Subject: [PATCH] Code optimization --- src/Protocols/Http.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Protocols/Http.php b/src/Protocols/Http.php index c5f8b1d4..490ccfef 100644 --- a/src/Protocols/Http.php +++ b/src/Protocols/Http.php @@ -98,8 +98,12 @@ public static function input(string $buffer, TcpConnection $connection): int } $header = substr($buffer, 0, $crlfPos); - if (preg_match("/\r\ncontent-length: ?(\d+)/i", $header, $match)) { - $length += (int)$match[1]; + if (preg_match('/\b(?:Transfer-Encoding\b.*)|(?:Content-Length:\s*(\d+)(?!.*\bTransfer-Encoding\b))/is', $header, $matches)) { + if (!isset($matches[1])) { + $connection->close("HTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\n\r\n", true); + return 0; + } + $length += (int)$matches[1]; } if ($length > $connection->maxPackageSize) {