From 30dae81e7e7839e65a605021415afa1a481a6951 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sat, 28 Aug 2021 01:21:23 +0800 Subject: [PATCH] 111 --- http-server/Constrict/Response.php | 10 ++-- http-server/Constrict/ResponseEmitter.php | 51 ++++++++++++------ http-server/ExceptionHandlerDispatcher.php | 35 +++++++------ http-server/Message/Message.php | 60 +++++++--------------- http-server/Message/Response.php | 12 ++--- http-server/ResponseInterface.php | 2 +- http-server/Service/Http.php | 3 +- 7 files changed, 83 insertions(+), 90 deletions(-) diff --git a/http-server/Constrict/Response.php b/http-server/Constrict/Response.php index 3dbf813e..05f488bb 100644 --- a/http-server/Constrict/Response.php +++ b/http-server/Constrict/Response.php @@ -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); } diff --git a/http-server/Constrict/ResponseEmitter.php b/http-server/Constrict/ResponseEmitter.php index 9ad5a9b6..9ee13d5b 100644 --- a/http-server/Constrict/ResponseEmitter.php +++ b/http-server/Constrict/ResponseEmitter.php @@ -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()); + + } } diff --git a/http-server/ExceptionHandlerDispatcher.php b/http-server/ExceptionHandlerDispatcher.php index 1b85c35c..39eb0db6 100644 --- a/http-server/ExceptionHandlerDispatcher.php +++ b/http-server/ExceptionHandlerDispatcher.php @@ -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); + } } diff --git a/http-server/Message/Message.php b/http-server/Message/Message.php index d59bad2f..6b7ccf52 100644 --- a/http-server/Message/Message.php +++ b/http-server/Message/Message.php @@ -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 = []; - } - } - - } diff --git a/http-server/Message/Response.php b/http-server/Message/Response.php index 6e5a3902..88a8c092 100644 --- a/http-server/Message/Response.php +++ b/http-server/Message/Response.php @@ -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; } diff --git a/http-server/ResponseInterface.php b/http-server/ResponseInterface.php index 9e67a375..f5e47191 100644 --- a/http-server/ResponseInterface.php +++ b/http-server/ResponseInterface.php @@ -3,7 +3,7 @@ namespace Server; -use Http\Context\Response; +use Server\Message\Response; /** * @mixin Response diff --git a/http-server/Service/Http.php b/http-server/Service/Http.php index c97f2098..b5b9887d 100644 --- a/http-server/Service/Http.php +++ b/http-server/Service/Http.php @@ -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('

HTTP 404 Not Found


Powered by Swoole', 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);