Knowing when to report response code when using HTTP AUTH
Date: Thu, 5 Jun 2008 16:29:28 +0100
I'm trying to work out where the best place for my libcurl client
application to abandon a CURL HTTP request when it receives an HTTP
error response code (4xx, 5xx). In the event of such failures it is a
waste of effort for the application to continue and download the error
message from the server (and actually causes further complications
down the line).
Until now I just called curl_easy_getinfo(CURLINFO_RESPONSE_CODE) from
inside the CURLOPT_HEADERFUNCTION callback when I saw the "\r\n"
terminating the headers. This seemed to work well.
But with HTTP AUTH the CURLOPT_HEADERFUNCTION callback is called for
both the initial request headers which ultimately yields a 401 and
again for the subsequent request yielding the real HTTP response
code. If I abort on the first "\r\n" then curl never gets the chance
to actually supply the authentication credentials.
The alternative, which I believe should work, would be to call
curl_easy_getinfo(CURLINFO_RESPONSE_CODE) the first time the
READFUNCTION (or other body function) callback is called and abort
then. This just seems a little ugly because I either have to
redundantly check every time or keep track of whether it is the first
time the body callback has been called.
Am I missing an obvious cleaner alternative?
Received on 2008-06-05