Files
kiri-core/http-server/Service/Http.php
T

160 lines
4.0 KiB
PHP
Raw Normal View History

2021-08-12 12:40:06 +08:00
<?php
namespace Server\Service;
2021-09-18 13:45:33 +08:00
use Annotation\Inject;
2021-08-12 12:40:06 +08:00
use Exception;
2021-09-18 11:44:42 +08:00
use Http\Handler\Abstracts\HandlerManager;
2021-09-24 17:46:20 +08:00
use Http\Handler\Abstracts\MiddlewareManager;
use Http\Handler\Context;
2021-09-18 11:44:42 +08:00
use Http\Handler\Dispatcher;
use Http\Handler\Handler;
2021-09-24 02:04:36 +08:00
use Http\Handler\Router;
2021-09-18 11:44:42 +08:00
use Http\Message\ServerRequest;
use Http\Message\Stream;
2021-09-24 02:04:36 +08:00
use Kiri\Abstracts\Config;
use Kiri\Exception\ConfigException;
2021-09-20 03:29:00 +08:00
use Kiri\Kiri;
2021-09-18 11:44:42 +08:00
use Psr\Http\Message\ServerRequestInterface;
2021-09-24 02:04:36 +08:00
use Server\Abstracts\Utility\EventDispatchHelper;
use Server\Abstracts\Utility\ResponseHelper;
2021-09-18 11:44:42 +08:00
use Server\Constrict\RequestInterface;
2021-09-24 02:04:36 +08:00
use Server\Constrict\ResponseEmitter;
2021-09-10 10:45:24 +08:00
use Server\Constrict\ResponseInterface;
2021-09-23 01:03:36 +08:00
use Server\Events\OnAfterRequest;
2021-09-24 02:04:36 +08:00
use Server\ExceptionHandlerDispatcher;
use Server\ExceptionHandlerInterface;
2021-09-24 02:23:24 +08:00
use Server\SInterface\OnCloseInterface;
use Server\SInterface\OnConnectInterface;
use Server\SInterface\OnRequestInterface;
2021-09-24 18:14:38 +08:00
use Swoole\Coroutine\Iterator;
2021-08-12 12:40:06 +08:00
use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\Server;
/**
*
*/
2021-09-24 02:23:24 +08:00
class Http implements OnCloseInterface, OnConnectInterface, OnRequestInterface
2021-08-12 12:40:06 +08:00
{
2021-09-24 17:46:20 +08:00
use EventDispatchHelper;
use ResponseHelper;
2021-09-24 02:04:36 +08:00
2021-09-24 17:46:20 +08:00
/** @var Router|mixed */
#[Inject(Router::class)]
public Router $router;
2021-09-24 02:04:36 +08:00
2021-09-24 17:46:20 +08:00
/**
* @var ExceptionHandlerInterface
*/
public ExceptionHandlerInterface $exceptionHandler;
2021-09-24 02:04:36 +08:00
2021-09-24 17:46:20 +08:00
/**
* @throws ConfigException
*/
public function init()
{
$exceptionHandler = Config::get('exception.http', ExceptionHandlerDispatcher::class);
if (!in_array(ExceptionHandlerInterface::class, class_implements($exceptionHandler))) {
$exceptionHandler = ExceptionHandlerDispatcher::class;
}
$this->exceptionHandler = Kiri::getDi()->get($exceptionHandler);
$this->responseEmitter = Kiri::getDi()->get(ResponseEmitter::class);
}
2021-09-24 02:04:36 +08:00
2021-08-12 12:40:06 +08:00
2021-08-31 14:01:18 +08:00
/**
* @param Server $server
* @param int $fd
*/
public function onConnect(Server $server, int $fd): void
{
// TODO: Implement onConnect() method.
}
2021-08-12 14:43:16 +08:00
2021-08-31 14:01:18 +08:00
/**
* @param Request $request
* @param Response $response
2021-09-18 10:38:38 +08:00
* @throws Exception
2021-08-31 14:01:18 +08:00
*/
public function onRequest(Request $request, Response $response): void
{
try {
2021-09-24 17:46:20 +08:00
[$PsrRequest, $PsrResponse] = $this->initRequestResponse($request);
2021-09-18 11:44:42 +08:00
/** @var Handler $handler */
$handler = HandlerManager::get($request->server['request_uri'], $request->getMethod());
if (is_integer($handler)) {
$PsrResponse->withStatus($handler)->withBody(new Stream('Allow Method[' . $request->getMethod() . '].'));
} else if (is_null($handler)) {
$PsrResponse->withStatus(404)->withBody(new Stream('Page not found.'));
} else {
$PsrResponse = $this->handler($handler, $PsrRequest);
2021-09-06 18:54:14 +08:00
}
2021-09-18 11:44:42 +08:00
} catch (\Throwable $throwable) {
2021-09-24 17:46:20 +08:00
$PsrResponse = $this->exceptionHandler->emit($throwable, $this->response);
2021-08-31 14:01:18 +08:00
} finally {
2021-09-18 11:46:12 +08:00
$this->responseEmitter->sender($response, $PsrResponse);
2021-09-24 17:46:20 +08:00
$this->eventDispatch->dispatch(new OnAfterRequest());
}
2021-08-31 14:01:18 +08:00
}
2021-08-12 12:40:06 +08:00
2021-09-18 11:44:42 +08:00
/**
* @param Handler $handler
* @param $PsrRequest
* @return ResponseInterface
* @throws Exception
*/
protected function handler(Handler $handler, $PsrRequest): \Psr\Http\Message\ResponseInterface
{
$middlewares = MiddlewareManager::get($handler->callback);
2021-09-24 18:16:39 +08:00
$dispatcher = new Dispatcher($handler, empty($middlewares) ? null : new Iterator($middlewares));
2021-09-18 11:44:42 +08:00
return $dispatcher->handle($PsrRequest);
}
2021-09-16 14:45:59 +08:00
/**
* @param Request $request
2021-09-18 11:44:42 +08:00
* @return array<ServerRequestInterface, ResponseInterface>
* @throws Exception
2021-09-16 14:45:59 +08:00
*/
2021-09-18 11:44:42 +08:00
private function initRequestResponse(Request $request): array
2021-09-16 14:45:59 +08:00
{
2021-09-20 03:37:39 +08:00
$PsrResponse = Context::setContext(ResponseInterface::class, new \Http\Message\Response());
2021-09-16 14:48:54 +08:00
2021-09-20 03:37:39 +08:00
$PsrRequest = Context::setContext(RequestInterface::class, ServerRequest::createServerRequest($request));
2021-09-24 17:46:20 +08:00
if ($PsrRequest->isMethod('OPTIONS')) {
$request->server['request_uri'] = '/*';
}
2021-09-18 11:44:42 +08:00
return [$PsrRequest, $PsrResponse];
2021-09-16 14:45:59 +08:00
}
2021-09-18 11:44:42 +08:00
2021-09-16 14:45:59 +08:00
2021-08-31 14:01:18 +08:00
/**
* @param Server $server
* @param int $fd
* @throws Exception
*/
public function onDisconnect(Server $server, int $fd): void
{
}
2021-08-28 01:57:33 +08:00
2021-08-31 14:01:18 +08:00
/**
* @param Server $server
* @param int $fd
* @throws Exception
*/
public function onClose(Server $server, int $fd): void
{
}
2021-08-12 12:40:06 +08:00
}