Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/Illuminate/Foundation/Console/stubs/maintenance-mode.stub
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ if (! file_exists($down = __DIR__.'/down')) {
// Decode the "down" file's JSON...
$data = json_decode(file_get_contents($down), true);

$expectsJson = (function () {
$acceptable = array_map('trim', explode(',', $_SERVER['HTTP_ACCEPT'] ?? ''));

$firstAcceptable = strtolower(strtok($acceptable[0] ?? '', ';'));

if (str_contains($firstAcceptable, '/json') || str_contains($firstAcceptable, '+json')) {
return true;
}

return isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
strcasecmp($_SERVER['HTTP_X_REQUESTED_WITH'], 'XMLHttpRequest') === 0 &&
(! isset($_SERVER['HTTP_X_PJAX']) || strcasecmp($_SERVER['HTTP_X_PJAX'], 'true') !== 0) &&
in_array($firstAcceptable, ['', '*/*', '*'], true);
})();

// Allow framework to handle request if no prerendered template...
if (! isset($data['template'])) {
return;
Expand Down Expand Up @@ -54,6 +69,11 @@ if (isset($_COOKIE['laravel_maintenance']) && isset($data['secret'])) {
}
}

// Allow framework to handle requests expecting a JSON response...
if ($expectsJson) {
return;
}

// Redirect to the proper path if necessary...
if (isset($data['redirect']) && $_SERVER['REQUEST_URI'] !== $data['redirect']) {
http_response_code(302);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function handle($request, Closure $next)
return $next($request);
}

if (isset($data['redirect'])) {
if (isset($data['redirect']) && ! $request->expectsJson()) {
$path = $data['redirect'] === '/'
? $data['redirect']
: trim($data['redirect'], '/');
Expand All @@ -90,7 +90,7 @@ public function handle($request, Closure $next)
}
}

if (isset($data['template'])) {
if (isset($data['template']) && ! $request->expectsJson()) {
return response(
$data['template'],
$data['status'] ?? 503,
Expand Down
62 changes: 62 additions & 0 deletions tests/Integration/Foundation/MaintenanceModeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ protected function setUp(): void
{
$this->beforeApplicationDestroyed(function () {
@unlink(storage_path('framework/down'));
@unlink(storage_path('framework/maintenance.php'));
});

parent::setUp();
Expand Down Expand Up @@ -80,6 +81,67 @@ public function testMaintenanceModeCanHaveCustomTemplate()
$this->assertSame('Rendered Content', $response->original);
}

public function testMaintenanceModeDoesNotUseCustomTemplateForJsonRequests()
{
file_put_contents(storage_path('framework/down'), json_encode([
'retry' => 60,
'template' => 'Rendered Content',
]));

Route::get('/foo', function () {
return 'Hello World';
})->middleware(PreventRequestsDuringMaintenance::class);

$response = $this->getJson('/foo');

$response->assertStatus(503);
$response->assertHeader('Retry-After', '60');
$response->assertJson(['message' => 'Service Unavailable']);
}

public function testMaintenanceModeDoesNotRedirectJsonRequests()
{
file_put_contents(storage_path('framework/down'), json_encode([
'redirect' => '/maintenance',
]));

Route::get('/foo', function () {
return 'Hello World';
})->middleware(PreventRequestsDuringMaintenance::class);

$response = $this->getJson('/foo');

$response->assertStatus(503);
$response->assertJson(['message' => 'Service Unavailable']);
}

public function testPrerenderedMaintenanceFileAllowsJsonRequestsToReachFramework()
{
file_put_contents(storage_path('framework/down'), json_encode([
'template' => 'Rendered Content',
]));

file_put_contents(
storage_path('framework/maintenance.php'),
file_get_contents(__DIR__.'/../../../src/Illuminate/Foundation/Console/stubs/maintenance-mode.stub')
);

$server = $_SERVER;

try {
$_SERVER['REQUEST_URI'] = '/foo';
$_SERVER['HTTP_ACCEPT'] = 'application/json';

ob_start();
include storage_path('framework/maintenance.php');
$output = ob_get_clean();
} finally {
$_SERVER = $server;
}

$this->assertSame('', $output);
}

public function testMaintenanceModeCanRedirectWithBypassCookie()
{
file_put_contents(storage_path('framework/down'), json_encode([
Expand Down
Loading