cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Wuery regarding "CURLOPT_FOLLOWLOCATION" option of libCurl

From: Arif Ali Saiyed <arif.ali.syed_at_gmail.com>
Date: Fri, 22 Aug 2014 10:25:59 +0530

Hello Daniel,

           Yes you were correct, It does follow the redirections if
CURLOPT_FOLLOWLOCATION is set.
I figured out the exact problem. its the "User Agent". When I give no User
agent redirection works fine and I get the response body of redirected page.
If give some of the old User Agents that are mentioned here
http://curl.haxx.se/docs/httpscripting.html#User_Agent
it does work with them but with all latest user agents ( desktop Google
Chrome, Desktop Firefox, IPad Safari ,iPhone Safari ) redirection is not
working.
Below is the sample code. I am using curl 7.37.0

#include <stdio.h>
#include <curl/curl.h>

static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
  int written = fwrite(ptr, size, nmemb, (FILE *)stream);
  fflush((FILE *)stream);
  return written;
}

int main(void)
{
  CURL *curl_handle;
  static const char *headerfilename = "head.txt";
  static const char *bodyfilename = "body.html";
  FILE *headerfile;
  FILE *bodyfile;

  curl_global_init(CURL_GLOBAL_ALL);

  /* init the curl session */
  curl_handle = curl_easy_init();

  /* set URL to get */
  curl_easy_setopt(curl_handle, CURLOPT_URL, "http://google.com");

  /* no progress meter please */
  curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);

  /* send all data to this function */
  curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1);
  curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
  // Dont give any User agent = Working
  //curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "Mozilla/5.0 (iPhone;
CPU iPhone OS 7_1 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko)
Mobile/11D167"); = NOT Working
  //curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows
NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0"); //Latest Firefox =
NOT Working
  //curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "Mozilla/4.0
(compatible; MSIE 5.01; Windows NT 5.0)"); //Internet Explorer 5 on a
Windows 2000 box:= WORKING
  //curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows
NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125
Safari/537.36"); //Latest Chrome = NOT Working
  //curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "Mozilla/4.73 [en]
(X11; U; Linux 2.2.15 i686)"); //Netscape 4.73 on an old Linux box = WORKING
  //curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "Mozilla/5.0 (iPhone;
CPU iPhone OS 7_1 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko)
Mobile/11D167"); //Latest safari on iPhone = NOT Working
  //curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "Mozilla/5.0 (iPad;
CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko)
Version/6.0 Mobile/10A5355d Safari/8536.25"); //Latest safari on iPad= NOT
Working

  /* open the files */
  headerfile = fopen(headerfilename,"wb");
  if (headerfile == NULL) {
    curl_easy_cleanup(curl_handle);
    return -1;
  }
  bodyfile = fopen(bodyfilename,"wb");
  if (bodyfile == NULL) {
    curl_easy_cleanup(curl_handle);
    return -1;
  }

  /* we want the headers be written to this file handle */
  curl_easy_setopt(curl_handle, CURLOPT_HEADERDATA, headerfile);

  /* we want the body be written to this file handle instead of stdout */
  curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, bodyfile);

  /* get it! */
  curl_easy_perform(curl_handle);

  /* close the header file */
  fclose(headerfile);

  /* close the body file */
  fclose(bodyfile);

  /* cleanup curl stuff */
  curl_easy_cleanup(curl_handle);

  return 0;
}

-Arif

On Thu, Aug 21, 2014 at 8:24 PM, Arif Ali Saiyed <arif.ali.syed_at_gmail.com>
wrote:

> Thank you so much Daniel for that quick response.
> Which means the problem is in the way I am using it. I will try to isolate
> this redirect case out of my code and see if it works for me and share the
> code in case of doesn't.
>
> -Arif
>
>
> -Arif
>
>
> On Thu, Aug 21, 2014 at 8:18 PM, Daniel Stenberg <daniel_at_haxx.se> wrote:
>
>> On Thu, 21 Aug 2014, Arif Ali Saiyed wrote:
>>
>> I foudn out that there is some option like "CURLOPT_FOLLOWLOCATION" but
>>> when I use it. curl only gets the responseHeader alone from redirected
>>> location and not the responsebody.
>>>
>>
>> That's not correct. libcurl will provide the body for the followed-to
>> response. You can test this very easily with curl and its -L option.
>>
>> I can't tell why it doesn't work for you.
>>
>>
>> "If you want the full bodies, don't ask libcurl to follow the redirects
>>> but handle that yourself."
>>>
>>> Here it looks contradictory to my previous understanding of "when you
>>> set CURLOPT_FOLLOWLOCATION"
>>>
>>
>> That line could probably be expanded somewhat to say
>>
>> If you want the full response bodies of *all* responses, including the
>> 3xx ones that libcurl will follow location: from when you have
>> CURLOPT_FOLLOWLOCATION enabled, you should do the redirect handling
>> yourself.
>>
>>
>> What is this option "CURLOPT_REDIRBODIES" thats being talked about in
>>> some places. I htink I have the latest version of libCurl. But this option
>>> is not there.
>>>
>>
>> It was a suggested option back in 2005 that never went anywhere:
>> http://curl.haxx.se/mail/lib-2005-07/0195.html
>>
>> --
>>
>> / daniel.haxx.se
>>
>
>

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2014-08-22