Skip to content

Commit

Permalink
[8.x] Passthrough excluded uri's in maintenance mode (#38041)
Browse files Browse the repository at this point in the history
* Passthrough excluded uri's in maintenance mode

* Apply fixes from StyleCI (#38040)

* wildcard paths

* formatting

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
driesvints and taylorotwell authored Jul 18, 2021
1 parent c5ee631 commit 8f3b411
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Illuminate/Foundation/Console/DownCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -69,6 +71,7 @@ public function handle()
protected function getDownFilePayload()
{
return [
'except' => $this->excludedPaths(),
'redirect' => $this->redirectPath(),
'retry' => $this->getRetryTime(),
'refresh' => $this->option('refresh'),
Expand All @@ -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.
*
Expand Down
23 changes: 23 additions & 0 deletions src/Illuminate/Foundation/Console/stubs/maintenance-mode.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 8f3b411

Please sign in to comment.