Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Allow sending a refresh header with maintenance mode response #37385

Merged
merged 2 commits into from
May 17, 2021
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
2 changes: 2 additions & 0 deletions src/Illuminate/Foundation/Console/DownCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class DownCommand extends Command
protected $signature = 'down {--redirect= : The path that users should be redirected to}
{--render= : The view that should be prerendered for display during maintenance mode}
{--retry= : The number of seconds after which the request may be retried}
{--refresh= : The number of seconds after which the browser may refresh}
{--secret= : The secret phrase that may be used to bypass maintenance mode}
{--status=503 : The status code that should be used when returning the maintenance mode response}';

Expand Down Expand Up @@ -71,6 +72,7 @@ protected function getDownFilePayload()
return [
'redirect' => $this->redirectPath(),
'retry' => $this->getRetryTime(),
'refresh' => $this->option('refresh'),
'secret' => $this->option('secret'),
'status' => (int) $this->option('status', 503),
'template' => $this->option('render') ? $this->prerenderView() : null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ public function handle($request, Closure $next)
return response(
$data['template'],
$data['status'] ?? 503,
isset($data['retry']) ? ['Retry-After' => $data['retry']] : []
$this->getHeaders($data)
);
}

throw new HttpException(
$data['status'] ?? 503,
'Service Unavailable',
null,
isset($data['retry']) ? ['Retry-After' => $data['retry']] : []
$this->getHeaders($data)
);
}

Expand Down Expand Up @@ -136,4 +136,21 @@ protected function bypassResponse(string $secret)
MaintenanceModeBypassCookie::create($secret)
);
}

/**
* Get the headers that should be sent with the response.
*
* @param array $data
* @return array
*/
protected function getHeaders($data)
{
$headers = isset($data['retry']) ? ['Retry-After' => $data['retry']] : [];

if (isset($data['refresh'])) {
$headers['Refresh'] = $data['refresh'];
}

return $headers;
}
}
2 changes: 2 additions & 0 deletions tests/Integration/Foundation/MaintenanceModeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function testBasicMaintenanceModeResponse()
{
file_put_contents(storage_path('framework/down'), json_encode([
'retry' => 60,
'refresh' => 60,
]));

Route::get('/foo', function () {
Expand All @@ -33,6 +34,7 @@ public function testBasicMaintenanceModeResponse()

$response->assertStatus(503);
$response->assertHeader('Retry-After', '60');
$response->assertHeader('Refresh', '60');
}

public function testMaintenanceModeCanHaveCustomStatus()
Expand Down