jenkins-promote-staging-trunk-libmemcached-9
[m6w6/libmemcached] / libtest / http.cc
index 5309d50447a7dea2bdbadc047e2caea445917dc3..8c36371d0827529aab2db90c5155e2908fc8a97c 100644 (file)
@@ -19,6 +19,8 @@
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
+#include <config.h>
+
 #include <libtest/common.h>
 
 #if defined(HAVE_CURL_CURL_H) && HAVE_CURL_CURL_H
 class CURL;
 #endif
 
+
+static void cleanup_curl(void)
+{
+#if defined(HAVE_CURL_CURL_H) && HAVE_CURL_CURL_H
+  curl_global_cleanup();
+#endif
+}
+
+static void initialize_curl_startup()
+{
+#if defined(HAVE_CURL_CURL_H) && HAVE_CURL_CURL_H
+  if (curl_global_init(CURL_GLOBAL_ALL))
+  {
+    fatal_message("curl_global_init(CURL_GLOBAL_ALL) failed");
+  }
+#endif
+
+  if (atexit(cleanup_curl))
+  {
+    fatal_message("atexit() failed");
+  }
+}
+
+static pthread_once_t start_key_once= PTHREAD_ONCE_INIT;
+void initialize_curl(void)
+{
+  int ret;
+  if ((ret= pthread_once(&start_key_once, initialize_curl_startup)) != 0)
+  {
+    fatal_message(strerror(ret));
+  }
+}
+
 namespace libtest {
 namespace http {
 
@@ -35,8 +70,6 @@ namespace http {
 extern "C" size_t
   http_get_result_callback(void *ptr, size_t size, size_t nmemb, void *data)
   {
-    size_t body_size= size * nmemb;
-
     vchar_t *_body= (vchar_t*)data;
 
     _body->resize(size * nmemb);
@@ -58,8 +91,17 @@ static void init(CURL *curl, const std::string& url)
   }
 }
 
+HTTP::HTTP(const std::string& url_arg) :
+  _url(url_arg),
+  _response(0)
+{
+  initialize_curl();
+}
+
 bool GET::execute()
 {
+  (void)init;
+
   if (HAVE_LIBCURL)
   {
 #if defined(HAVE_LIBCURL) && HAVE_LIBCURL