From 1dfce55965a8f6c8af9afe0215f9e77bca260928 Mon Sep 17 00:00:00 2001 From: Shreyas Sinha Date: Tue, 14 Feb 2017 17:50:13 +0530 Subject: [PATCH] Incorrect protocol number at 401/303 headers (#12920) Shared code for handling HTTP response codes and correctly set it based on PHP SAPI. Signed-off-by: Shreyas Sinha --- libraries/Response.php | 83 +++++++++++++++++++ libraries/plugins/auth/AuthenticationHttp.php | 5 +- setup/config.php | 9 +- setup/lib/form_processing.lib.php | 24 +----- test/PMATestCase.php | 11 ++- .../plugin/auth/AuthenticationHttpTest.php | 12 +-- test/libraries/PMA_Form_Processing_test.php | 7 +- 7 files changed, 114 insertions(+), 37 deletions(-) diff --git a/libraries/Response.php b/libraries/Response.php index 97ab19ab6d..845a03532d 100644 --- a/libraries/Response.php +++ b/libraries/Response.php @@ -456,4 +456,87 @@ class Response { return headers_sent(); } + + /** + * Wrapper around PHP's http_response_code() function. + * + * @param int $response_code will set the response code. + * + * @return void + */ + public function http_response_code($response_code) + { + http_response_code($response_code); + } + + /** + * Sets http response code. + * + * @param int $response_code will set the response code. + * + * @return void + */ + public function set_http_response_code($response_code) + { + $this->http_response_code($response_code); + switch ($response_code) { + case 100: $httpStatusMsg = ' Continue'; break; + case 101: $httpStatusMsg = ' Switching Protocols'; break; + case 200: $httpStatusMsg = ' OK'; break; + case 201: $httpStatusMsg = ' Created'; break; + case 202: $httpStatusMsg = ' Accepted'; break; + case 203: $httpStatusMsg = ' Non-Authoritative Information'; break; + case 204: $httpStatusMsg = ' No Content'; break; + case 205: $httpStatusMsg = ' Reset Content'; break; + case 206: $httpStatusMsg = ' Partial Content'; break; + case 300: $httpStatusMsg = ' Multiple Choices'; break; + case 301: $httpStatusMsg = ' Moved Permanently'; break; + case 302: $httpStatusMsg = ' Moved Temporarily'; break; + case 303: $httpStatusMsg = ' See Other'; break; + case 304: $httpStatusMsg = ' Not Modified'; break; + case 305: $httpStatusMsg = ' Use Proxy'; break; + case 400: $httpStatusMsg = ' Bad Request'; break; + case 401: $httpStatusMsg = ' Unauthorized'; break; + case 402: $httpStatusMsg = ' Payment Required'; break; + case 403: $httpStatusMsg = ' Forbidden'; break; + case 404: $httpStatusMsg = ' Not Found'; break; + case 405: $httpStatusMsg = ' Method Not Allowed'; break; + case 406: $httpStatusMsg = ' Not Acceptable'; break; + case 407: $httpStatusMsg = ' Proxy Authentication Required'; break; + case 408: $httpStatusMsg = ' Request Time-out'; break; + case 409: $httpStatusMsg = ' Conflict'; break; + case 410: $httpStatusMsg = ' Gone'; break; + case 411: $httpStatusMsg = ' Length Required'; break; + case 412: $httpStatusMsg = ' Precondition Failed'; break; + case 413: $httpStatusMsg = ' Request Entity Too Large'; break; + case 414: $httpStatusMsg = ' Request-URI Too Large'; break; + case 415: $httpStatusMsg = ' Unsupported Media Type'; break; + case 500: $httpStatusMsg = ' Internal Server Error'; break; + case 501: $httpStatusMsg = ' Not Implemented'; break; + case 502: $httpStatusMsg = ' Bad Gateway'; break; + case 503: $httpStatusMsg = ' Service Unavailable'; break; + case 504: $httpStatusMsg = ' Gateway Time-out'; break; + case 505: $httpStatusMsg = ' HTTP Version not supported'; break; + default: $httpStatusMsg = ' Web server is down'; break; + } + if (php_sapi_name() !== 'cgi-fcgi') { + $this->header('status: ' . $response_code . $httpStatusMsg); + } + } + + /** + * Generate header for 303 + * + * @param string $location will set location to redirect. + * + * @return void + */ + public function generateHeader303($location) + { + $this->set_http_response_code(303); + $this->header('Location: '.$location); + if (!defined('TESTSUITE')) { + exit; + } + } } diff --git a/libraries/plugins/auth/AuthenticationHttp.php b/libraries/plugins/auth/AuthenticationHttp.php index aca37bd9fd..5ced799e6e 100644 --- a/libraries/plugins/auth/AuthenticationHttp.php +++ b/libraries/plugins/auth/AuthenticationHttp.php @@ -66,10 +66,7 @@ class AuthenticationHttp extends AuthenticationPlugin // remove non US-ASCII to respect RFC2616 $realm_message = preg_replace('/[^\x20-\x7e]/i', '', $realm_message); $response->header('WWW-Authenticate: Basic realm="' . $realm_message . '"'); - $response->header('HTTP/1.0 401 Unauthorized'); - if (php_sapi_name() !== 'cgi-fcgi') { - $response->header('status: 401 Unauthorized'); - } + $response->set_http_response_code(401); /* HTML header */ $footer = $response->getFooter(); diff --git a/setup/config.php b/setup/config.php index 3bc5d56a58..8235f965a8 100644 --- a/setup/config.php +++ b/setup/config.php @@ -8,6 +8,7 @@ use PMA\libraries\config\FormDisplay; use PMA\setup\lib\ConfigGenerator; use PMA\libraries\URL; +use PMA\libraries\Response; /** * Core libraries. @@ -20,6 +21,8 @@ $form_display = new FormDisplay($GLOBALS['ConfigFile']); $form_display->registerForm('_config.php', $forms['_config.php']); $form_display->save('_config.php'); +$response = Response::getInstance(); + if (isset($_POST['eol'])) { $_SESSION['eol'] = ($_POST['eol'] == 'unix') ? 'unix' : 'win'; } @@ -30,8 +33,7 @@ if (PMA_ifSetOr($_POST['submit_clear'], '')) { // $GLOBALS['ConfigFile']->resetConfigData(); // drop post data - header('HTTP/1.1 303 See Other'); - header('Location: index.php' . URL::getCommonRaw()); + $response->generateHeader303('index.php' . URL::getCommonRaw()); exit; } elseif (PMA_ifSetOr($_POST['submit_download'], '')) { // @@ -44,7 +46,6 @@ if (PMA_ifSetOr($_POST['submit_clear'], '')) { // // Show generated config file in a