walkor пре 5 година
родитељ
комит
b1eeec9813
1 измењених фајлова са 35 додато и 21 уклоњено
  1. 35 21
      Protocols/Http.php

+ 35 - 21
Protocols/Http.php

@@ -47,13 +47,6 @@ class Http
     protected static $_uploadTmpDir = '';
 
     /**
-     * Cache.
-     *
-     * @var array
-     */
-    protected static $_cache = array();
-
-    /**
      * Open cache.
      *
      * @var bool.
@@ -107,12 +100,15 @@ class Http
      */
     public static function input($recv_buffer, TcpConnection $connection)
     {
+        static $input = array();
+        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) {
-                $connection->send("HTTP/1.1 413 Request Entity Too Large\r\n\r\n");
-                $connection->consumeRecvBuffer($recv_len);
+                $connection->close("HTTP/1.1 413 Request Entity Too Large\r\n\r\n");
                 return 0;
             }
             return 0;
@@ -121,22 +117,39 @@ class Http
         $head_len = $crlf_pos + 4;
         $method = \strstr($recv_buffer, ' ', true);
 
-        if ($method === 'GET' || $method === 'OPTIONS' || $method === 'HEAD') {
+        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 !== 'DELETE') {
-            $connection->send("HTTP/1.1 400 Bad Request\r\n\r\n", true);
-            $connection->consumeRecvBuffer(\strlen($recv_buffer));
+        } else if ($method !== 'POST' && $method !== 'PUT') {
+            $connection->close("HTTP/1.1 400 Bad Request\r\n\r\n", true);
             return 0;
         }
 
         $header = \substr($recv_buffer, 0, $crlf_pos);
+        $length = false;
         if ($pos = \strpos($header, "\r\nContent-Length: ")) {
-            return  $head_len + (int)\substr($header, $pos + 18, 10);
+            $length = $head_len + (int)\substr($header, $pos + 18, 10);
         } else if (\preg_match("/\r\ncontent-length: ?(\d+)/i", $header, $match)) {
-            return $head_len + $match[1];
+            $length = $head_len + $match[1];
+        }
+
+        if ($length !== false) {
+            if (!isset($recv_buffer[512])) {
+                $input[$recv_buffer] = $length;
+                if (\count($input) > 512) {
+                    unset($input[key($input)]);
+                }
+            }
+            return $length;
         }
 
-        return $method === 'DELETE' ? $head_len : 0;
+        $connection->close("HTTP/1.1 400 Bad Request\r\n\r\n", true);
+        return 0;
     }
 
     /**
@@ -148,9 +161,10 @@ class Http
      */
     public static function decode($recv_buffer, TcpConnection $connection)
     {
+        static $requests = array();
         $cacheable = static::$_enableCache && !isset($recv_buffer[512]);
-        if (true === $cacheable && isset(static::$_cache[$recv_buffer])) {
-            $request = static::$_cache[$recv_buffer];
+        if (true === $cacheable && isset($requests[$recv_buffer])) {
+            $request = $requests[$recv_buffer];
             $request->connection = $connection;
             $connection->__request = $request;
             $request->properties = array();
@@ -160,9 +174,9 @@ class Http
         $request->connection = $connection;
         $connection->__request = $request;
         if (true === $cacheable) {
-            static::$_cache[$recv_buffer] = $request;
-            if (\count(static::$_cache) > 512) {
-                unset(static::$_cache[key(static::$_cache)]);
+            $requests[$recv_buffer] = $request;
+            if (\count($requests) > 512) {
+                unset($requests[key($requests)]);
             }
         }
         return $request;