From e535daf8c5778dd05ab75bcc0ba9e1959b1a4ce5 Mon Sep 17 00:00:00 2001 From: walkor Date: Mon, 19 Sep 2022 12:08:49 +0800 Subject: [PATCH] Support body for GET OPTIONS HEAD DELETE --- Protocols/Http.php | 46 +++++++++++++++++--------------------- Protocols/Http/Request.php | 3 +++ 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/Protocols/Http.php b/Protocols/Http.php index 2cb87f093..543670dee 100644 --- a/Protocols/Http.php +++ b/Protocols/Http.php @@ -94,60 +94,56 @@ public static function enableCache($value) */ public static function input($recv_buffer, TcpConnection $connection) { - static $input = array(); + static $input = []; if (!isset($recv_buffer[512]) && isset($input[$recv_buffer])) { return $input[$recv_buffer]; } $crlf_pos = \strpos($recv_buffer, "\r\n\r\n"); if (false === $crlf_pos) { // Judge whether the package length exceeds the limit. - if ($recv_len = \strlen($recv_buffer) >= 16384) { + if (\strlen($recv_buffer) >= 16384) { $connection->close("HTTP/1.1 413 Request Entity Too Large\r\n\r\n", true); return 0; } return 0; } - $head_len = $crlf_pos + 4; + $length = $crlf_pos + 4; $method = \strstr($recv_buffer, ' ', true); - if ($method === 'GET' || $method === 'OPTIONS' || $method === 'HEAD' || $method === 'DELETE') { - if (!isset($recv_buffer[512])) { - $input[$recv_buffer] = $head_len; - if (\count($input) > 512) { - unset($input[key($input)]); - } - } - return $head_len; - } else if ($method !== 'POST' && $method !== 'PUT' && $method !== 'PATCH') { + if (!\in_array($method, ['GET', 'POST', 'OPTIONS', 'HEAD', 'DELETE', 'PUT', 'PATCH'])) { $connection->close("HTTP/1.1 400 Bad Request\r\n\r\n", true); return 0; } $header = \substr($recv_buffer, 0, $crlf_pos); - $length = false; + $has_content_length = false; if ($pos = \strpos($header, "\r\nContent-Length: ")) { - $length = $head_len + (int)\substr($header, $pos + 18, 10); + $length = $length + (int)\substr($header, $pos + 18, 10); + $has_content_length = true; } else if (\preg_match("/\r\ncontent-length: ?(\d+)/i", $header, $match)) { - $length = $head_len + $match[1]; + $length = $length + $match[1]; + $has_content_length = true; } - if ($length !== false) { - if (!isset($recv_buffer[512])) { - $input[$recv_buffer] = $length; - if (\count($input) > 512) { - unset($input[key($input)]); - } - } + if ($has_content_length) { if ($length > $connection->maxPackageSize) { $connection->close("HTTP/1.1 413 Request Entity Too Large\r\n\r\n", true); return 0; } - return $length; + } elseif (\in_array($method, ['POST', 'PUT', 'PATCH'])) { + $connection->close("HTTP/1.1 400 Bad Request\r\n\r\n", true); + return 0; + } + + if (!isset($recv_buffer[512])) { + $input[$recv_buffer] = $length; + if (\count($input) > 512) { + unset($input[key($input)]); + } } - $connection->close("HTTP/1.1 400 Bad Request\r\n\r\n", true); - return 0; + return $length; } /** diff --git a/Protocols/Http/Request.php b/Protocols/Http/Request.php index 47f7f0a0c..39e999875 100644 --- a/Protocols/Http/Request.php +++ b/Protocols/Http/Request.php @@ -520,6 +520,9 @@ protected function parseUploadFile($boundary, $section_start_offset, &$post_enco { $file = []; $boundary = "\r\n$boundary"; + if (\strlen($this->_buffer) < $section_start_offset) { + return 0; + } $section_end_offset = \strpos($this->_buffer, $boundary, $section_start_offset); if (!$section_end_offset) { return 0;