This commit is contained in:
2021-09-16 14:45:59 +08:00
parent 7e7239be87
commit e0b72c4f74
+12 -4
View File
@@ -42,15 +42,13 @@ class Http extends \Server\Abstracts\Http implements OnClose, OnConnect
public function onRequest(Request $request, Response $response): void public function onRequest(Request $request, Response $response): void
{ {
try { try {
$node = $this->router->radix_tree($Psr7Request = ScRequest::create($request)); if (!(($node = $this->router->radix_tree($Psr7Request = ScRequest::create($request))) instanceof Node)) {
if (!($node instanceof Node)) {
throw new RequestException(Constant::STATUS_404_MESSAGE, 404); throw new RequestException(Constant::STATUS_404_MESSAGE, 404);
} }
if (!(($psr7Response = $node->dispatch($Psr7Request)) instanceof ResponseInterface)) { if (!(($psr7Response = $node->dispatch($Psr7Request)) instanceof ResponseInterface)) {
$psr7Response = $this->transferToResponse($psr7Response); $psr7Response = $this->transferToResponse($psr7Response);
} }
$request_time_float = $request->server['request_time_float'] - $request->server['request_time']; $psr7Response->withHeader('Run-Time', $this->_runTime($request));
$psr7Response->withHeader('Run-Time', microtime(true) - $request_time_float);
} catch (Error | \Throwable $exception) { } catch (Error | \Throwable $exception) {
$psr7Response = $this->exceptionHandler->emit($exception, $this->response); $psr7Response = $this->exceptionHandler->emit($exception, $this->response);
} finally { } finally {
@@ -60,6 +58,16 @@ class Http extends \Server\Abstracts\Http implements OnClose, OnConnect
} }
/**
* @param Request $request
* @return float
*/
private function _runTime(Request $request): float
{
return round(microtime(true) - ($request->server['request_time_float'] - $request->server['request_time']), 6);
}
/** /**
* @param mixed $responseData * @param mixed $responseData
* @return ResponseInterface * @return ResponseInterface