Created
September 17, 2012 13:40
-
-
Save azat/3737325 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ASSERT(curl_easy_setopt(ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTP) == CURLE_OK); | |
ASSERT(curl_easy_setopt(ch, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP) == CURLE_OK); | |
// main loop, for all URL's | |
while (1) { | |
CURLcode code; | |
int currentRedirectIteration = 0; | |
long httpResponseCode; | |
char buffer[URL_MAX]; | |
char *url[URL_MAX]; | |
getUrl(url); | |
if (!strlen(url)) { | |
break; | |
} | |
// loop need only for manually redirect handling | |
while (1) { | |
curl_easy_setopt(ch, CURLOPT_NOBODY, 0L); | |
curl_easy_setopt(ch, CURLOPT_HTTPGET, 1L); | |
curl_easy_setopt(ch, CURLOPT_URL, url); | |
code = curl_easy_perform(ch); | |
// curl error | |
if (code != CURLE_OK) { | |
break; | |
} | |
// redirect | |
curl_easy_getinfo(ch, CURLINFO_RESPONSE_CODE, &httpResponseCode) | |
if ((httpResponseCode == HTTP_STATUS_MOVED_PERMANENTLY) || (httpResponseCode == HTTP_STATUS_FOUND)) { | |
currentRedirectIteration++; | |
curl_easy_getinfo(ch, CURLINFO_REDIRECT_URL, &buffer); | |
if (!buffer) { | |
break; | |
} | |
strncpy(url, buffer, strlen(buffer) + 1 /* + null byte */); | |
// reset "Host: " | |
curl_easy_setopt(ch, CURLOPT_HTTPHEADER, NULL); | |
// we have redirect, need to fetch next URL | |
// but only if we not exceed redirect limit | |
if (currentRedirectIteration <= MAXIMUM_REDIRECTS) { | |
continue; | |
} | |
// exceed redirect limit | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment