From 0510c1af41e325cc5cd1863569ac564c9a1e7337 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Tue, 27 Jul 2021 18:34:57 +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 --- HttpServer/Http/Response.php | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/HttpServer/Http/Response.php b/HttpServer/Http/Response.php index 8c9aa808..9a19b41e 100644 --- a/HttpServer/Http/Response.php +++ b/HttpServer/Http/Response.php @@ -165,9 +165,9 @@ class Response extends HttpService public function send(mixed $context = '', int $statusCode = 200): mixed { $sendData = $this->parseData($context); - $response = Context::getContext('response'); - if ($response instanceof SResponse) { - $this->sendData($response, $sendData, $statusCode); + $this->statusCode = $statusCode; + if ($this->response instanceof SResponse) { + $this->sendData($sendData); } else { $this->printResult($sendData); } @@ -214,19 +214,21 @@ class Response extends HttpService } /** - * @param SResponse $response * @param $sendData - * @param $status - * @throws Exception */ - private function sendData(SResponse $response, $sendData, $status): void + private function sendData($sendData): void { - if (!$response->isWritable()) { + if (!$this->response->isWritable()) { return; } - $this->setHeaders($response); - $response->status($status); - $response->end($sendData); + defer(fn() => $this->headers = []); + $this->response->header('Content-Type', $this->getContentType()); + $this->response->header('Run-Time', $this->getRuntime()); + foreach ($this->headers as $key => $header) { + $this->response->header($key, $header); + } + $this->response->status($this->statusCode); + $this->response->end($sendData); } @@ -294,8 +296,9 @@ class Response extends HttpService */ public static function create($response = null): static { - Context::setContext('response', $response); $ciResponse = Snowflake::getDi()->get(Response::class); + Context::setContext('response', $ciResponse); + $ciResponse->response = $response; $ciResponse->startTime = microtime(true); $ciResponse->format = self::JSON; return $ciResponse;