From 5ff6cb937afb8699358b9738e8bce414df673022 Mon Sep 17 00:00:00 2001 From: sunagne <583985775@qq.com> Date: Wed, 20 Sep 2023 11:41:42 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=AC:=20getMethodNotAllowedFallback?= =?UTF-8?q?=E5=AF=B9405=20Method=20Not=20Allowed=20=E7=9A=84=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.php | 34 +++++++++++++++++++++++++++++++++- src/Route.php | 26 ++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/src/App.php b/src/App.php index ce51b8c..1751e0a 100644 --- a/src/App.php +++ b/src/App.php @@ -109,6 +109,15 @@ class App */ protected static $requestClass = ''; + /** + * @var bool + */ + protected static $methodNotAllowed = false; + /** + * @var string + */ + public static $allowedMethods = ''; + /** * App constructor. * @param string $requestClass @@ -154,7 +163,7 @@ public function onMessage($connection, $request) $plugin = $controllerAndAction['plugin'] ?? static::getPluginByPath($path); if (!$controllerAndAction || Route::hasDisableDefaultRoute($plugin)) { $request->plugin = $plugin; - $callback = static::getFallback($plugin); + $callback = self::$methodNotAllowed ? static::getMethodNotAllowedFallback($plugin) : static::getFallback($plugin); $request->app = $request->controller = $request->action = ''; static::send($connection, $callback($request), $request); return null; @@ -238,6 +247,24 @@ protected static function getFallback(string $plugin = ''): Closure }; } + /** + * GetMethodNotAllowedFallback. + * @param string $plugin + * @return Closure + */ + protected static function getMethodNotAllowedFallback(string $plugin = ''): Closure + { + + return Route::getMethodNotAllowedFallback($plugin) ?: function () { + try { + $notFoundContent = file_get_contents(static::$publicPath . '/405.html'); + } catch (Throwable $e) { + $notFoundContent = '405 Method Not Allowed'; + } + return new Response(405, [], $notFoundContent); + }; + } + /** * ExceptionResponse. * @param Throwable $e @@ -560,6 +587,11 @@ protected static function findRoute(TcpConnection $connection, string $path, str static::send($connection, $callback($request), $request); return true; } + + if ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED and !in_array('OPTIONS' , $routeInfo[1])){ + static::$methodNotAllowed = true; + static::$allowedMethods = implode(',' , $routeInfo[1] ?? []); + } return false; } diff --git a/src/Route.php b/src/Route.php index 87d7a70..cce7dee 100644 --- a/src/Route.php +++ b/src/Route.php @@ -61,6 +61,11 @@ class Route */ protected static $fallback = []; + /** + * @var null|callable + */ + protected static $methodNotAllowedFallback = []; + /** * @var array */ @@ -458,6 +463,27 @@ public static function getFallback(string $plugin = ''): ?callable return static::$fallback[$plugin] ?? null; } + /** + * methodNotAllowedFallback. + * @param callable|mixed $callback + * @param string $plugin + * @return void + */ + public static function methodNotAllowedFallback(callable $callback, string $plugin = '') + { + static::$methodNotAllowedFallback[$plugin] = $callback; + } + + /** + * GetMethodNotAllowedFallback. + * @param string $plugin + * @return callable|null + */ + public static function getMethodNotAllowedFallback(string $plugin = ''): ?callable + { + return static::$methodNotAllowedFallback[$plugin] ?? null; + } + /** * @return void * @deprecated From 58815cf69dcc4895a88d5cf597a113045c05f214 Mon Sep 17 00:00:00 2001 From: sunagne <583985775@qq.com> Date: Wed, 20 Sep 2023 13:49:45 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=90=AC:=20=E4=BC=98=E5=8C=96=E5=B9=B6?= =?UTF-8?q?=E5=9C=A8=E7=94=9F=E5=91=BD=E5=91=A8=E6=9C=9F=E7=BB=93=E6=9D=9F?= =?UTF-8?q?=E5=89=8D=E9=87=8D=E7=BD=AE$methodNotAllowed=3DFALSE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/App.php b/src/App.php index 1751e0a..13572bc 100644 --- a/src/App.php +++ b/src/App.php @@ -163,9 +163,10 @@ public function onMessage($connection, $request) $plugin = $controllerAndAction['plugin'] ?? static::getPluginByPath($path); if (!$controllerAndAction || Route::hasDisableDefaultRoute($plugin)) { $request->plugin = $plugin; - $callback = self::$methodNotAllowed ? static::getMethodNotAllowedFallback($plugin) : static::getFallback($plugin); + $callback = static::$methodNotAllowed ? static::getMethodNotAllowedFallback($plugin) : static::getFallback($plugin); $request->app = $request->controller = $request->action = ''; static::send($connection, $callback($request), $request); + static::$methodNotAllowed = false; return null; } $app = $controllerAndAction['app']; @@ -588,7 +589,10 @@ protected static function findRoute(TcpConnection $connection, string $path, str return true; } - if ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED and !in_array('OPTIONS' , $routeInfo[1])){ + if ( + $routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED and + count(array_diff($routeInfo[1], ['OPTIONS'])) > 0 + ){ static::$methodNotAllowed = true; static::$allowedMethods = implode(',' , $routeInfo[1] ?? []); }