]> git.ipfire.org Git - thirdparty/pdns.git/blobdiff - pdns/minicurl.cc
Merge pull request #7908 from omoerbeek/rec-4.1.14-changelog
[thirdparty/pdns.git] / pdns / minicurl.cc
index 034957783def2573f7a62b8b7cc3b000d222856c..5c846d09bd67fb666d975e3de048e3615cf808aa 100644 (file)
@@ -1,10 +1,51 @@
+/*
+ * 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()
@@ -38,7 +79,7 @@ void MiniCurl::setupURL(const std::string& str, const ComboAddress* rem, const C
   if(rem) {
     struct curl_slist *hostlist = nullptr; // THIS SHOULD BE FREED
 
-    // url = http://hostname.enzo/url 
+    // url = http://hostname.enzo/url
     string host4=extractHostFromURL(str);
     // doest the host contain port indication
     std::size_t found = host4.find(':');
@@ -62,7 +103,7 @@ void MiniCurl::setupURL(const std::string& str, const ComboAddress* rem, const C
     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 */
+  /* only allow HTTP and HTTPS */
   curl_easy_setopt(d_curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
   curl_easy_setopt(d_curl, CURLOPT_SSL_VERIFYPEER, false);
   curl_easy_setopt(d_curl, CURLOPT_SSL_VERIFYHOST, false);
@@ -71,9 +112,11 @@ 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);
-  
+
+  clearHeaders();
   d_data.clear();
 }
+
 std::string MiniCurl::getURL(const std::string& str, const ComboAddress* rem, const ComboAddress* src)
 {
   setupURL(str, rem, src);
@@ -89,17 +132,46 @@ std::string MiniCurl::getURL(const std::string& str, const ComboAddress* rem, co
   return ret;
 }
 
-std::string MiniCurl::postURL(const std::string& str, const std::string& postdata)
+std::string MiniCurl::postURL(const std::string& str, const std::string& postdata, MiniCurlHeaders& headers)
 {
   setupURL(str);
+  setHeaders(headers);
+  curl_easy_setopt(d_curl, CURLOPT_POSTFIELDSIZE, postdata.size());
   curl_easy_setopt(d_curl, CURLOPT_POSTFIELDS, postdata.c_str());
 
   auto res = curl_easy_perform(d_curl);
-  if(res != CURLE_OK) 
-    throw std::runtime_error("Unable to post URL");
+
+  long http_code = 0;
+  curl_easy_getinfo(d_curl, CURLINFO_RESPONSE_CODE, &http_code);
+
+  if(res != CURLE_OK)
+    throw std::runtime_error("Unable to post URL ("+std::to_string(http_code)+"): "+string(curl_easy_strerror(res)));
 
   std::string ret=d_data;
 
   d_data.clear();
   return ret;
 }
+
+void MiniCurl::clearHeaders()
+{
+  if (d_curl) {
+    curl_easy_setopt(d_curl, CURLOPT_HTTPHEADER, NULL);
+    if (d_header_list != nullptr) {
+      curl_slist_free_all(d_header_list);
+      d_header_list = nullptr;
+    }
+  }
+}
+
+void MiniCurl::setHeaders(const MiniCurlHeaders& headers)
+{
+  if (d_curl) {
+    for (auto& header : headers) {
+      std::stringstream header_ss;
+      header_ss << header.first << ": " << header.second;
+      d_header_list = curl_slist_append(d_header_list, header_ss.str().c_str());
+    }
+    curl_easy_setopt(d_curl, CURLOPT_HTTPHEADER, d_header_list);
+  }
+}