From 0d75a171538fde32ab28859710c3c37ecbc7b520 Mon Sep 17 00:00:00 2001 From: walkor Date: Fri, 8 Nov 2024 23:28:17 +0800 Subject: [PATCH] Fix session # --- src/Connection/TcpConnection.php | 3 ++- src/Protocols/Http.php | 4 ++-- src/Protocols/Http/Request.php | 40 +++++++++++++++++++++++--------- src/Protocols/Http/Session.php | 12 ---------- src/Worker.php | 2 +- 5 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/Connection/TcpConnection.php b/src/Connection/TcpConnection.php index b0bec479..2ade1388 100644 --- a/src/Connection/TcpConnection.php +++ b/src/Connection/TcpConnection.php @@ -662,7 +662,7 @@ public function baseRead($socket, bool $checkEof = true): void } catch (Throwable $e) { $this->error($e); } - $request->properties = []; + $request->destroy(); $requests[$buffer] = clone $request; return; } @@ -732,6 +732,7 @@ public function baseRead($socket, bool $checkEof = true): void 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; diff --git a/src/Protocols/Http.php b/src/Protocols/Http.php index 1afaba1c..4623f865 100644 --- a/src/Protocols/Http.php +++ b/src/Protocols/Http.php @@ -133,7 +133,7 @@ public static function decode(string $buffer, TcpConnection $connection): Reques $request = $requests[$buffer]; $request->connection = $connection; $connection->request = $request; - $request->properties = []; + $request->destroy(); return $request; } $request = new static::$requestClass($buffer); @@ -160,7 +160,7 @@ public static function encode(mixed $response, TcpConnection $connection): strin { if (isset($connection->request)) { $request = $connection->request; - $request->session = $request->connection = $connection->request = null; + $request->connection = $connection->request = null; } if (!is_object($response)) { diff --git a/src/Protocols/Http/Request.php b/src/Protocols/Http/Request.php index f3c87737..72b31e5b 100644 --- a/src/Protocols/Http/Request.php +++ b/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 @@ public function queryString(): string */ public function session(): Session { - return $this->session ??= new Session($this->sessionId()); + return $this->context['session'] ??= new Session($this->sessionId()); } /** @@ -322,12 +322,13 @@ public function session(): Session 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 @@ public function sessionId(?string $sessionId = null): string $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 @@ public function __wakeup(): void } /** - * __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) { diff --git a/src/Protocols/Http/Session.php b/src/Protocols/Http/Session.php index 6554e5f8..7bdd5337 100644 --- a/src/Protocols/Http/Session.php +++ b/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 @@ public function __destruct() } } - /** - * 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. diff --git a/src/Worker.php b/src/Worker.php index fdd81e47..0485db7a 100644 --- a/src/Worker.php +++ b/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.