From 8f3b411969ffdd733a0ba54098e08f8ca7c1369e Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Sun, 18 Jul 2021 17:57:36 +0200 Subject: [PATCH] [8.x] Passthrough excluded uri's in maintenance mode (#38041) * Passthrough excluded uri's in maintenance mode * Apply fixes from StyleCI (#38040) * wildcard paths * formatting Co-authored-by: Taylor Otwell --- .../Foundation/Console/DownCommand.php | 17 ++++++++++++++ .../Console/stubs/maintenance-mode.stub | 23 +++++++++++++++++++ .../PreventRequestsDuringMaintenance.php | 10 ++++++++ 3 files changed, 50 insertions(+) diff --git a/src/Illuminate/Foundation/Console/DownCommand.php b/src/Illuminate/Foundation/Console/DownCommand.php index 28586e003350..c798b0d5d7a2 100644 --- a/src/Illuminate/Foundation/Console/DownCommand.php +++ b/src/Illuminate/Foundation/Console/DownCommand.php @@ -2,9 +2,11 @@ namespace Illuminate\Foundation\Console; +use App\Http\Middleware\PreventRequestsDuringMaintenance; use Exception; use Illuminate\Console\Command; use Illuminate\Foundation\Exceptions\RegisterErrorViewPaths; +use Throwable; class DownCommand extends Command { @@ -69,6 +71,7 @@ public function handle() protected function getDownFilePayload() { return [ + 'except' => $this->excludedPaths(), 'redirect' => $this->redirectPath(), 'retry' => $this->getRetryTime(), 'refresh' => $this->option('refresh'), @@ -78,6 +81,20 @@ protected function getDownFilePayload() ]; } + /** + * Get the paths that should be excluded from maintenance mode. + * + * @return array + */ + protected function excludedPaths() + { + try { + return $this->laravel->make(PreventRequestsDuringMaintenance::class)->getExcludedPaths(); + } catch (Throwable $e) { + return []; + } + } + /** * Get the path that users should be redirected to. * diff --git a/src/Illuminate/Foundation/Console/stubs/maintenance-mode.stub b/src/Illuminate/Foundation/Console/stubs/maintenance-mode.stub index 18189ebd1b4e..d3d7e148444e 100644 --- a/src/Illuminate/Foundation/Console/stubs/maintenance-mode.stub +++ b/src/Illuminate/Foundation/Console/stubs/maintenance-mode.stub @@ -13,6 +13,29 @@ if (! isset($data['template'])) { return; } +// Allow framework to handle request if request URI is in the exclude list... +if (isset($data['except'])) { + $uri = parse_url($_SERVER['REQUEST_URI'])['path']; + + $uri = rawurldecode($uri !== '/' ? trim($uri, '/') : $uri); + + foreach ((array) $data['except'] as $except) { + $except = $except !== '/' ? trim($except, '/') : $except; + + if ($except == $uri) { + return; + } + + $except = preg_quote($except, '#'); + + $except = str_replace('\*', '.*', $except); + + if (preg_match('#^'.$except.'\z#u', $uri) === 1) { + return; + } + } +} + // Allow framework to handle maintenance mode bypass route... if (isset($data['secret']) && $_SERVER['REQUEST_URI'] === '/'.$data['secret']) { return; diff --git a/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php b/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php index c65629d0aeee..a8692bc4f7e3 100644 --- a/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php +++ b/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php @@ -153,4 +153,14 @@ protected function getHeaders($data) return $headers; } + + /** + * Get the URIs that should be accessible even when maintenance mode is enabled. + * + * @return array + */ + public function getExcludedPaths() + { + return $this->except; + } }