This commit is contained in:
2021-07-21 18:50:40 +08:00
parent b99551e326
commit 1e6333a18e
3 changed files with 22 additions and 16 deletions
+5 -5
View File
@@ -16,7 +16,6 @@ use Snowflake\Snowflake;
use Swoole\Error; use Swoole\Error;
use Swoole\Http\Request; use Swoole\Http\Request;
use Swoole\Http\Response; use Swoole\Http\Response;
use Swoole\Http\Status;
use Throwable; use Throwable;
/** /**
@@ -49,7 +48,7 @@ class OnRequest extends Callback
* @return void * @return void
* @throws Exception * @throws Exception
*/ */
public function onHandler(Request $request, Response $response): mixed public function onHandler(Request $request, Response $response): void
{ {
try { try {
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES)); defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
@@ -58,12 +57,13 @@ class OnRequest extends Callback
[$request, $response] = OnRequest::createContext($request, $response); [$request, $response] = OnRequest::createContext($request, $response);
if ($request->is('favicon.ico')) { if ($request->is('favicon.ico')) {
return $response->close(404); $response->close(404);
} else {
$this->router->dispatch();
} }
return $this->router->dispatch();
} catch (ExitException | Error | Throwable $exception) { } catch (ExitException | Error | Throwable $exception) {
$this->addError($exception, 'throwable'); $this->addError($exception, 'throwable');
return $this->sendErrorMessage($request, $response, $exception); $this->sendErrorMessage($request, $response, $exception);
} }
} }
+2 -4
View File
@@ -82,7 +82,6 @@ class Request extends HttpService
$this->fd = $fd; $this->fd = $fd;
} }
/** /**
* @return array|null * @return array|null
* @throws Exception * @throws Exception
@@ -243,12 +242,11 @@ class Request extends HttpService
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
public function adapter(): mixed public function adapter(): void
{ {
if (!$this->isHead()) { if (!$this->isHead()) {
return router()->dispatch(); router()->dispatch();
} }
return '';
} }
+14 -6
View File
@@ -8,6 +8,7 @@ use Exception;
use HttpServer\Abstracts\HttpService; use HttpServer\Abstracts\HttpService;
use HttpServer\Controller; use HttpServer\Controller;
use HttpServer\Http\Request; use HttpServer\Http\Request;
use HttpServer\Http\Response;
use HttpServer\IInterface\Middleware; use HttpServer\IInterface\Middleware;
use HttpServer\IInterface\RouterInterface; use HttpServer\IInterface\RouterInterface;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
@@ -43,6 +44,9 @@ class Router extends HttpService implements RouterInterface
public int $useTree = ROUTER_TREE; public int $useTree = ROUTER_TREE;
public ?Response $response = null;
/** /**
* @param Closure $middleware * @param Closure $middleware
*/ */
@@ -54,11 +58,14 @@ class Router extends HttpService implements RouterInterface
/** /**
* @throws ConfigException * @throws ConfigException
* @throws Exception
* 初始化函数路径 * 初始化函数路径
*/ */
public function init() public function init()
{ {
$this->dir = Config::get('http.namespace', $this->dir); $this->dir = Config::get('http.namespace', $this->dir);
$this->response = Snowflake::app()->get('response');
} }
@@ -513,17 +520,18 @@ class Router extends HttpService implements RouterInterface
* @return mixed * @return mixed
* @throws * @throws
*/ */
public function dispatch(): mixed public function dispatch(): void
{ {
$node = $this->find_path(\request()); $node = $this->find_path(\request());
if (!($node instanceof Node)) { if (!($node instanceof Node)) {
return send(\request()->getUri() . ' -> ' . self::NOT_FOUND); $this->response->setFormat(Response::HTML);
$this->response->send('<h1>404</h1>');
} else {
$this->response->send(($response = $node->dispatch()), 200);
if ($node->hasAfter()) {
$node->afterDispatch($response);
} }
send(($response = $node->dispatch()), 200);
if (!$node->hasAfter()) {
return null;
} }
return $node->afterDispatch($response);
} }