]> 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 a02239a44bb535ce450c4439066d2bf7f7e61882..e8ba8e9edad4b13f5f4b5203bf64d933a1cebfb9 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * MIT License
  *
- * Copyright (c) 2018 powerdns.com bv
+ * 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
 #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()
@@ -98,6 +115,7 @@ void MiniCurl::setupURL(const std::string& str, const ComboAddress* rem, const C
 
   d_data.clear();
 }
+
 std::string MiniCurl::getURL(const std::string& str, const ComboAddress* rem, const ComboAddress* src)
 {
   setupURL(str, rem, src);