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\Kiri;
use ReflectionException;
use Server\Constant;
use Server\Events\OnAfterWorkerStart;
use Server\RequestInterface;
@@ -322,11 +323,11 @@ class Node
public function dispatch(RequestInterface $request): mixed
{
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());
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);
}
+28 -27
View File
@@ -11,36 +11,37 @@ namespace Server;
class Constant
{
const START = 'Start';
const SHUTDOWN = 'Shutdown';
const WORKER_START = 'WorkerStart';
const WORKER_STOP = 'WorkerStop';
const WORKER_EXIT = 'WorkerExit';
const CONNECT = 'Connect';
const HANDSHAKE = 'handshake';
const DISCONNECT = 'disconnect';
const MESSAGE = 'message';
const RECEIVE = 'Receive';
const PACKET = 'Packet';
const REQUEST = 'request';
const CLOSE = 'Close';
const TASK = 'Task';
const FINISH = 'Finish';
const PIPE_MESSAGE = 'PipeMessage';
const WORKER_ERROR = 'WorkerError';
const MANAGER_START = 'ManagerStart';
const MANAGER_STOP = 'ManagerStop';
const BEFORE_RELOAD = 'BeforeReload';
const AFTER_RELOAD = 'AfterReload';
const START = 'Start';
const SHUTDOWN = 'Shutdown';
const WORKER_START = 'WorkerStart';
const WORKER_STOP = 'WorkerStop';
const WORKER_EXIT = 'WorkerExit';
const CONNECT = 'Connect';
const HANDSHAKE = 'handshake';
const DISCONNECT = 'disconnect';
const MESSAGE = 'message';
const RECEIVE = 'Receive';
const PACKET = 'Packet';
const REQUEST = 'request';
const CLOSE = 'Close';
const TASK = 'Task';
const FINISH = 'Finish';
const PIPE_MESSAGE = 'PipeMessage';
const WORKER_ERROR = 'WorkerError';
const MANAGER_START = 'ManagerStart';
const MANAGER_STOP = 'ManagerStop';
const BEFORE_RELOAD = 'BeforeReload';
const AFTER_RELOAD = 'AfterReload';
const SERVER_TYPE_HTTP = 'http';
const SERVER_TYPE_WEBSOCKET = 'ws';
const SERVER_TYPE_TCP = 'tcp';
const SERVER_TYPE_UDP = 'udp';
const SERVER_TYPE_BASE = 'base';
const SERVER_TYPE_HTTP = 'http';
const SERVER_TYPE_WEBSOCKET = 'ws';
const SERVER_TYPE_TCP = 'tcp';
const SERVER_TYPE_UDP = 'udp';
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\Route\Node;
use Kiri\Core\Help;
use Server\Constant;
use Server\Events\OnAfterRequest;
use Server\Message\Response as MsgResponse;
use Server\RequestInterface;
@@ -46,9 +47,10 @@ class Http extends \Server\Abstracts\Http implements OnClose, OnConnect
/** @var RequestInterface $request */
$node = $this->router->Branch_search($request);
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);
}
} catch (Error | \Throwable $exception) {