Skip to content

Commit df67931

Browse files
committed
Code optimization
1 parent b2e9950 commit df67931

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/Protocols/Http.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use function is_array;
3333
use function is_object;
3434
use function preg_match;
35+
use function str_starts_with;
3536
use function strlen;
3637
use function strpos;
3738
use function strstr;
@@ -91,13 +92,21 @@ public static function input(string $buffer, TcpConnection $connection): int
9192
}
9293

9394
$length = $crlfPos + 4;
94-
$method = strstr($buffer, ' ', true);
95-
if (!in_array($method, ['GET', 'POST', 'OPTIONS', 'HEAD', 'DELETE', 'PUT', 'PATCH'])) {
95+
$header = substr($buffer, 0, $crlfPos);
96+
97+
if (
98+
!str_starts_with($header, 'GET ') &&
99+
!str_starts_with($header, 'POST ') &&
100+
!str_starts_with($header, 'OPTIONS ') &&
101+
!str_starts_with($header, 'HEAD ') &&
102+
!str_starts_with($header, 'DELETE ') &&
103+
!str_starts_with($header, 'PUT ') &&
104+
!str_starts_with($header, 'PATCH ')
105+
) {
96106
$connection->close("HTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\n\r\n", true);
97107
return 0;
98108
}
99109

100-
$header = substr($buffer, 0, $crlfPos);
101110
if (preg_match('/\b(?:Transfer-Encoding\b.*)|(?:Content-Length:\s*(\d+)(?!.*\bTransfer-Encoding\b))/is', $header, $matches)) {
102111
if (!isset($matches[1])) {
103112
$connection->close("HTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\n\r\n", true);

0 commit comments

Comments
 (0)