Bläddra i källkod

websocket http header optimization

walkor 9 år sedan
förälder
incheckning
2be13d13a8
2 ändrade filer med 25 tillägg och 25 borttagningar
  1. 5 3
      Protocols/Http.php
  2. 20 22
      Protocols/Websocket.php

+ 5 - 3
Protocols/Http.php

@@ -104,9 +104,9 @@ class Http
             if (empty($content)) {
                 continue;
             }
-            list($key, $value) = explode(':', $content, 2);
-            $key   = str_replace('-', '_', strtoupper($key));
-            $value = trim($value);
+            list($key, $value)       = explode(':', $content, 2);
+            $key                     = str_replace('-', '_', strtoupper($key));
+            $value                   = trim($value);
             $_SERVER['HTTP_' . $key] = $value;
             switch ($key) {
                 // HTTP_HOST
@@ -129,6 +129,7 @@ class Http
                         $_SERVER['CONTENT_TYPE'] = 'multipart/form-data';
                         $http_post_boundary      = '--' . $match[1];
                     }
+                    break;
                 case 'CONTENT_LENGTH':
                     $_SERVER['CONTENT_LENGTH'] = $value;
                     break;
@@ -338,6 +339,7 @@ class Http
                 session_decode($raw);
             }
         }
+        return true;
     }
 
     /**

+ 20 - 22
Protocols/Websocket.php

@@ -356,7 +356,10 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
                     Worker::log($e);
                     exit(250);
                 }
-                $_GET = $_COOKIE = $_SERVER = array();
+                if ($_SESSION && class_exists('\GatewayWorker\Lib\Context')) {
+                    $connection->session = \GatewayWorker\Lib\Context::sessionEncode($_SESSION);
+                }
+                $_GET = $_SERVER = $_SESSION = $_COOKIE = array();
             }
             if (strlen($buffer) > $header_length) {
                 return self::input(substr($buffer, $header_length), $connection);
@@ -384,45 +387,40 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
      */
     protected static function parseHttpHeader($buffer)
     {
-        $header_data = explode("\r\n", $buffer);
-        $_SERVER     = array();
+        // Parse headers.
+        list($http_header, ) = explode("\r\n\r\n", $buffer, 2);
+        $header_data = explode("\r\n", $http_header);
+
+        if ($_SERVER) {
+            $_SERVER = array();
+        }
+
         list($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'], $_SERVER['SERVER_PROTOCOL']) = explode(' ',
             $header_data[0]);
+
         unset($header_data[0]);
         foreach ($header_data as $content) {
             // \r\n\r\n
             if (empty($content)) {
                 continue;
             }
-            list($key, $value) = explode(':', $content, 2);
-            $key   = strtolower($key);
-            $value = trim($value);
+            list($key, $value)       = explode(':', $content, 2);
+            $key                     = str_replace('-', '_', strtoupper($key));
+            $value                   = trim($value);
+            $_SERVER['HTTP_' . $key] = $value;
             switch ($key) {
                 // HTTP_HOST
-                case 'host':
-                    $_SERVER['HTTP_HOST']   = $value;
+                case 'HOST':
                     $tmp                    = explode(':', $value);
                     $_SERVER['SERVER_NAME'] = $tmp[0];
                     if (isset($tmp[1])) {
                         $_SERVER['SERVER_PORT'] = $tmp[1];
                     }
                     break;
-                // HTTP_COOKIE
-                case 'cookie':
-                    $_SERVER['HTTP_COOKIE'] = $value;
+                // cookie
+                case 'COOKIE':
                     parse_str(str_replace('; ', '&', $_SERVER['HTTP_COOKIE']), $_COOKIE);
                     break;
-                // HTTP_USER_AGENT
-                case 'user-agent':
-                    $_SERVER['HTTP_USER_AGENT'] = $value;
-                    break;
-                // HTTP_REFERER
-                case 'referer':
-                    $_SERVER['HTTP_REFERER'] = $value;
-                    break;
-                case 'origin':
-                    $_SERVER['HTTP_ORIGIN'] = $value;
-                    break;
             }
         }