Skip to content

Commit

Permalink
Support body for GET OPTIONS HEAD DELETE
Browse files Browse the repository at this point in the history
  • Loading branch information
walkor committed Sep 19, 2022
1 parent 7265c08 commit e535daf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
46 changes: 21 additions & 25 deletions Protocols/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions Protocols/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e535daf

Please sign in to comment.