How can I disable the display of errors 502 and 503 in Privoxy?

63 Views Asked by At

I use Privoxy in CURL queries for different sites.

$ch = curl_init($url);
...
curl_setopt($ch, CURLOPT_PROXY, "localhost:8118");
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
...
$result = curl_exec($ch);

Sometimes, if the site is not available, I get one of the Privoxy html templates displayed in the browser, usually it is /privoxy/templates/forwarding-failed at 503 error or /privoxy/templates/no-server-data at 502.

I would like to be able to handle these errors myself after receiving $result = curl_exec($ch); as with normal CURL requests, without using Privoxy. But I do not know how in Privoxy in the settings you can disable the connection and display of these error templates. Please tell me how you can solve this problem.

I used to be able to disable the output of the /privoxy/templates/blocked template using edits in the default.action and user.action files, but I did not find such settings there to disable the 502 or 503 error templates.

1

There are 1 best solutions below

0
On

You can add the CURLOPT_FAILONERROR option to your CURL request to prevent CURL from returning the response content for HTTP error status codes (e.g., 502, 503) and instead handle them yourself.

If the HTTP server returns an error code of 400 or greater. This means that if the server returns a status code of 401 (Unauthorized), 403 (Forbidden), 404 (Not Found), or anything else in the 400 range, the request will be aborted and the curl_exec() function will return FALSE.

$ch = curl_init($url);
// Set other CURL options as needed

// Set the CURLOPT_PROXY and CURLOPT_PROXYTYPE options as you were doing

// Add the following option to handle errors manually
curl_setopt($ch, CURLOPT_FAILONERROR, true);

// Execute the request
$result = curl_exec($ch);

// Check if the request was successful or not
if ($result === false) {
    // Handle the error here
    $error = curl_error($ch);
    // You can log the error, retry the request, or take appropriate action
} else {
    // Request was successful, process the response
}

// Close the CURL session
curl_close($ch);