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