This commit is contained in:
2021-08-28 00:52:20 +08:00
parent a29ed02053
commit 8be7975d54
4 changed files with 157 additions and 12 deletions
+87 -2
View File
@@ -4,9 +4,7 @@ namespace Server\Message;
use JetBrains\PhpStorm\Pure;
use Kiri\Core\Xml;
use Kiri\Kiri;
use Psr\Http\Message\StreamInterface;
use ReflectionException;
/**
@@ -27,6 +25,9 @@ trait Message
public array $servers = [];
public array $cookies = [];
/**
* @return string
*/
@@ -36,6 +37,62 @@ trait Message
}
/**
* @param $name
* @param null $value
* @param null $expires
* @param null $path
* @param null $domain
* @param null $secure
* @param null $httponly
* @param null $samesite
* @param null $priority
* @return static
*/
public function withCookie($name, $value = null, $expires = null, $path = null, $domain = null, $secure = null, $httponly = null, $samesite = null, $priority = null): static
{
$class = clone $this;
$class->cookies[$name] = [$value, $expires, $path, $domain, $secure, $httponly, $samesite, $priority];
return $class;
}
/**
* @return array
*/
public function getCookie(): array
{
return $this->cookies;
}
/**
* @return string
*/
#[Pure] public function getAccessControlAllowOrigin(): string
{
return $this->getHeaderLine('Access-Control-Allow-Origin');
}
/**
* @return string
*/
#[Pure] public function getAccessControlRequestHeaders(): string
{
return $this->getHeaderLine('Access-Control-Request-Headers');
}
/**
* @return string
*/
#[Pure] public function getAccessControlRequestMethod(): string
{
return $this->getHeaderLine('Access-Control-Request-Method');
}
/**
* @param $version
* @return $this
@@ -217,4 +274,32 @@ trait Message
return $content;
}
public function redirectTo($host)
{
return $this->withHeader('Location', $host)
->withStatus(302);
}
public function getStreamData()
{
$response = new \Swoole\Http\Response();
$response->setStatusCode($this->statusCode);
$response->setHeader('Run-Time', time());
if (!empty($this->headers) && is_array($this->headers)) {
foreach ($this->headers as $name => $values) {
$response->setHeader($name, implode(';', $values));
}
$this->headers = [];
}
if (!empty($this->cookies) && is_array($this->cookies)) {
foreach ($this->cookies as $name => $cookie) {
$response->cookie($name, ...$cookie);
}
$this->cookies = [];
}
}
}