From 42c5a60c2944d9f509095bca0849fdb7803ae5b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 5 Sep 2024 16:10:05 +0200 Subject: [PATCH] fix: Force 503 HTTP status code for maintenance mode on v1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- lib/private/OCS/ApiHelper.php | 5 +++-- ocs/v1.php | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/private/OCS/ApiHelper.php b/lib/private/OCS/ApiHelper.php index 04d6983db98..4679b4da864 100644 --- a/lib/private/OCS/ApiHelper.php +++ b/lib/private/OCS/ApiHelper.php @@ -21,8 +21,9 @@ class ApiHelper { /** * Respond to a call * @psalm-taint-escape html + * @param int $httpStatusCode force the HTTP status code, only used for the special case of maintenance mode which return 503 even for v1 */ - public static function respond(int $statusCode, string $statusMessage, array $headers = []): void { + public static function respond(int $statusCode, string $statusMessage, array $headers = [], ?int $httpStatusCode = null): void { $request = Server::get(IRequest::class); $format = $request->getParam('format', 'xml'); if (self::isV2($request)) { @@ -46,7 +47,7 @@ class ApiHelper { header($name . ': ' . $value); } - http_response_code($response->getStatus()); + http_response_code($httpStatusCode ?? $response->getStatus()); self::setContentType($format); $body = $response->render(); diff --git a/ocs/v1.php b/ocs/v1.php index 7aad1526d22..90d7971c549 100644 --- a/ocs/v1.php +++ b/ocs/v1.php @@ -24,7 +24,7 @@ if (Util::needUpgrade() || \OC::$server->getConfig()->getSystemValueBool('maintenance')) { // since the behavior of apps or remotes are unpredictable during // an upgrade, return a 503 directly - ApiHelper::respond(503, 'Service unavailable', ['X-Nextcloud-Maintenance-Mode' => '1']); + ApiHelper::respond(503, 'Service unavailable', ['X-Nextcloud-Maintenance-Mode' => '1'], 503); exit; }