Skip to content

Commit

Permalink
Optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
walkor committed Oct 29, 2024
1 parent d459794 commit 1a5f4ad
Showing 1 changed file with 2 additions and 20 deletions.
22 changes: 2 additions & 20 deletions src/Protocols/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,6 @@ 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 All @@ -97,20 +93,13 @@ public static function input(string $buffer, TcpConnection $connection): int
}

$length = $crlfPos + 4;
$firstLine = explode(" ", strstr($buffer, "\r\n", true), 3);

if (!in_array($firstLine[0], ['GET', 'POST', 'OPTIONS', 'HEAD', 'DELETE', 'PUT', 'PATCH'])) {
$method = strstr($buffer, ' ', true);
if (!in_array($method, ['GET', 'POST', 'OPTIONS', 'HEAD', 'DELETE', 'PUT', 'PATCH'])) {
$connection->close("HTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\n\r\n", true);
return 0;
}

$header = substr($buffer, 0, $crlfPos);

if (!str_contains($header, "\r\nHost: ") && $firstLine[2] === "HTTP/1.1") {
$connection->close("HTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\n\r\n", true);
return 0;
}

if ($pos = stripos($header, "\r\nContent-Length: ")) {
$length += (int)substr($header, $pos + 18, 10);
$hasContentLength = true;
Expand All @@ -130,13 +119,6 @@ public static function input(string $buffer, TcpConnection $connection): int
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 Down

0 comments on commit 1a5f4ad

Please sign in to comment.