Răsfoiți Sursa

optimizations

walkor 2 ani în urmă
părinte
comite
301858e5a2

+ 12 - 2
src/Connection/TcpConnection.php

@@ -162,9 +162,19 @@ class TcpConnection extends ConnectionInterface implements \JsonSerializable
     /**
      * Context.
      *
-     * @var object|null
+     * @var object
      */
-    public $context = null;
+    public $context;
+
+    /**
+     * @var array
+     */
+    public $headers = [];
+
+    /**
+     * @var object
+     */
+    public $request;
 
     /**
      * Default send buffer size.

+ 6 - 6
src/Protocols/Http.php

@@ -185,8 +185,8 @@ class Http
         }
         if (!\is_object($response)) {
             $extHeader = '';
-            if (isset($connection->header)) {
-                foreach ($connection->header as $name => $value) {
+            if (isset($connection->headers)) {
+                foreach ($connection->headers as $name => $value) {
                     if (\is_array($value)) {
                         foreach ($value as $item) {
                             $extHeader = "$name: $item\r\n";
@@ -195,15 +195,15 @@ class Http
                         $extHeader = "$name: $value\r\n";
                     }
                 }
-                unset($connection->header);
+                $connection->headers = [];
             }
             $bodyLen = \strlen((string)$response);
             return "HTTP/1.1 200 OK\r\nServer: workerman\r\n{$extHeader}Connection: keep-alive\r\nContent-Type: text/html;charset=utf-8\r\nContent-Length: $bodyLen\r\n\r\n$response";
         }
 
-        if (isset($connection->header)) {
-            $response->withHeaders($connection->header);
-            unset($connection->header);
+        if (isset($connection->headers)) {
+            $response->withHeaders($connection->headers);
+            $connection->headers = [];
         }
 
         if (isset($response->file)) {

+ 1 - 1
src/Protocols/Http/Request.php

@@ -623,7 +623,7 @@ class Request
      */
     protected function setSidCookie(string $sessionName, string $sid, array $cookieParams)
     {
-        $this->connection->header['Set-Cookie'] = [$sessionName . '=' . $sid
+        $this->connection->headers['Set-Cookie'] = [$sessionName . '=' . $sid
             . (empty($cookieParams['domain']) ? '' : '; Domain=' . $cookieParams['domain'])
             . (empty($cookieParams['lifetime']) ? '' : '; Max-Age=' . $cookieParams['lifetime'])
             . (empty($cookieParams['path']) ? '' : '; Path=' . $cookieParams['path'])

+ 11 - 11
src/Protocols/Http/Response.php

@@ -25,7 +25,7 @@ class Response
      *
      * @var array
      */
-    protected $header = null;
+    protected $headers = null;
 
     /**
      * Http status.
@@ -169,7 +169,7 @@ class Response
     )
     {
         $this->status = $status;
-        $this->header = $headers;
+        $this->headers = $headers;
         $this->body = (string)$body;
     }
 
@@ -182,7 +182,7 @@ class Response
      */
     public function header($name, $value)
     {
-        $this->header[$name] = $value;
+        $this->headers[$name] = $value;
         return $this;
     }
 
@@ -206,7 +206,7 @@ class Response
      */
     public function withHeaders($headers)
     {
-        $this->header = \array_merge_recursive($this->header, $headers);
+        $this->headers = \array_merge_recursive($this->headers, $headers);
         return $this;
     }
 
@@ -218,7 +218,7 @@ class Response
      */
     public function withoutHeader($name)
     {
-        unset($this->header[$name]);
+        unset($this->headers[$name]);
         return $this;
     }
 
@@ -231,7 +231,7 @@ class Response
     public function getHeader($name)
     {
 
-        return $this->header[$name] ?? null;
+        return $this->headers[$name] ?? null;
     }
 
     /**
@@ -241,7 +241,7 @@ class Response
      */
     public function getHeaders()
     {
-        return $this->header;
+        return $this->headers;
     }
 
     /**
@@ -344,7 +344,7 @@ class Response
      */
     public function cookie($name, $value = '', $maxAge = null, $path = '', $domain = '', $secure = false, $httpOnly = false, $sameSite  = false)
     {
-        $this->header['Set-Cookie'][] = $name . '=' . \rawurlencode($value)
+        $this->headers['Set-Cookie'][] = $name . '=' . \rawurlencode($value)
             . (empty($domain) ? '' : '; Domain=' . $domain)
             . ($maxAge === null ? '' : '; Max-Age=' . $maxAge)
             . (empty($path) ? '' : '; Path=' . $path)
@@ -365,7 +365,7 @@ class Response
         $file = $fileInfo['file'];
         $reason = $this->reason ?: self::PHRASES[$this->status];
         $head = "HTTP/{$this->version} {$this->status} $reason\r\n";
-        $headers = $this->header;
+        $headers = $this->headers;
         if (!isset($headers['Server'])) {
             $head .= "Server: workerman\r\n";
         }
@@ -420,12 +420,12 @@ class Response
 
         $reason = $this->reason ?: self::PHRASES[$this->status] ?? '';
         $bodyLen = \strlen($this->body);
-        if (empty($this->header)) {
+        if (empty($this->headers)) {
             return "HTTP/{$this->version} {$this->status} $reason\r\nServer: workerman\r\nContent-Type: text/html;charset=utf-8\r\nContent-Length: $bodyLen\r\nConnection: keep-alive\r\n\r\n{$this->body}";
         }
 
         $head = "HTTP/{$this->version} {$this->status} $reason\r\n";
-        $headers = $this->header;
+        $headers = $this->headers;
         if (!isset($headers['Server'])) {
             $head .= "Server: workerman\r\n";
         }

+ 10 - 10
src/Protocols/Ws.php

@@ -321,9 +321,9 @@ class Ws
         $connection->context->websocketCurrentFrameLength = 0;
         $connection->context->tmpWebsocketData = '';
         $connection->context->websocketDataBuffer = '';
-        if (!empty($connection->websocketPingTimer)) {
-            Timer::del($connection->websocketPingTimer);
-            $connection->websocketPingTimer = null;
+        if (!empty($connection->context->websocketPingTimer)) {
+            Timer::del($connection->context->websocketPingTimer);
+            $connection->context->websocketPingTimer = null;
         }
     }
 
@@ -342,8 +342,8 @@ class Ws
         $port = $connection->getRemotePort();
         $host = $port === 80 ? $connection->getRemoteHost() : $connection->getRemoteHost() . ':' . $port;
         // Handshake header.
-        $connection->websocketSecKey = \base64_encode(random_bytes(16));
-        $userHeader = $connection->headers ?? $connection->wsHttpHeader ?? null;
+        $connection->context->websocketSecKey = \base64_encode(random_bytes(16));
+        $userHeader = $connection->headers ?? null;
         $userHeaderStr = '';
         if (!empty($userHeader)) {
             if (\is_array($userHeader)) {
@@ -362,7 +362,7 @@ class Ws
             (isset($connection->websocketOrigin) ? "Origin: " . $connection->websocketOrigin . "\r\n" : '') .
             (isset($connection->websocketClientProtocol) ? "Sec-WebSocket-Protocol: " . $connection->websocketClientProtocol . "\r\n" : '') .
             "Sec-WebSocket-Version: 13\r\n" .
-            "Sec-WebSocket-Key: " . $connection->websocketSecKey . $userHeaderStr . "\r\n\r\n";
+            "Sec-WebSocket-Key: " . $connection->context->websocketSecKey . $userHeaderStr . "\r\n\r\n";
         $connection->send($header, true);
         $connection->context->handshakeStep = 1;
         $connection->context->websocketCurrentFrameLength = 0;
@@ -383,7 +383,7 @@ class Ws
         if ($pos) {
             //checking Sec-WebSocket-Accept
             if (\preg_match("/Sec-WebSocket-Accept: *(.*?)\r\n/i", $buffer, $match)) {
-                if ($match[1] !== \base64_encode(\sha1($connection->websocketSecKey . "258EAFA5-E914-47DA-95CA-C5AB0DC85B11", true))) {
+                if ($match[1] !== \base64_encode(\sha1($connection->context->websocketSecKey . "258EAFA5-E914-47DA-95CA-C5AB0DC85B11", true))) {
                     Worker::safeEcho("Sec-WebSocket-Accept not match. Header:\n" . \substr($buffer, 0, $pos) . "\n");
                     $connection->close();
                     return 0;
@@ -413,10 +413,10 @@ class Ws
             }
             // Headbeat.
             if (!empty($connection->websocketPingInterval)) {
-                $connection->websocketPingTimer = Timer::add($connection->websocketPingInterval, function () use ($connection) {
+                $connection->context->websocketPingTimer = Timer::add($connection->websocketPingInterval, function () use ($connection) {
                     if (false === $connection->send(\pack('H*', '898000000000'), true)) {
-                        Timer::del($connection->websocketPingTimer);
-                        $connection->websocketPingTimer = null;
+                        Timer::del($connection->context->websocketPingTimer);
+                        $connection->context->websocketPingTimer = null;
                     }
                 });
             }