]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/minicurl.cc
7 d_curl
= curl_easy_init();
12 curl_easy_cleanup(d_curl
);
15 size_t MiniCurl::write_callback(char *ptr
, size_t size
, size_t nmemb
, void *userdata
)
17 MiniCurl
* us
= (MiniCurl
*)userdata
;
18 us
->d_data
.append(ptr
, size
*nmemb
);
22 static string
extractHostFromURL(const std::string
& url
)
24 auto pos
= url
.find("://");
25 if(pos
== string::npos
)
26 throw runtime_error("Can't find host part of '"+url
+"'");
28 auto endpos
= url
.find('/', pos
);
29 if(endpos
== string::npos
)
30 return url
.substr(pos
);
32 return url
.substr(pos
, endpos
-pos
);
35 void MiniCurl::setupURL(const std::string
& str
, const ComboAddress
* rem
, const ComboAddress
* src
)
38 struct curl_slist
*hostlist
= NULL
;
40 // url = http://hostname.enzo/url
42 string host4
=extractHostFromURL(str
);
43 string hcode
=(host4
+":80:"+rem
->toString());
44 //cout<<"Setting hardcoded IP: "<<hcode<<endl;
45 hostlist
= curl_slist_append(NULL
, hcode
.c_str());
46 hcode
=(host4
+":443:"+rem
->toString());
47 // cout<<"Setting hardcoded IP: "<<hcode<<endl;;
48 hostlist
= curl_slist_append(hostlist
, hcode
.c_str());
50 curl_easy_setopt(d_curl
, CURLOPT_RESOLVE
, hostlist
);
53 curl_easy_setopt(d_curl
, CURLOPT_INTERFACE
, src
->toString().c_str());
55 curl_easy_setopt(d_curl
, CURLOPT_FOLLOWLOCATION
, true);
56 /* only allow HTTP, TFTP and SFTP */
57 curl_easy_setopt(d_curl
, CURLOPT_PROTOCOLS
, CURLPROTO_HTTP
| CURLPROTO_HTTPS
);
58 curl_easy_setopt(d_curl
, CURLOPT_SSL_VERIFYPEER
, false);
59 curl_easy_setopt(d_curl
, CURLOPT_SSL_VERIFYHOST
, false);
60 curl_easy_setopt(d_curl
, CURLOPT_FAILONERROR
, true);
61 curl_easy_setopt(d_curl
, CURLOPT_URL
, str
.c_str());
62 curl_easy_setopt(d_curl
, CURLOPT_WRITEFUNCTION
, write_callback
);
63 curl_easy_setopt(d_curl
, CURLOPT_WRITEDATA
, this);
64 curl_easy_setopt(d_curl
, CURLOPT_TIMEOUT
, 2L);
68 std::string
MiniCurl::getURL(const std::string
& str
, const ComboAddress
* rem
, const ComboAddress
* src
)
70 setupURL(str
, rem
, src
);
71 auto res
= curl_easy_perform(d_curl
);
73 curl_easy_getinfo(d_curl
, CURLINFO_RESPONSE_CODE
, &http_code
);
75 if(res
!= CURLE_OK
|| http_code
!= 200) {
76 throw std::runtime_error("Unable to retrieve URL ("+std::to_string(http_code
)+"): "+string(curl_easy_strerror(res
)));
78 std::string ret
=d_data
;
83 std::string
MiniCurl::postURL(const std::string
& str
, const std::string
& postdata
)
86 curl_easy_setopt(d_curl
, CURLOPT_POSTFIELDS
, postdata
.c_str());
88 auto res
= curl_easy_perform(d_curl
);
90 throw std::runtime_error("Unable to post URL");
92 std::string ret
=d_data
;