Libcurl NXDOMAIN导致超时

时间:2013-04-27 20:05:31

标签: curl dns libcurl curl-multi c-ares

我的应用中的Libcurl似乎在使用非现有域时遇到问题,因为它在“ NXDOMAIN ”请求中保持至少7到20秒(这似乎与CURLOPT_CONNECTTIMEOUT匹配)。

这是pmp(poore man的探查器)输出:

   2585 __GI___poll,Curl_poll,curl_multi_wait,curl_easy_perform,getweb,athread,start_thread,clone,??
   1281 __GI___poll,Curl_poll,curl_multi_wait,curl_easy_perform,getweb,getweb,athread,start_thread,clone,??
    100 nanosleep,__sleep,athread,start_thread,clone,??
    ...

curl命令似乎没有此问题。它在一秒钟内完成相同的请求。

这个问题可能与this one有关,因为看起来我已经解决了一个问题并最终解决了另一个问题,无法判断它是在最后一次ubuntu更新之后还是之前。

这是我项目中的libcurl代码:

PAGE_TIMEOUT=20;
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, (PAGE_TIMEOUT-PAGE_TIMEOUT%3)/3); //3 times less
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, true);
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 20);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, PAGE_TIMEOUT);
curl_easy_setopt(curl, CURLOPT_URL, argv);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_to_string);
curl_easy_setopt(curl, CURLOPT_WRITEHEADER, &header);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
dns_index=DNS_SERVER_I;
pthread_mutex_lock(&running_mutex);
    if(DNS_SERVER_I>DNS_SERVERS.size())
    {
        DNS_SERVER_I=1;
    }else
    {
        DNS_SERVER_I++;
    }
pthread_mutex_unlock(&running_mutex);
}
string dns_servers_string=DNS_SERVERS.at(dns_index%DNS_SERVERS.size())+","+DNS_SERVERS.at((dns_index+1)%DNS_SERVERS.size())+","+DNS_SERVERS.at((dns_index+2)%DNS_SERVERS.size());
curl_easy_setopt(curl, CURLOPT_DNS_SERVERS, &dns_servers_string[0]);    
curl_easy_setopt(curl, CURLOPT_DNS_USE_GLOBAL_CACHE,false);

struct curl_slist *slist=NULL;
slist = curl_slist_append(slist, "ACCEPT: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,*/*;q=0.5");
slist = curl_slist_append(slist, "ACCEPT_CHARSET: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
slist = curl_slist_append(slist, "ACCEPT_ENCODING: gzip,deflate");
slist = curl_slist_append(slist, "ACCEPT_LANGUAGE: en-gb,en;q=0.5");
slist = curl_slist_append(slist, "CONNECTION: keep-alive");
slist = curl_slist_append(slist, "KEEP_ALIVE: 300");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
string useragent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16";
curl_easy_setopt(curl, CURLOPT_USERAGENT, useragent.c_str());   

sm=curl_easy_perform(curl);

我已经从源代码构建了libcurl v7.30(带有c-ares而没有ipv6)并安装在ubuntu 12.10存储库版本上(不确定是否被覆盖,我使用过make install)。

在上次自动更新后,我收到了error,但不知道它是否相关。

0 个答案:

没有答案