Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error management improvement (using HTTP status code) #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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'] . '://' : '';
Expand All @@ -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');
}
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down