This commit is contained in:
2021-09-09 17:33:43 +08:00
parent 6c133983fb
commit e53b060022
3 changed files with 35 additions and 31 deletions
+3 -2
View File
@@ -16,6 +16,7 @@ use Kiri\Exception\NotFindClassException;
use Kiri\IAspect; use Kiri\IAspect;
use Kiri\Kiri; use Kiri\Kiri;
use ReflectionException; use ReflectionException;
use Server\Constant;
use Server\Events\OnAfterWorkerStart; use Server\Events\OnAfterWorkerStart;
use Server\RequestInterface; use Server\RequestInterface;
@@ -322,11 +323,11 @@ class Node
public function dispatch(RequestInterface $request): mixed public function dispatch(RequestInterface $request): mixed
{ {
if (!in_array($request->getMethod(), $this->method)) { if (!in_array($request->getMethod(), $this->method)) {
throw new RequestException('<h2>HTTP 405 Method allow</h2><hr><i>Powered by Swoole</i>', 405); throw new RequestException(Constant::STATUS_405_MESSAGE, 405);
} }
$handlerProviders = HandlerProviders::get($this->sourcePath, $request->getMethod()); $handlerProviders = HandlerProviders::get($this->sourcePath, $request->getMethod());
if (empty($handlerProviders)) { if (empty($handlerProviders)) {
throw new RequestException('<h2>HTTP 404 Not Found</h2><hr><i>Powered by Swoole</i>', 404); throw new RequestException(Constant::STATUS_404_MESSAGE, 404);
} }
return $handlerProviders->interpreter($request); return $handlerProviders->interpreter($request);
} }
+4 -3
View File
@@ -34,13 +34,14 @@ class Constant
const AFTER_RELOAD = 'AfterReload'; const AFTER_RELOAD = 'AfterReload';
const SERVER_TYPE_HTTP = 'http'; const SERVER_TYPE_HTTP = 'http';
const SERVER_TYPE_WEBSOCKET = 'ws'; const SERVER_TYPE_WEBSOCKET = 'ws';
const SERVER_TYPE_TCP = 'tcp'; const SERVER_TYPE_TCP = 'tcp';
const SERVER_TYPE_UDP = 'udp'; const SERVER_TYPE_UDP = 'udp';
const SERVER_TYPE_BASE = 'base'; const SERVER_TYPE_BASE = 'base';
const STATUS_404_MESSAGE = '<h2>HTTP 404 Not Found</h2><hr><i>Powered by Swoole</i>';
const STATUS_405_MESSAGE = '<h2>HTTP 405 Method allow</h2><hr><i>Powered by Swoole</i>';
} }
+4 -2
View File
@@ -7,6 +7,7 @@ use Exception;
use Http\Exception\RequestException; use Http\Exception\RequestException;
use Http\Route\Node; use Http\Route\Node;
use Kiri\Core\Help; use Kiri\Core\Help;
use Server\Constant;
use Server\Events\OnAfterRequest; use Server\Events\OnAfterRequest;
use Server\Message\Response as MsgResponse; use Server\Message\Response as MsgResponse;
use Server\RequestInterface; use Server\RequestInterface;
@@ -46,9 +47,10 @@ class Http extends \Server\Abstracts\Http implements OnClose, OnConnect
/** @var RequestInterface $request */ /** @var RequestInterface $request */
$node = $this->router->Branch_search($request); $node = $this->router->Branch_search($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(Constant::STATUS_404_MESSAGE, 404);
} }
if (!(($psr7Response = $node->dispatch($request)) instanceof ResponseInterface)) { $psr7Response = $node->dispatch($request);
if (!($psr7Response instanceof ResponseInterface)) {
$psr7Response = $this->transferToResponse($psr7Response); $psr7Response = $this->transferToResponse($psr7Response);
} }
} catch (Error | \Throwable $exception) { } catch (Error | \Throwable $exception) {