Skip to content

Commit

Permalink
Code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
walkor committed Dec 30, 2024
1 parent b2e9950 commit df67931
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/Protocols/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use function is_array;
use function is_object;
use function preg_match;
use function str_starts_with;
use function strlen;
use function strpos;
use function strstr;
Expand Down Expand Up @@ -91,13 +92,21 @@ public static function input(string $buffer, TcpConnection $connection): int
}

$length = $crlfPos + 4;
$method = strstr($buffer, ' ', true);
if (!in_array($method, ['GET', 'POST', 'OPTIONS', 'HEAD', 'DELETE', 'PUT', 'PATCH'])) {
$header = substr($buffer, 0, $crlfPos);

if (
!str_starts_with($header, 'GET ') &&
!str_starts_with($header, 'POST ') &&
!str_starts_with($header, 'OPTIONS ') &&
!str_starts_with($header, 'HEAD ') &&
!str_starts_with($header, 'DELETE ') &&
!str_starts_with($header, 'PUT ') &&
!str_starts_with($header, '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 (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);
Expand Down

0 comments on commit df67931

Please sign in to comment.