cURL / Mailing Lists / curl-users / Single Mail

curl-users

About IP resolve problem when using HTTP proxy

From: Jin <myhao840430_at_gmail.com>
Date: Fri, 14 Jun 2013 15:52:36 +0800

Hello.

I'm trying to use libcurl to download some file from HTTPS web site
using HTTP proxy and SOCKS proxy.

And one domain has several IP addresses and it requires the
authentication for domain using CA certificate.
If I used IP address, then it has failed due to verification of server
domain name.

So I'm using IP resolve option(CURLOPT_RESOLVE) when downloading file
from individual servers.

But when I'm going to use HTTP proxy, it doesn't resolve IP address
correctly.
For example, there are 2 IP addresses(1.1.1.1, 2.2.2.2) for domain
xxx.test.com.
If I'm going to download some file from 1.1.1.1 using HTTP proxy, but
HTTP header says it's going to xxx.test.com not 1.1.1.1.

SOCKS proxy is correctly works.

Here is the part of my source code.

How I can resolve this problem?

Thanks.
Jin.

  CURL *curl;
  FILE *fp;

   int ret = 0;
   struct curl_slist *slist = NULL;

     char cacert_file_path[MAX_PATH + 1], cacert_file_dir[MAX_PATH + 1];

   /* initialize curl operation */
   curl = curl_easy_init();
   if (!curl)
     {
       return -1;
     }

   fp = fopen(file_path, "wb");
   if (!fp)
     {
       curl_easy_cleanup(curl);
       return -1;
     }

   /* set curl debugging option */
   curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
   curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, write_curl_dbg);

   /* set down URL */
   curl_easy_setopt(curl, CURLOPT_URL, url);

   /* set write file operation */
   curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
   curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);

   /* timeout after 5 second instead of waiting the customary 120 */
   curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
   curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, PTCORE_CURL_TIMEOUT);

   curl_easy_setopt(curl, CURLOPT_CAINFO, cacert_file_path);
   curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1);
   curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2);

    /* set resolve options */
    char curl_resolv_str[MAX_IPADDR_LEN + MAX_HOST_NAME_LEN + 10];

    memset(curl_resolv_str, 0, sizeof(curl_resolv_str));
   sprintf(curl_resolv_str, "%s:443:%s", SERVER_HOSTNAME, srv_addr);

   slist = curl_slist_append(slist, curl_resolv_str);

   curl_easy_setopt(curl, CURLOPT_RESOLVE, slist);

   /* set proxy info */
   char proxy_url[MAX_URL_LEN + 1];

   memset(proxy_url, 0, sizeof(proxy_url));
   if (proxy_type == PTCORE_PROXY_TYPE_HTTP)
         {
           curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
           sprintf(proxy_url, "http://%s:%d", proxy_addr, proxy_port);
         }
       else
         {
           curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
           sprintf(proxy_url, "socks5://%s:%d", proxy_addr, proxy_port);
         }

       curl_easy_setopt(curl, CURLOPT_PROXY, proxy_url);

   /* perform curl operation */
   *res = curl_easy_perform(curl);
   if (*res != CURLE_OK)
     {
       ret = -1;
     }

   /* free string list */
   if (slist)
     curl_slist_free_all(slist);

   /* cleanup curl operation */
   curl_easy_cleanup(curl);

     /* close file */
     fclose(fp);

   return ret;
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-users
FAQ: http://curl.haxx.se/docs/faq.html
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2013-06-14