Skip to content

Commit

Permalink
Fix session #
Browse files Browse the repository at this point in the history
  • Loading branch information
walkor committed Nov 8, 2024
1 parent 5101b08 commit 0d75a17
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 27 deletions.
3 changes: 2 additions & 1 deletion src/Connection/TcpConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/Protocols/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)) {
Expand Down
40 changes: 29 additions & 11 deletions src/Protocols/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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());
}

/**
Expand All @@ -322,22 +322,34 @@ 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');
}
$sid = $sessionId ?: static::createSessionId();
$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);
}

/**
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 0 additions & 12 deletions src/Protocols/Http/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ class Session
*/
public function __construct(string $sessionId)
{
static::checkSessionId($sessionId);
if (static::$handler === null) {
static::initHandler();
}
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 0d75a17

Please sign in to comment.