From 99e7e7876e44bf9d23506016e9e2c06c722d8f6b Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Sun, 6 Aug 2017 12:47:48 +0200 Subject: [PATCH] exception: Use built-in HTTP response codes PHP >= 5.4 provides a built-in function to generate valid HTTP error header including the error description: http_response_code() See: http://php.net/manual/en/function.http-response-code.php See also: https://stackoverflow.com/a/12018482 This commit removes the '\Http' utility class and replaces all calls to 'Http::getMessageForCode()' by 'http_response_code()' --- bridges/FacebookBridge.php | 2 +- index.php | 6 ++-- lib/Exceptions.php | 58 -------------------------------------- 3 files changed, 4 insertions(+), 62 deletions(-) diff --git a/bridges/FacebookBridge.php b/bridges/FacebookBridge.php index 4cce5b9..d2f8b91 100644 --- a/bridges/FacebookBridge.php +++ b/bridges/FacebookBridge.php @@ -155,7 +155,7 @@ class FacebookBridge extends BridgeAbstract { //Show captcha filling form to the viewer, proxying the captcha image $img = base64_encode(getContents($captcha->find('img', 0)->src)); - header('HTTP/1.1 500 ' . Http::getMessageForCode(500)); + http_response_code(500); header('Content-Type: text/html'); $message = << diff --git a/index.php b/index.php index 70b80cb..d6d5b19 100644 --- a/index.php +++ b/index.php @@ -174,7 +174,7 @@ try { $bridge->setCache($cache); $bridge->setDatas($params); } catch(Exception $e) { - header('HTTP/1.1 ' . $e->getCode() . ' ' . Http::getMessageForCode($e->getCode())); + http_response_code($e->getCode()); header('Content-Type: text/html'); die(buildBridgeException($e, $bridge)); } @@ -186,7 +186,7 @@ try { $format->setExtraInfos($bridge->getExtraInfos()); $format->display(); } catch(Exception $e) { - header('HTTP/1.1 ' . $e->getCode() . ' ' . Http::getMessageForCode($e->getCode())); + http_response_code($e->getCode()); header('Content-Type: text/html'); die(buildTransformException($e, $bridge)); } @@ -194,7 +194,7 @@ try { die; } } catch(HttpException $e) { - header('HTTP/1.1 ' . $e->getCode() . ' ' . Http::getMessageForCode($e->getCode())); + http_response_code($e->getCode()); header('Content-Type: text/plain'); die($e->getMessage()); } catch(\Exception $e) { diff --git a/lib/Exceptions.php b/lib/Exceptions.php index ff8ec86..252d1e3 100644 --- a/lib/Exceptions.php +++ b/lib/Exceptions.php @@ -1,64 +1,6 @@ 'OK', - 201 => 'Created', - 202 => 'Accepted', - 300 => 'Multiple Choices', - 301 => 'Moved Permanently', - 302 => 'Moved Temporarily', - 307 => 'Temporary Redirect', - 310 => 'Too many Redirects', - 400 => 'Bad Request', - 401 => 'Unauthorized', - 402 => 'Payment Required', - 403 => 'Forbidden', - 404 => 'Not Found', - 405 => 'Method Not', - 406 => 'Not Acceptable', - 407 => 'Proxy Authentication Required', - 408 => 'Request Time-out', - 409 => 'Conflict', - 410 => 'Gone', - 411 => 'Length Required', - 412 => 'Precondition Failed', - 413 => 'Request Entity Too Large', - 414 => 'Request-URI Too Long', - 415 => 'Unsupported Media Type', - 416 => 'Requested range unsatisfiable', - 417 => 'Expectation failed', - 500 => 'Internal Server Error', - 501 => 'Not Implemented', - 502 => 'Bad Gateway', - 503 => 'Service Unavailable', - 504 => 'Gateway Time-out', - 508 => 'Loop detected', - ); - } -} - /** * Returns an URL that automatically populates a new issue on GitHub based * on the information provided