111
This commit is contained in:
@@ -5,14 +5,14 @@ namespace Server\Constrict;
|
|||||||
|
|
||||||
|
|
||||||
use Http\Context\Context;
|
use Http\Context\Context;
|
||||||
use Http\Context\Response as HttpResponse;
|
|
||||||
use Server\ResponseInterface;
|
use Server\ResponseInterface;
|
||||||
|
use Server\Message\Response as Psr7Response;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Response
|
* Class Response
|
||||||
* @package Server
|
* @package Server
|
||||||
* @mixin HttpResponse
|
* @mixin Psr7Response
|
||||||
*/
|
*/
|
||||||
class Response implements ResponseInterface
|
class Response implements ResponseInterface
|
||||||
{
|
{
|
||||||
@@ -29,10 +29,10 @@ class Response implements ResponseInterface
|
|||||||
*/
|
*/
|
||||||
public function __call($name, $args)
|
public function __call($name, $args)
|
||||||
{
|
{
|
||||||
if (!Context::hasContext(HttpResponse::class)) {
|
if (!Context::hasContext(Psr7Response::class)) {
|
||||||
$context = Context::setContext(HttpResponse::class, new HttpResponse());
|
$context = Context::setContext(Psr7Response::class, new Psr7Response());
|
||||||
} else {
|
} else {
|
||||||
$context = Context::getContext(HttpResponse::class);
|
$context = Context::getContext(Psr7Response::class);
|
||||||
}
|
}
|
||||||
return $context->{$name}(...$args);
|
return $context->{$name}(...$args);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace Server\Constrict;
|
namespace Server\Constrict;
|
||||||
|
|
||||||
|
use Annotation\Inject;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Http\Context\Formatter\FileFormatter;
|
use Http\Context\Formatter\FileFormatter;
|
||||||
use Kiri\Exception\NotFindClassException;
|
use Kiri\Exception\NotFindClassException;
|
||||||
@@ -17,6 +18,13 @@ class ResponseEmitter implements Emitter
|
|||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Server\Constrict\DownloadEmitter
|
||||||
|
*/
|
||||||
|
#[Inject(DownloadEmitter::class)]
|
||||||
|
public DownloadEmitter $downloadEmitter;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \Swoole\Http\Response|\Swoole\Http2\Response $response
|
* @param \Swoole\Http\Response|\Swoole\Http2\Response $response
|
||||||
* @param ResponseInterface $emitter
|
* @param ResponseInterface $emitter
|
||||||
@@ -26,13 +34,22 @@ class ResponseEmitter implements Emitter
|
|||||||
*/
|
*/
|
||||||
public function sender(mixed $response, ResponseInterface $emitter): void
|
public function sender(mixed $response, ResponseInterface $emitter): void
|
||||||
{
|
{
|
||||||
$content = $emitter->configure($response)->getContent();
|
if (!empty($this->headers) && is_array($this->headers)) {
|
||||||
if ($content instanceof FileFormatter) {
|
foreach ($this->headers as $name => $values) {
|
||||||
di(DownloadEmitter::class)->sender($response, $emitter);
|
$response->header($name, implode(';', $values));
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
$response->header('Content-Type', $emitter->getResponseFormat());
|
$this->headers = [];
|
||||||
$response->end($content->getData());
|
}
|
||||||
|
if (!empty($this->cookies) && is_array($this->cookies)) {
|
||||||
|
foreach ($this->cookies as $name => $cookie) {
|
||||||
|
$response->cookie($name, ...$cookie);
|
||||||
|
}
|
||||||
|
$this->cookies = [];
|
||||||
|
}
|
||||||
|
$response->setStatusCode($emitter->getStatusCode());
|
||||||
|
$response->header('Run-Time', time());
|
||||||
|
$response->end($emitter->getBody());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ namespace Server;
|
|||||||
|
|
||||||
use Server\Constrict\Response;
|
use Server\Constrict\Response;
|
||||||
use Server\Constrict\Response as CResponse;
|
use Server\Constrict\Response as CResponse;
|
||||||
|
use Server\Message\Stream;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -22,14 +23,14 @@ class ExceptionHandlerDispatcher implements ExceptionHandlerInterface
|
|||||||
public function emit(Throwable $exception, Response $response): ResponseInterface
|
public function emit(Throwable $exception, Response $response): ResponseInterface
|
||||||
{
|
{
|
||||||
if ($exception->getCode() == 404) {
|
if ($exception->getCode() == 404) {
|
||||||
return $response->setContent($exception->getMessage())
|
return $response->withBody(new Stream($exception->getMessage()))
|
||||||
->setFormat(CResponse::HTML)
|
->withHeader('Content-Type', 'text/html')
|
||||||
->setStatusCode(404);
|
->withStatus(404);
|
||||||
}
|
}
|
||||||
$code = $exception->getCode() == 0 ? 500 : $exception->getCode();
|
$code = $exception->getCode() == 0 ? 500 : $exception->getCode();
|
||||||
return $response->setContent(jTraceEx($exception, null, true))
|
return $response->withBody(new Stream(jTraceEx($exception, null, true)))
|
||||||
->setFormat(CResponse::HTML)
|
->withHeader('Content-Type', 'text/html')
|
||||||
->setStatusCode($code);
|
->withStatus($code);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,9 +51,8 @@ trait Message
|
|||||||
*/
|
*/
|
||||||
public function withCookie($name, $value = null, $expires = null, $path = null, $domain = null, $secure = null, $httponly = null, $samesite = null, $priority = null): 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;
|
$this->cookies[$name] = [$value, $expires, $path, $domain, $secure, $httponly, $samesite, $priority];
|
||||||
$class->cookies[$name] = [$value, $expires, $path, $domain, $secure, $httponly, $samesite, $priority];
|
return $this;
|
||||||
return $class;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -99,9 +98,8 @@ trait Message
|
|||||||
*/
|
*/
|
||||||
public function withProtocolVersion($version): static
|
public function withProtocolVersion($version): static
|
||||||
{
|
{
|
||||||
$class = clone $this;
|
$this->version = $version;
|
||||||
$class->version = $version;
|
return $this;
|
||||||
return $class;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -188,12 +186,11 @@ trait Message
|
|||||||
*/
|
*/
|
||||||
public function withHeader($name, $value): static
|
public function withHeader($name, $value): static
|
||||||
{
|
{
|
||||||
$class = clone $this;
|
|
||||||
if (!is_array($value)) {
|
if (!is_array($value)) {
|
||||||
$value = [$value];
|
$value = [$value];
|
||||||
}
|
}
|
||||||
$class->headers[$name] = $value;
|
$this->headers[$name] = $value;
|
||||||
return $class;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -205,12 +202,11 @@ trait Message
|
|||||||
*/
|
*/
|
||||||
public function withAddedHeader($name, $value): static
|
public function withAddedHeader($name, $value): static
|
||||||
{
|
{
|
||||||
$class = clone $this;
|
if (!array_key_exists($name, $this->headers)) {
|
||||||
if (!array_key_exists($name, $class->headers)) {
|
|
||||||
throw new \Exception('Headers `' . $name . '` not exists.');
|
throw new \Exception('Headers `' . $name . '` not exists.');
|
||||||
}
|
}
|
||||||
$class->headers[$name][] = $value;
|
$this->headers[$name][] = $value;
|
||||||
return $class;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -220,9 +216,8 @@ trait Message
|
|||||||
*/
|
*/
|
||||||
public function withoutHeader($name): static
|
public function withoutHeader($name): static
|
||||||
{
|
{
|
||||||
$class = clone $this;
|
unset($this->headers[$name]);
|
||||||
unset($class->headers[$name]);
|
return $this;
|
||||||
return $class;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -231,7 +226,7 @@ trait Message
|
|||||||
*/
|
*/
|
||||||
public function getBody(): string
|
public function getBody(): string
|
||||||
{
|
{
|
||||||
return $this->stream;
|
return $this->stream->getContents();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -241,9 +236,8 @@ trait Message
|
|||||||
*/
|
*/
|
||||||
public function withBody(StreamInterface $body): static
|
public function withBody(StreamInterface $body): static
|
||||||
{
|
{
|
||||||
$class = clone $this;
|
$this->stream = $body;
|
||||||
$class->stream = $body;
|
return $this;
|
||||||
return $class;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -275,31 +269,13 @@ trait Message
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $host
|
||||||
|
* @return \Server\Message\Request|\Server\Message\Response
|
||||||
|
*/
|
||||||
public function redirectTo($host)
|
public function redirectTo($host)
|
||||||
{
|
{
|
||||||
return $this->withHeader('Location', $host)
|
return $this->withHeader('Location', $host)
|
||||||
->withStatus(302);
|
->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 = [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,15 +30,13 @@ class Response implements ResponseInterface
|
|||||||
/**
|
/**
|
||||||
* @param int $code
|
* @param int $code
|
||||||
* @param string $reasonPhrase
|
* @param string $reasonPhrase
|
||||||
* @return ResponseInterface
|
* @return static
|
||||||
*/
|
*/
|
||||||
public function withStatus($code, $reasonPhrase = ''): ResponseInterface
|
public function withStatus($code, $reasonPhrase = ''): static
|
||||||
{
|
{
|
||||||
// TODO: Implement withStatus() method.
|
$this->statusCode = $code;
|
||||||
$class = clone $this;
|
$this->reasonPhrase = $reasonPhrase;
|
||||||
$class->statusCode = $code;
|
return $this;
|
||||||
$class->reasonPhrase = $reasonPhrase;
|
|
||||||
return $class;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
namespace Server;
|
namespace Server;
|
||||||
|
|
||||||
|
|
||||||
use Http\Context\Response;
|
use Server\Message\Response;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @mixin Response
|
* @mixin Response
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ use Exception;
|
|||||||
use Http\Exception\RequestException;
|
use Http\Exception\RequestException;
|
||||||
use Http\Route\Node;
|
use Http\Route\Node;
|
||||||
use Server\Events\OnAfterRequest;
|
use Server\Events\OnAfterRequest;
|
||||||
|
use Server\Message\Stream;
|
||||||
use Server\ResponseInterface;
|
use Server\ResponseInterface;
|
||||||
use Server\SInterface\OnClose;
|
use Server\SInterface\OnClose;
|
||||||
use Server\SInterface\OnConnect;
|
use Server\SInterface\OnConnect;
|
||||||
@@ -45,7 +46,7 @@ class Http extends \Server\Abstracts\Http implements OnClose, OnConnect
|
|||||||
throw new RequestException('<h2>HTTP 404 Not Found</h2><hr><i>Powered by Swoole</i>', 404);
|
throw new RequestException('<h2>HTTP 404 Not Found</h2><hr><i>Powered by Swoole</i>', 404);
|
||||||
}
|
}
|
||||||
if (!(($responseData = $node->dispatch()) instanceof ResponseInterface)) {
|
if (!(($responseData = $node->dispatch()) instanceof ResponseInterface)) {
|
||||||
$responseData = $this->response->setContent($responseData)->setStatusCode(200);
|
$responseData = $this->response->withStatus(200)->withBody(new Stream($responseData));
|
||||||
}
|
}
|
||||||
} catch (Error | \Throwable $exception) {
|
} catch (Error | \Throwable $exception) {
|
||||||
$responseData = $this->exceptionHandler->emit($exception, $this->response);
|
$responseData = $this->exceptionHandler->emit($exception, $this->response);
|
||||||
|
|||||||
Reference in New Issue
Block a user