]> git.ipfire.org Git - thirdparty/pdns.git/blobdiff - pdns/minicurl.cc
Merge pull request #7870 from omoerbeek/stubquery-fix-arg
[thirdparty/pdns.git] / pdns / minicurl.cc
index 0c3ab98fb9444ebb21c882f46eb8ceabae260ed8..e8ba8e9edad4b13f5f4b5203bf64d933a1cebfb9 100644 (file)
@@ -1,14 +1,56 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2018-2019 powerdns.com bv
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
 #include "minicurl.hh"
 #include <curl/curl.h>
 #include <stdexcept>
 
-MiniCurl::MiniCurl()
+void MiniCurl::init()
+{
+  static std::atomic_flag s_init = ATOMIC_FLAG_INIT;
+
+  if (s_init.test_and_set())
+    return;
+
+  CURLcode code = curl_global_init(CURL_GLOBAL_ALL);
+  if (code != 0) {
+    throw std::runtime_error("Error initializing libcurl");
+  }
+}
+
+MiniCurl::MiniCurl(const string& useragent)
 {
   d_curl = curl_easy_init();
+  if (d_curl == nullptr) {
+    throw std::runtime_error("Error creating a MiniCurl session");
+  }
+  curl_easy_setopt(d_curl, CURLOPT_USERAGENT, useragent.c_str());
 }
 
 MiniCurl::~MiniCurl()
 {
+  // NEEDS TO CLEAN HOSTLIST
   curl_easy_cleanup(d_curl);
 }
 
@@ -35,22 +77,30 @@ static string extractHostFromURL(const std::string& url)
 void MiniCurl::setupURL(const std::string& str, const ComboAddress* rem, const ComboAddress* src)
 {
   if(rem) {
-    struct curl_slist *hostlist = NULL;
-
-    // url = http://hostname.enzo/url 
+    struct curl_slist *hostlist = nullptr; // THIS SHOULD BE FREED
 
+    // url = http://hostname.enzo/url
     string host4=extractHostFromURL(str);
-    string hcode=(host4+":80:"+rem->toString());
-    //cout<<"Setting hardcoded IP: "<<hcode<<endl;
-    hostlist = curl_slist_append(NULL, hcode.c_str());
-    hcode=(host4+":443:"+rem->toString());
-    //    cout<<"Setting hardcoded IP: "<<hcode<<endl;;
-    hostlist = curl_slist_append(hostlist, hcode.c_str());
+    // doest the host contain port indication
+    std::size_t found = host4.find(':');
+    vector<uint16_t> ports{80, 443};
+    if (found != std::string::npos) {
+      int port = std::stoi(host4.substr(found + 1));
+      if (port <= 0 || port > 65535)
+        throw std::overflow_error("Invalid port number");
+      ports = {(uint16_t)port};
+      host4 = host4.substr(0, found);
+    }
+
+    for (const auto& port : ports) {
+      string hcode = boost::str(boost::format("%s:%u:%s") % host4 % port % rem->toString());
+      hostlist = curl_slist_append(hostlist, hcode.c_str());
+    }
 
     curl_easy_setopt(d_curl, CURLOPT_RESOLVE, hostlist);
   }
   if(src) {
-    curl_easy_setopt(d_curl, CURLOPT_INTERFACE, src->toString());
+    curl_easy_setopt(d_curl, CURLOPT_INTERFACE, src->toString().c_str());
   }
   curl_easy_setopt(d_curl, CURLOPT_FOLLOWLOCATION, true);
   /* only allow HTTP, TFTP and SFTP */
@@ -62,9 +112,10 @@ void MiniCurl::setupURL(const std::string& str, const ComboAddress* rem, const C
   curl_easy_setopt(d_curl, CURLOPT_WRITEFUNCTION, write_callback);
   curl_easy_setopt(d_curl, CURLOPT_WRITEDATA, this);
   curl_easy_setopt(d_curl, CURLOPT_TIMEOUT, 2L);
-  
+
   d_data.clear();
 }
+
 std::string MiniCurl::getURL(const std::string& str, const ComboAddress* rem, const ComboAddress* src)
 {
   setupURL(str, rem, src);
@@ -86,7 +137,7 @@ std::string MiniCurl::postURL(const std::string& str, const std::string& postdat
   curl_easy_setopt(d_curl, CURLOPT_POSTFIELDS, postdata.c_str());
 
   auto res = curl_easy_perform(d_curl);
-  if(res != CURLE_OK) 
+  if(res != CURLE_OK)
     throw std::runtime_error("Unable to post URL");
 
   std::string ret=d_data;