diff --git a/http-handler/Abstracts/Middleware.php b/http-handler/Abstracts/Middleware.php index 7e091375..0ef5e1d5 100644 --- a/http-handler/Abstracts/Middleware.php +++ b/http-handler/Abstracts/Middleware.php @@ -2,11 +2,19 @@ namespace Http\Handler\Abstracts; +use Annotation\Inject; use Psr\Http\Server\MiddlewareInterface; +use Server\Constrict\ResponseInterface; + +/** + * + */ abstract class Middleware implements MiddlewareInterface { + #[Inject(ResponseInterface::class)] + public ResponseInterface $response; } diff --git a/http-handler/CoreMiddleware.php b/http-handler/CoreMiddleware.php index 0de6485c..c31fc552 100644 --- a/http-handler/CoreMiddleware.php +++ b/http-handler/CoreMiddleware.php @@ -20,14 +20,9 @@ class CoreMiddleware extends Middleware */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { - // TODO: Implement process() method. - - \response()->withAccessControlAllowOrigin('*') + $this->response->withAccessControlAllowOrigin('*') ->withAccessControlRequestMethod($request->getAccessControlRequestMethod()) ->withAccessControlAllowHeaders($request->getAccessControlAllowHeaders()); - - var_dump(get_called_class()); - return $handler->handle($request); } diff --git a/http-server/Service/Http.php b/http-server/Service/Http.php index e58f010d..b8c9a4e8 100644 --- a/http-server/Service/Http.php +++ b/http-server/Service/Http.php @@ -5,15 +5,14 @@ namespace Server\Service; use Annotation\Inject; use Exception; -use Http\Handler\Context; use Http\Handler\Abstracts\HandlerManager; +use Http\Handler\Abstracts\MiddlewareManager; +use Http\Handler\Context; use Http\Handler\Dispatcher; use Http\Handler\Handler; use Http\Handler\Router; -use Http\Message\ContentType; use Http\Message\ServerRequest; use Http\Message\Stream; -use Http\Handler\Abstracts\MiddlewareManager; use Kiri\Abstracts\Config; use Kiri\Exception\ConfigException; use Kiri\Kiri; @@ -39,32 +38,32 @@ use Swoole\Server; class Http implements OnCloseInterface, OnConnectInterface, OnRequestInterface { - use EventDispatchHelper; - use ResponseHelper; + use EventDispatchHelper; + use ResponseHelper; - /** @var Router|mixed */ - #[Inject(Router::class)] - public Router $router; + /** @var Router|mixed */ + #[Inject(Router::class)] + public Router $router; - /** - * @var ExceptionHandlerInterface - */ - public ExceptionHandlerInterface $exceptionHandler; + /** + * @var ExceptionHandlerInterface + */ + public ExceptionHandlerInterface $exceptionHandler; - /** - * @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); - } + /** + * @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); + } /** @@ -85,7 +84,7 @@ class Http implements OnCloseInterface, OnConnectInterface, OnRequestInterface public function onRequest(Request $request, Response $response): void { try { - [$PsrRequest, $PsrResponse] = $this->initRequestResponse($request); + [$PsrRequest, $PsrResponse] = $this->initRequestResponse($request); /** @var Handler $handler */ $handler = HandlerManager::get($request->server['request_uri'], $request->getMethod()); if (is_integer($handler)) { @@ -96,11 +95,11 @@ class Http implements OnCloseInterface, OnConnectInterface, OnRequestInterface $PsrResponse = $this->handler($handler, $PsrRequest); } } catch (\Throwable $throwable) { - $PsrResponse= $this->exceptionHandler->emit($throwable, $this->response); + $PsrResponse = $this->exceptionHandler->emit($throwable, $this->response); } finally { $this->responseEmitter->sender($response, $PsrResponse); - $this->eventDispatch->dispatch(new OnAfterRequest()); - } + $this->eventDispatch->dispatch(new OnAfterRequest()); + } } @@ -130,12 +129,13 @@ class Http implements OnCloseInterface, OnConnectInterface, OnRequestInterface $PsrResponse = Context::setContext(ResponseInterface::class, new \Http\Message\Response()); $PsrRequest = Context::setContext(RequestInterface::class, ServerRequest::createServerRequest($request)); - + if ($PsrRequest->isMethod('OPTIONS')) { + $request->server['request_uri'] = '/*'; + } return [$PsrRequest, $PsrResponse]; } - /** * @param Server $server * @param int $fd