From 9fe138d09f1fa35b7058c22777bc76724741ee40 Mon Sep 17 00:00:00 2001 From: GBO Date: Tue, 3 Jan 2017 15:20:25 +0100 Subject: [PATCH] Error management improvement (using HTTP status code) --- proxy.php | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/proxy.php b/proxy.php index 4c3d027..8e1df17 100644 --- a/proxy.php +++ b/proxy.php @@ -90,10 +90,7 @@ } elseif (isset($_SERVER['HTTP_X_PROXY_URL'])) { $request_url = urldecode($_SERVER['HTTP_X_PROXY_URL']); } else { - header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found'); - header('Status: 404 Not Found'); - $_SERVER['REDIRECT_STATUS'] = 404; - exit; + csajax_exit_error(404, 'Not Found'); } $p_request_url = parse_url($request_url); @@ -106,7 +103,7 @@ // ignore requests for proxy :) if (preg_match('!' . $_SERVER['SCRIPT_NAME'] . '!', $request_url) || empty($request_url) || count($p_request_url) == 1) { csajax_debug_message('Invalid request - make sure that csurl variable is not empty'); - exit; + csajax_exit_error(403, 'Forbidden'); } // check against valid requests @@ -115,7 +112,7 @@ if (CSAJAX_FILTER_DOMAIN) { if (!in_array($parsed['host'], $valid_requests)) { csajax_debug_message('Invalid domain - ' . $parsed['host'] . ' does not included in valid requests'); - exit; + csajax_exit_error(403, 'Forbidden'); } } else { $check_url = isset($parsed['scheme']) ? $parsed['scheme'] . '://' : ''; @@ -125,7 +122,7 @@ $check_url .= isset($parsed['path']) ? $parsed['path'] : ''; if (!in_array($check_url, $valid_requests)) { csajax_debug_message('Invalid domain - ' . $request_url . ' does not included in valid requests'); - exit; + csajax_exit_error(403, 'Forbidden'); } } } @@ -157,6 +154,17 @@ // retrieve response (headers and content) $response = curl_exec($ch); +// error management +if (false === $response) { + $errno = curl_errno($ch); + $message = curl_error($ch); + curl_close($ch); + if ($errno == 7) { + csajax_exit_error(408, 'Request Time-out'); + } else { + csajax_exit_error(500, 'Server Error', $message); + } +} curl_close($ch); // split response to header and content @@ -178,6 +186,17 @@ // finally, output the content print($response_content); +function csajax_exit_error($error_code, $error_text, $message = '') +{ + header($_SERVER['SERVER_PROTOCOL'] . " $error_code $error_text"); + header("Status: $error_code $error_text"); + $_SERVER['REDIRECT_STATUS'] = $error_code; + if (!empty($message)) { + print($message); + } + exit($error_code); +} + function csajax_debug_message($message) { if (true == CSAJAX_DEBUG) {