Skip to content

Commit 79d9841

Browse files
committed
Replace hardcoded status headers with calls to http_response_code()
Signed-off-by: Morris Jobke <hey@morrisjobke.de>
1 parent d9d557a commit 79d9841

File tree

12 files changed

+29
-83
lines changed

12 files changed

+29
-83
lines changed

apps/files/ajax/list.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
try {
3939
$dirInfo = \OC\Files\Filesystem::getFileInfo($dir);
4040
if (!$dirInfo || !$dirInfo->getType() === 'dir') {
41-
header("HTTP/1.0 404 Not Found");
41+
http_response_code(404);
4242
exit();
4343
}
4444

apps/files_sharing/public.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,13 @@
3030
if($token !== '') {
3131
$protocol = \OC::$server->getRequest()->getHttpProtocol();
3232
if ($protocol == 'HTTP/1.0') {
33-
$status = '302 Found';
33+
http_response_code(302);
3434
} else {
35-
$status = '307 Temporary Redirect';
35+
http_response_code(307);
3636
}
37-
header($protocol.' ' . $status);
3837
header('Location: ' . $urlGenerator->linkToRoute($route, array('token' => $token)));
3938
} else {
40-
header('HTTP/1.0 404 Not Found');
39+
http_response_code(404);
4140
$tmpl = new OCP\Template('', '404', 'guest');
4241
print_unescaped($tmpl->fetchPage());
4342
}

apps/files_trashbin/ajax/list.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
try {
3535
$files = \OCA\Files_Trashbin\Helper::getTrashFiles($dir, \OCP\User::getUser(), $sortAttribute, $sortDirection);
3636
} catch (Exception $e) {
37-
header("HTTP/1.0 404 Not Found");
37+
http_response_code(404);
3838
exit();
3939
}
4040

apps/files_versions/download.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
try {
3535
list($uid, $filename) = OCA\Files_Versions\Storage::getUidAndFilename($file);
3636
} catch(\OCP\Files\NotFoundException $e) {
37-
header("HTTP/1.1 404 Not Found");
37+
http_response_code(404);
3838
$tmpl = new OCP\Template('', '404', 'guest');
3939
$tmpl->assign('file', '');
4040
$tmpl->printPage();

index.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,32 +54,20 @@
5454
\OC::$server->getLogger()->logException($ex2, array('app' => 'index'));
5555

5656
//show the user a detailed error page
57-
OC_Template::printExceptionErrorPage($ex, \OC_Response::STATUS_INTERNAL_SERVER_ERROR);
57+
OC_Template::printExceptionErrorPage($ex, 500);
5858
}
5959
} catch (\OC\User\LoginException $ex) {
6060
OC_Template::printErrorPage($ex->getMessage(), $ex->getMessage(), OC_Response::STATUS_FORBIDDEN);
6161
} catch (Exception $ex) {
6262
\OC::$server->getLogger()->logException($ex, array('app' => 'index'));
6363

6464
//show the user a detailed error page
65-
OC_Response::setStatus(OC_Response::STATUS_INTERNAL_SERVER_ERROR);
66-
OC_Template::printExceptionErrorPage($ex);
65+
OC_Template::printExceptionErrorPage($ex, 500);
6766
} catch (Error $ex) {
6867
try {
6968
\OC::$server->getLogger()->logException($ex, array('app' => 'index'));
7069
} catch (Error $e) {
71-
72-
$claimedProtocol = strtoupper($_SERVER['SERVER_PROTOCOL']);
73-
$validProtocols = [
74-
'HTTP/1.0',
75-
'HTTP/1.1',
76-
'HTTP/2',
77-
];
78-
$protocol = 'HTTP/1.1';
79-
if(in_array($claimedProtocol, $validProtocols, true)) {
80-
$protocol = $claimedProtocol;
81-
}
82-
header($protocol . ' 500 Internal Server Error');
70+
http_response_code(500);
8371
header('Content-Type: text/plain; charset=utf-8');
8472
print("Internal Server Error\n\n");
8573
print("The server encountered an internal error and was unable to complete your request.\n");

lib/base.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,7 @@ public static function checkMaintenanceMode() {
287287
// Allow ajax update script to execute without being stopped
288288
if (\OC::$server->getSystemConfig()->getValue('maintenance', false) && OC::$SUBURI != '/core/ajax/update.php') {
289289
// send http status 503
290-
header('HTTP/1.1 503 Service Temporarily Unavailable');
291-
header('Status: 503 Service Temporarily Unavailable');
290+
http_response_code(503);
292291
header('Retry-After: 120');
293292

294293
// render error page
@@ -344,8 +343,7 @@ private static function printUpgradePage(\OC\SystemConfig $systemConfig) {
344343

345344
if ($disableWebUpdater || ($tooBig && !$ignoreTooBigWarning)) {
346345
// send http status 503
347-
header('HTTP/1.1 503 Service Temporarily Unavailable');
348-
header('Status: 503 Service Temporarily Unavailable');
346+
http_response_code(503);
349347
header('Retry-After: 120');
350348

351349
// render error page
@@ -600,9 +598,7 @@ public static function init() {
600598

601599
} catch (\RuntimeException $e) {
602600
if (!self::$CLI) {
603-
$claimedProtocol = strtoupper($_SERVER['SERVER_PROTOCOL']);
604-
$protocol = in_array($claimedProtocol, ['HTTP/1.0', 'HTTP/1.1', 'HTTP/2']) ? $claimedProtocol : 'HTTP/1.1';
605-
header($protocol . ' ' . OC_Response::STATUS_SERVICE_UNAVAILABLE);
601+
http_response_code(503);
606602
}
607603
// we can't use the template error page here, because this needs the
608604
// DI container which isn't available yet
@@ -689,7 +685,7 @@ public static function init() {
689685
}
690686
exit(1);
691687
} else {
692-
OC_Response::setStatus(OC_Response::STATUS_SERVICE_UNAVAILABLE);
688+
http_response_code(503);
693689
OC_Util::addStyle('guest');
694690
OC_Template::printGuestPage('', 'error', array('errors' => $errors));
695691
exit;
@@ -778,16 +774,14 @@ public static function init() {
778774
}
779775

780776
if(substr($request->getRequestUri(), -11) === '/status.php') {
781-
OC_Response::setStatus(\OC_Response::STATUS_BAD_REQUEST);
782-
header('Status: 400 Bad Request');
777+
http_response_code(400);
783778
header('Content-Type: application/json');
784779
echo '{"error": "Trusted domain error.", "code": 15}';
785780
exit();
786781
}
787782

788783
if (!$isScssRequest) {
789-
OC_Response::setStatus(\OC_Response::STATUS_BAD_REQUEST);
790-
header('Status: 400 Bad Request');
784+
http_response_code(400);
791785

792786
\OC::$server->getLogger()->info(
793787
'Trusted domain error. "{remoteAddress}" tried to access using "{host}" as host.',
@@ -997,7 +991,7 @@ public static function handleRequest() {
997991
} catch (Symfony\Component\Routing\Exception\ResourceNotFoundException $e) {
998992
//header('HTTP/1.0 404 Not Found');
999993
} catch (Symfony\Component\Routing\Exception\MethodNotAllowedException $e) {
1000-
OC_Response::setStatus(405);
994+
http_response_code(405);
1001995
return;
1002996
}
1003997
}
@@ -1007,8 +1001,7 @@ public static function handleRequest() {
10071001
// not allowed any more to prevent people
10081002
// mounting this root directly.
10091003
// Users need to mount remote.php/webdav instead.
1010-
header('HTTP/1.1 405 Method Not Allowed');
1011-
header('Status: 405 Method Not Allowed');
1004+
http_response_code(405);
10121005
return;
10131006
}
10141007

lib/private/legacy/api.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public static function respond($result, $format='xml') {
8888
} else {
8989
header('WWW-Authenticate: Basic realm="Authorisation Required"');
9090
}
91-
header('HTTP/1.0 401 Unauthorized');
91+
http_response_code(401);
9292
}
9393

9494
foreach($result->getHeaders() as $name => $value) {
@@ -101,7 +101,7 @@ public static function respond($result, $format='xml') {
101101
$statusCode = self::mapStatusCodes($result->getStatusCode());
102102
if (!is_null($statusCode)) {
103103
$meta['statuscode'] = $statusCode;
104-
OC_Response::setStatus($statusCode);
104+
http_response_code($statusCode);
105105
}
106106
}
107107

lib/private/legacy/files.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private static function sendHeaders($filename, $name, array $rangeArray) {
8383
$type = \OC::$server->getMimeTypeDetector()->getSecureMimeType(\OC\Files\Filesystem::getMimeType($filename));
8484
if ($fileSize > -1) {
8585
if (!empty($rangeArray)) {
86-
header('HTTP/1.1 206 Partial Content', true);
86+
http_response_code(206);
8787
header('Accept-Ranges: bytes', true);
8888
if (count($rangeArray) > 1) {
8989
$type = 'multipart/byteranges; boundary='.self::getBoundary();
@@ -286,12 +286,12 @@ private static function getSingleFile($view, $dir, $name, $params) {
286286
if (\OC\Files\Filesystem::isReadable($filename)) {
287287
self::sendHeaders($filename, $name, $rangeArray);
288288
} elseif (!\OC\Files\Filesystem::file_exists($filename)) {
289-
header("HTTP/1.1 404 Not Found");
289+
http_response_code(404);
290290
$tmpl = new OC_Template('', '404', 'guest');
291291
$tmpl->printPage();
292292
exit();
293293
} else {
294-
header("HTTP/1.1 403 Forbidden");
294+
http_response_code(403);
295295
die('403 Forbidden');
296296
}
297297
if (isset($params['head']) && $params['head']) {
@@ -321,7 +321,7 @@ private static function getSingleFile($view, $dir, $name, $params) {
321321
// file is unseekable
322322
header_remove('Accept-Ranges');
323323
header_remove('Content-Range');
324-
header("HTTP/1.1 200 OK");
324+
http_response_code(200);
325325
self::sendHeaders($filename, $name, array());
326326
$view->readfile($filename);
327327
}

lib/private/legacy/response.php

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -40,40 +40,6 @@ class OC_Response {
4040
const STATUS_INTERNAL_SERVER_ERROR = 500;
4141
const STATUS_SERVICE_UNAVAILABLE = 503;
4242

43-
/**
44-
* Set response status
45-
* @param int $status a HTTP status code, see also the STATUS constants
46-
*/
47-
static public function setStatus($status) {
48-
$protocol = \OC::$server->getRequest()->getHttpProtocol();
49-
switch($status) {
50-
case self::STATUS_NOT_MODIFIED:
51-
$status = $status . ' Not Modified';
52-
break;
53-
case self::STATUS_TEMPORARY_REDIRECT:
54-
if ($protocol == 'HTTP/1.0') {
55-
$status = self::STATUS_FOUND;
56-
// fallthrough
57-
} else {
58-
$status = $status . ' Temporary Redirect';
59-
break;
60-
}
61-
case self::STATUS_FOUND;
62-
$status = $status . ' Found';
63-
break;
64-
case self::STATUS_NOT_FOUND;
65-
$status = $status . ' Not Found';
66-
break;
67-
case self::STATUS_INTERNAL_SERVER_ERROR;
68-
$status = $status . ' Internal Server Error';
69-
break;
70-
case self::STATUS_SERVICE_UNAVAILABLE;
71-
$status = $status . ' Service Unavailable';
72-
break;
73-
}
74-
header($protocol.' '.$status);
75-
}
76-
7743
/**
7844
* Sets the content disposition header (with possible workarounds)
7945
* @param string $filename file name

ocs/v1.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
|| \OC::$server->getSystemConfig()->getValue('maintenance', false)) {
3737
// since the behavior of apps or remotes are unpredictable during
3838
// an upgrade, return a 503 directly
39-
OC_Response::setStatus(OC_Response::STATUS_SERVICE_UNAVAILABLE);
39+
http_response_code(503);
4040
$response = new \OC\OCS\Result(null, OC_Response::STATUS_SERVICE_UNAVAILABLE, 'Service unavailable');
4141
OC_API::respond($response, OC_API::requestedFormat());
4242
exit;
@@ -65,7 +65,7 @@
6565
// Fall through the not found
6666
} catch (MethodNotAllowedException $e) {
6767
OC_API::setContentType();
68-
OC_Response::setStatus(405);
68+
http_response_code(405);
6969
exit();
7070
} catch (Exception $ex) {
7171
OC_API::respond($ex->getResult(), OC_API::requestedFormat());
@@ -89,7 +89,7 @@
8989
OC_API::respond(new \OC\OCS\Result(null, \OCP\API::RESPOND_NOT_FOUND, $txt), $format);
9090
} catch (MethodNotAllowedException $e) {
9191
OC_API::setContentType();
92-
OC_Response::setStatus(405);
92+
http_response_code(405);
9393
} catch (\OC\OCS\Exception $ex) {
9494
OC_API::respond($ex->getResult(), OC_API::requestedFormat());
9595
} catch (\OC\User\LoginException $e) {

0 commit comments

Comments
 (0)