walkor před 1 rokem
rodič
revize
0d75a17153

+ 2 - 1
src/Connection/TcpConnection.php

@@ -662,7 +662,7 @@ class TcpConnection extends ConnectionInterface implements JsonSerializable
                         } catch (Throwable $e) {
                             $this->error($e);
                         }
-                        $request->properties = [];
+                        $request->destroy();
                         $requests[$buffer] = clone $request;
                         return;
                     }
@@ -732,6 +732,7 @@ class TcpConnection extends ConnectionInterface implements JsonSerializable
                     if (static::$enableCache && (!is_object($request) || $request instanceof Request) && $one && !isset($oneRequestBuffer[static::MAX_CACHE_STRING_LENGTH])) {
                         ($this->onMessage)($this, $request);
                         if ($request instanceof Request) {
+                            $request->destroy();
                             $requests[$oneRequestBuffer] = clone $request;
                         } else {
                             $requests[$oneRequestBuffer] = $request;

+ 2 - 2
src/Protocols/Http.php

@@ -133,7 +133,7 @@ class Http
             $request = $requests[$buffer];
             $request->connection = $connection;
             $connection->request = $request;
-            $request->properties = [];
+            $request->destroy();
             return $request;
         }
         $request = new static::$requestClass($buffer);
@@ -160,7 +160,7 @@ class Http
     {
         if (isset($connection->request)) {
             $request = $connection->request;
-            $request->session = $request->connection = $connection->request = null;
+            $request->connection = $connection->request = null;
         }
 
         if (!is_object($response)) {

+ 29 - 11
src/Protocols/Http/Request.php

@@ -107,11 +107,11 @@ class Request implements Stringable
     protected bool $isSafe = true;
 
     /**
-     * Session id.
+     * Context.
      *
-     * @var mixed
+     * @var array
      */
-    protected mixed $sid;
+    public array $context = [];
 
     /**
      * Request constructor.
@@ -309,7 +309,7 @@ class Request implements Stringable
      */
     public function session(): Session
     {
-        return $this->session ??= new Session($this->sessionId());
+        return $this->context['session'] ??= new Session($this->sessionId());
     }
 
     /**
@@ -322,12 +322,13 @@ class Request implements Stringable
     public function sessionId(?string $sessionId = null): string
     {
         if ($sessionId) {
-            unset($this->sid);
+            unset($this->context['sid']);
         }
-        if (!isset($this->sid)) {
+        if (!isset($this->context['sid'])) {
             $sessionName = Session::$name;
             $sid = $sessionId ? '' : $this->cookie($sessionName);
-            if ($sid === '' || $sid === null) {
+            $sid = $this->isValidSessionId($sid) ? $sid : '';
+            if ($sid === '') {
                 if (!$this->connection) {
                     throw new RuntimeException('Request->session() fail, header already send');
                 }
@@ -335,9 +336,20 @@ class Request implements Stringable
                 $cookieParams = Session::getCookieParams();
                 $this->setSidCookie($sessionName, $sid, $cookieParams);
             }
-            $this->sid = $sid;
+            $this->context['sid'] = $sid;
         }
-        return $this->sid;
+        return $this->context['sid'];
+    }
+
+    /**
+     * Check if session id is valid.
+     *
+     * @param mixed $sessionId
+     * @return bool
+     */
+    public function isValidSessionId(mixed $sessionId): bool
+    {
+        return is_string($sessionId) && preg_match('/^[a-zA-Z0-9"]+$/', $sessionId);
     }
 
     /**
@@ -738,12 +750,18 @@ class Request implements Stringable
     }
 
     /**
-     * __destruct.
+     * Destroy.
      *
      * @return void
      */
-    public function __destruct()
+    public function destroy(): void
     {
+        if ($this->context) {
+            $this->context  = [];
+        }
+        if ($this->properties) {
+            $this->properties = [];
+        }
         if (isset($this->data['files']) && $this->isSafe) {
             clearstatcache();
             array_walk_recursive($this->data['files'], function ($value, $key) {

+ 0 - 12
src/Protocols/Http/Session.php

@@ -163,7 +163,6 @@ class Session
      */
     public function __construct(string $sessionId)
     {
-        static::checkSessionId($sessionId);
         if (static::$handler === null) {
             static::initHandler();
         }
@@ -449,17 +448,6 @@ class Session
         }
     }
 
-    /**
-     * Check session id.
-     *
-     * @param string $sessionId
-     */
-    protected static function checkSessionId(string $sessionId): void
-    {
-        if (!preg_match('/^[a-zA-Z0-9"]+$/', $sessionId)) {
-            throw new RuntimeException("session_id $sessionId is invalid");
-        }
-    }
 }
 
 // Init session.

+ 1 - 1
src/Worker.php

@@ -59,7 +59,7 @@ class Worker
      *
      * @var string
      */
-    final public const VERSION = '5.0.0';
+    final public const VERSION = '5.0.0-RC3';
 
     /**
      * Status starting.