Skip to content

Commit

Permalink
[8.x] Allow sending a refresh header with maintenance mode response (#…
Browse files Browse the repository at this point in the history
…37385)

* allow sending a refresh header with maintenance mode response

* Update PreventRequestsDuringMaintenance.php

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
themsaid and taylorotwell authored May 17, 2021
1 parent ac2a366 commit 8030a1b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
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

0 comments on commit 8030a1b

Please sign in to comment.