This commit is contained in:
as2252258@163.com
2021-08-28 01:57:33 +08:00
parent c898292ebe
commit 567d440dcd
+60 -46
View File
@@ -23,57 +23,71 @@ class Http extends \Server\Abstracts\Http implements OnClose, OnConnect
{ {
/** /**
* @param Server $server * @param Server $server
* @param int $fd * @param int $fd
*/ */
public function onConnect(Server $server, int $fd): void public function onConnect(Server $server, int $fd): void
{ {
// TODO: Implement onConnect() method. // TODO: Implement onConnect() method.
} }
/** /**
* @param Request $request * @param Request $request
* @param Response $response * @param Response $response
*/ */
public function onRequest(Request $request, Response $response): void public function onRequest(Request $request, Response $response): void
{ {
// TODO: Implement onRequest() method. // TODO: Implement onRequest() method.
try { try {
$node = $this->router->Branch_search(\Server\Constrict\Request::create($request)); $node = $this->router->Branch_search(\Server\Constrict\Request::create($request));
if (!($node instanceof Node)) { if (!($node instanceof Node)) {
throw new RequestException('<h2>HTTP 404 Not Found</h2><hr><i>Powered by Swoole</i>', 404); throw new RequestException('<h2>HTTP 404 Not Found</h2><hr><i>Powered by Swoole</i>', 404);
} }
if (!(($responseData = $node->dispatch()) instanceof ResponseInterface)) { if (!(($responseData = $node->dispatch()) instanceof ResponseInterface)) {
$responseData = $this->response->withStatus(200)->withBody(new Stream($responseData)); $responseData = $this->transferToResponse($responseData);
} }
} catch (Error | \Throwable $exception) { } catch (Error | \Throwable $exception) {
$responseData = $this->exceptionHandler->emit($exception, $this->response); $responseData = $this->exceptionHandler->emit($exception, $this->response);
} finally { } finally {
$this->responseEmitter->sender($response, $responseData); $this->responseEmitter->sender($response, $responseData);
$this->eventDispatch->dispatch(new OnAfterRequest()); $this->eventDispatch->dispatch(new OnAfterRequest());
} }
} }
/** /**
* @param Server $server * @param $responseData
* @param int $fd * @return \Server\ResponseInterface
* @throws Exception */
*/ private function transferToResponse($responseData): ResponseInterface
public function onDisconnect(Server $server, int $fd): void {
{ $this->response->withStatus(200);
} if (is_array($responseData)) {
return $this->response->withBody(new Stream(json_encode($responseData, JSON_UNESCAPED_UNICODE)));
}
return $this->response->withBody(new Stream((string)$responseData));
}
/** /**
* @param Server $server * @param Server $server
* @param int $fd * @param int $fd
* @throws Exception * @throws Exception
*/ */
public function onClose(Server $server, int $fd): void public function onDisconnect(Server $server, int $fd): void
{ {
} }
/**
* @param Server $server
* @param int $fd
* @throws Exception
*/
public function onClose(Server $server, int $fd): void
{
}
} }