Merge from libtest.
[awesomized/libmemcached] / libtest / http.cc
index 1a0a10ec0525a973368aa0283bdaec3abc57d3af..920fd021d647f34d1a01d257adcecad100b5b892 100644 (file)
@@ -20,6 +20,7 @@
  */
 
 #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 (pthread_once(&start_key_once, initialize_curl_startup) != 0)
+  {
+    fatal_message(strerror(ret));
+  }
+}
+
 namespace libtest {
 namespace http {
 
@@ -59,6 +93,13 @@ 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()
 {
   if (HAVE_LIBCURL)