Skip to content

Commit b1eeec9

Browse files
authored
optimization
1 parent 6041872 commit b1eeec9

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed

Protocols/Http.php

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,6 @@ class Http
4646
*/
4747
protected static $_uploadTmpDir = '';
4848

49-
/**
50-
* Cache.
51-
*
52-
* @var array
53-
*/
54-
protected static $_cache = array();
55-
5649
/**
5750
* Open cache.
5851
*
@@ -107,12 +100,15 @@ public static function enableCache($value)
107100
*/
108101
public static function input($recv_buffer, TcpConnection $connection)
109102
{
103+
static $input = array();
104+
if (!isset($recv_buffer[512]) && isset($input[$recv_buffer])) {
105+
return $input[$recv_buffer];
106+
}
110107
$crlf_pos = \strpos($recv_buffer, "\r\n\r\n");
111108
if (false === $crlf_pos) {
112109
// Judge whether the package length exceeds the limit.
113110
if ($recv_len = \strlen($recv_buffer) >= 16384) {
114-
$connection->send("HTTP/1.1 413 Request Entity Too Large\r\n\r\n");
115-
$connection->consumeRecvBuffer($recv_len);
111+
$connection->close("HTTP/1.1 413 Request Entity Too Large\r\n\r\n");
116112
return 0;
117113
}
118114
return 0;
@@ -121,22 +117,39 @@ public static function input($recv_buffer, TcpConnection $connection)
121117
$head_len = $crlf_pos + 4;
122118
$method = \strstr($recv_buffer, ' ', true);
123119

124-
if ($method === 'GET' || $method === 'OPTIONS' || $method === 'HEAD') {
120+
if ($method === 'GET' || $method === 'OPTIONS' || $method === 'HEAD' || $method === 'DELETE') {
121+
if (!isset($recv_buffer[512])) {
122+
$input[$recv_buffer] = $head_len;
123+
if (\count($input) > 512) {
124+
unset($input[key($input)]);
125+
}
126+
}
125127
return $head_len;
126-
} else if ($method !== 'POST' && $method !== 'PUT' && $method !== 'DELETE') {
127-
$connection->send("HTTP/1.1 400 Bad Request\r\n\r\n", true);
128-
$connection->consumeRecvBuffer(\strlen($recv_buffer));
128+
} else if ($method !== 'POST' && $method !== 'PUT') {
129+
$connection->close("HTTP/1.1 400 Bad Request\r\n\r\n", true);
129130
return 0;
130131
}
131132

132133
$header = \substr($recv_buffer, 0, $crlf_pos);
134+
$length = false;
133135
if ($pos = \strpos($header, "\r\nContent-Length: ")) {
134-
return $head_len + (int)\substr($header, $pos + 18, 10);
136+
$length = $head_len + (int)\substr($header, $pos + 18, 10);
135137
} else if (\preg_match("/\r\ncontent-length: ?(\d+)/i", $header, $match)) {
136-
return $head_len + $match[1];
138+
$length = $head_len + $match[1];
139+
}
140+
141+
if ($length !== false) {
142+
if (!isset($recv_buffer[512])) {
143+
$input[$recv_buffer] = $length;
144+
if (\count($input) > 512) {
145+
unset($input[key($input)]);
146+
}
147+
}
148+
return $length;
137149
}
138150

139-
return $method === 'DELETE' ? $head_len : 0;
151+
$connection->close("HTTP/1.1 400 Bad Request\r\n\r\n", true);
152+
return 0;
140153
}
141154

142155
/**
@@ -148,9 +161,10 @@ public static function input($recv_buffer, TcpConnection $connection)
148161
*/
149162
public static function decode($recv_buffer, TcpConnection $connection)
150163
{
164+
static $requests = array();
151165
$cacheable = static::$_enableCache && !isset($recv_buffer[512]);
152-
if (true === $cacheable && isset(static::$_cache[$recv_buffer])) {
153-
$request = static::$_cache[$recv_buffer];
166+
if (true === $cacheable && isset($requests[$recv_buffer])) {
167+
$request = $requests[$recv_buffer];
154168
$request->connection = $connection;
155169
$connection->__request = $request;
156170
$request->properties = array();
@@ -160,9 +174,9 @@ public static function decode($recv_buffer, TcpConnection $connection)
160174
$request->connection = $connection;
161175
$connection->__request = $request;
162176
if (true === $cacheable) {
163-
static::$_cache[$recv_buffer] = $request;
164-
if (\count(static::$_cache) > 512) {
165-
unset(static::$_cache[key(static::$_cache)]);
177+
$requests[$recv_buffer] = $request;
178+
if (\count($requests) > 512) {
179+
unset($requests[key($requests)]);
166180
}
167181
}
168182
return $request;

0 commit comments

Comments
 (0)