From 517cbee90091f757369b9ffce7e566a7e0f7226a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Mon, 6 Sep 2021 19:03:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- http-server/Constrict/Request.php | 8 ++++---- http-server/Service/Http.php | 13 ++++++++----- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/http-server/Constrict/Request.php b/http-server/Constrict/Request.php index 20219898..cc86b726 100644 --- a/http-server/Constrict/Request.php +++ b/http-server/Constrict/Request.php @@ -50,14 +50,14 @@ class Request implements RequestInterface /** * @param \Swoole\Http\Request $request - * @return Request + * @return array */ - public static function create(\Swoole\Http\Request $request): RequestInterface + public static function create(\Swoole\Http\Request $request): array { - Context::setContext(ResponseInterface::class, new Response()); + Context::setContext(ResponseInterface::class, $response = new Response()); Context::setContext(RequestMessage::class, RequestMessage::parseRequest($request)); - return Kiri::getDi()->get(Request::class); + return [Kiri::getDi()->get(Request::class), $response]; } } diff --git a/http-server/Service/Http.php b/http-server/Service/Http.php index 3a2303ce..fbf62a98 100644 --- a/http-server/Service/Http.php +++ b/http-server/Service/Http.php @@ -42,18 +42,21 @@ class Http extends \Server\Abstracts\Http implements OnClose, OnConnect { // TODO: Implement onRequest() method. try { - $request = \Server\Constrict\Request::create($request); + [$request, $psr7Response] = \Server\Constrict\Request::create($request); $node = $this->router->Branch_search($request); if (!($node instanceof Node)) { throw new RequestException('

HTTP 404 Not Found


Powered by Swoole', 404); } - if (!(($responseData = $node->dispatch($request)) instanceof ResponseInterface)) { - $responseData = $this->transferToResponse($responseData); + if (!(($psr7Response = $node->dispatch($request)) instanceof ResponseInterface)) { + $psr7Response = $this->transferToResponse($psr7Response); } } catch (Error | \Throwable $exception) { - $responseData = $this->exceptionHandler->emit($exception, $this->response); + $psr7Response = $this->exceptionHandler->emit($exception, $this->response); } finally { - $this->responseEmitter->sender($response, $responseData); + if (!isset($psr7Response)) { + return; + } + $this->responseEmitter->sender($response, $psr7Response); $this->eventDispatch->dispatch(new OnAfterRequest()); } }