From 1c5c7a3c92b7f531916c4995697267888ad040b5 Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Mon, 17 May 2021 11:08:59 +0200 Subject: [PATCH 1/2] allow sending a refresh header with maintenance mode response --- .../Foundation/Console/DownCommand.php | 2 ++ .../PreventRequestsDuringMaintenance.php | 21 +++++++++++++++++-- .../Foundation/MaintenanceModeTest.php | 2 ++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Foundation/Console/DownCommand.php b/src/Illuminate/Foundation/Console/DownCommand.php index feaf95047078..a114451841ad 100644 --- a/src/Illuminate/Foundation/Console/DownCommand.php +++ b/src/Illuminate/Foundation/Console/DownCommand.php @@ -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}'; @@ -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, diff --git a/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php b/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php index 831468281fbc..90a3ffbc03ba 100644 --- a/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php +++ b/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php @@ -71,7 +71,7 @@ public function handle($request, Closure $next) return response( $data['template'], $data['status'] ?? 503, - isset($data['retry']) ? ['Retry-After' => $data['retry']] : [] + $this->getHeaders($data) ); } @@ -79,7 +79,7 @@ public function handle($request, Closure $next) $data['status'] ?? 503, 'Service Unavailable', null, - isset($data['retry']) ? ['Retry-After' => $data['retry']] : [] + $this->getHeaders($data) ); } @@ -136,4 +136,21 @@ protected function bypassResponse(string $secret) MaintenanceModeBypassCookie::create($secret) ); } + + /** + * Determine the response headers. + * + * @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; + } } diff --git a/tests/Integration/Foundation/MaintenanceModeTest.php b/tests/Integration/Foundation/MaintenanceModeTest.php index ff93fe41a492..0fda6e3b5ab9 100644 --- a/tests/Integration/Foundation/MaintenanceModeTest.php +++ b/tests/Integration/Foundation/MaintenanceModeTest.php @@ -23,6 +23,7 @@ public function testBasicMaintenanceModeResponse() { file_put_contents(storage_path('framework/down'), json_encode([ 'retry' => 60, + 'refresh' => 60, ])); Route::get('/foo', function () { @@ -33,6 +34,7 @@ public function testBasicMaintenanceModeResponse() $response->assertStatus(503); $response->assertHeader('Retry-After', '60'); + $response->assertHeader('Refresh', '60'); } public function testMaintenanceModeCanHaveCustomStatus() From 3d307773f63254562b875c16b4ce92cd79227b2c Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 17 May 2021 08:18:38 -0500 Subject: [PATCH 2/2] Update PreventRequestsDuringMaintenance.php --- .../Http/Middleware/PreventRequestsDuringMaintenance.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php b/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php index 90a3ffbc03ba..c65629d0aeee 100644 --- a/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php +++ b/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php @@ -138,7 +138,7 @@ protected function bypassResponse(string $secret) } /** - * Determine the response headers. + * Get the headers that should be sent with the response. * * @param array $data * @return array