Skip to content

Commit

Permalink
Optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
walkor committed Dec 7, 2024
1 parent ed4b431 commit 7bd003b
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/Protocols/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
Expand Down

0 comments on commit 7bd003b

Please sign in to comment.