Skip to content

Commit 62e53d3

Browse files
authored
performance optimization
1 parent f63892c commit 62e53d3

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

Protocols/Http.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ class Http
4343
*/
4444
public static function input($recv_buffer, TcpConnection $connection)
4545
{
46+
if (isset(static::$_cache[$recv_buffer]['input'])) {
47+
return static::$_cache[$recv_buffer]['input'];
48+
}
4649
$recv_len = \strlen($recv_buffer);
4750
$crlf_post = \strpos($recv_buffer, "\r\n\r\n");
4851
if (!$crlf_post) {
@@ -61,13 +64,16 @@ public static function input($recv_buffer, TcpConnection $connection)
6164
}
6265

6366
if ($method === 'GET' || $method === 'OPTIONS' || $method === 'HEAD') {
67+
static::$_cache[$recv_buffer]['input'] = $recv_len;
6468
return $recv_len;
6569
}
6670

6771
$match = array();
6872
if (\preg_match("/\r\nContent-Length: ?(\d+)/i", $recv_buffer, $match)) {
6973
$content_length = isset($match[1]) ? $match[1] : 0;
70-
return $content_length + $crlf_post + 4;
74+
$total_length = $content_length + $crlf_post + 4;
75+
static::$_cache[$recv_buffer]['input'] = $total_length;
76+
return $total_length;
7177
}
7278

7379
return $method === 'DELETE' ? $recv_len : 0;
@@ -82,9 +88,9 @@ public static function input($recv_buffer, TcpConnection $connection)
8288
*/
8389
public static function decode($recv_buffer, TcpConnection $connection)
8490
{
85-
if (isset(static::$_cache[$recv_buffer])) {
91+
if (isset(static::$_cache[$recv_buffer]['decode'])) {
8692
HttpCache::reset();
87-
$cache = static::$_cache[$recv_buffer];
93+
$cache = static::$_cache[$recv_buffer]['decode'];
8894
//$cache['server']['REQUEST_TIME_FLOAT'] = \microtime(true);
8995
//$cache['server']['REQUEST_TIME'] = (int)$cache['server']['REQUEST_TIME_FLOAT'];
9096
$_SERVER = $cache['server'];
@@ -93,7 +99,7 @@ public static function decode($recv_buffer, TcpConnection $connection)
9399
$_COOKIE = $cache['cookie'];
94100
$_REQUEST = $cache['request'];
95101
$GLOBALS['HTTP_RAW_POST_DATA'] = $GLOBALS['HTTP_RAW_REQUEST_DATA'] = '';
96-
return static::$_cache[$recv_buffer];
102+
return static::$_cache[$recv_buffer]['decode'];
97103
}
98104
// Init.
99105
$_POST = $_GET = $_COOKIE = $_REQUEST = $_SESSION = $_FILES = array();
@@ -230,7 +236,7 @@ public static function decode($recv_buffer, TcpConnection $connection)
230236
$_SERVER['REMOTE_PORT'] = $connection->getRemotePort();
231237
$ret = array('get' => $_GET, 'post' => $_POST, 'cookie' => $_COOKIE, 'server' => $_SERVER, 'files' => $_FILES, 'request'=>$_REQUEST);
232238
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
233-
static::$_cache[$recv_buffer] = $ret;
239+
static::$_cache[$recv_buffer]['decode'] = $ret;
234240
if (\count(static::$_cache) > 256) {
235241
unset(static::$_cache[key(static::$_cache)]);
236242
}

0 commit comments

Comments
 (0)