RoToRa
RoToRa

Reputation: 38390

Is it ok to terminate a HTTP request in the callback function set by CURLOPT_HEADERFUNCTION?

Currently I'm writing a PHP script that is supposed to check if a URL is current (returns a HTTP 200 code or redirects to such an URL).

Since several of the URLs that are to be tested return a file, I'd like to avoid using a normal GET request, in order not having to actually download a file.

I would normally use the HTTP HEAD method, however tests show, that many servers don't recognize it and return a different HTTP code than the corresponding GET request.

My idea was know to make a GET request and use CURLOPT_HEADERFUNCTION to define a callback function which checks the HTTP code in the first line of the header and then immediately terminate the request by having it return 0 (instead of the length of the header) if it's not a redirect code.

My question is: Is it ok, to terminate a HTTP request like that? Or will it have any negative effects on the server? Will this actually avoid the unnecessary download?

Example code (untested):

$url = "http://www.example.com/";

$ch = curl_init($url);

curl_setopt_array($ch, array(
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HEADER => true,
    CURLINFO_HEADER_OUT => true,
    CURLOPT_HTTPGET => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HEADERFUNCTION => 'requestHeaderCallback',
));

$curlResult = curl_exec($ch);

curl_close($ch);

function requestHeaderCallback($ch, $header) {
    $matches = array();
    if (preg_match("/^HTTP/\d.\d (\d{3}) /")) {
        if ($matches[1] < 300 || $matches[1] >= 400) {
            return 0;
        }
    }
    return strlen($header);
}

Upvotes: 2

Views: 120

Answers (1)

Daniel Stenberg
Daniel Stenberg

Reputation: 57994

Yes it is fine and yes it will stop the transfer right there.

It will also cause the connection to get disconnected, which only is a concern if you intend to do many requests to the same host as then keeping the connection alive could be a performance benefit.

Upvotes: 1

Related Questions