Skip to content

Instantly share code, notes, and snippets.

@azat
Created September 17, 2012 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azat/3737325 to your computer and use it in GitHub Desktop.
Save azat/3737325 to your computer and use it in GitHub Desktop.
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