From 7bd003bc37db42e4f2be739c215f5773eececa38 Mon Sep 17 00:00:00 2001 From: walkor Date: Sat, 7 Dec 2024 22:18:53 +0800 Subject: [PATCH] Optimization --- src/Protocols/Http.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Protocols/Http.php b/src/Protocols/Http.php index 4623f865..669a4127 100644 --- a/src/Protocols/Http.php +++ b/src/Protocols/Http.php @@ -20,6 +20,7 @@ use Workerman\Protocols\Http\Request; use Workerman\Protocols\Http\Response; use function clearstatcache; +use function count; use function explode; use function filesize; use function fopen; @@ -80,6 +81,10 @@ public static function requestClass(?string $className = null): string */ public static function input(string $buffer, TcpConnection $connection): int { + static $input = []; + if (isset($input[$buffer])) { + return $input[$buffer]; + } $crlfPos = strpos($buffer, "\r\n\r\n"); if (false === $crlfPos) { // Judge whether the package length exceeds the limit. @@ -115,6 +120,14 @@ public static function input(string $buffer, TcpConnection $connection): int $connection->close("HTTP/1.1 413 Payload Too Large\r\n\r\n", true); return 0; } + + if (!isset($buffer[TcpConnection::MAX_CACHE_STRING_LENGTH])) { + $input[$buffer] = $length; + if (count($input) > TcpConnection::MAX_CACHE_SIZE) { + unset($input[key($input)]); + } + } + return $length; } @@ -139,7 +152,7 @@ public static function decode(string $buffer, TcpConnection $connection): Reques $request = new static::$requestClass($buffer); if (!isset($buffer[TcpConnection::MAX_CACHE_STRING_LENGTH])) { $requests[$buffer] = $request; - if (\count($requests) > TcpConnection::MAX_CACHE_SIZE) { + if (count($requests) > TcpConnection::MAX_CACHE_SIZE) { unset($requests[key($requests)]); } $request = clone $request;