1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <libtest/common.h>
26 #if defined(HAVE_CURL_CURL_H) && HAVE_CURL_CURL_H
27 #include <curl/curl.h>
33 static void cleanup_curl(void)
35 #if defined(HAVE_CURL_CURL_H) && HAVE_CURL_CURL_H
36 curl_global_cleanup();
40 static void initialize_curl_startup()
42 #if defined(HAVE_CURL_CURL_H) && HAVE_CURL_CURL_H
43 if (curl_global_init(CURL_GLOBAL_ALL
))
45 fatal_message("curl_global_init(CURL_GLOBAL_ALL) failed");
49 if (atexit(cleanup_curl
))
51 fatal_message("atexit() failed");
55 static pthread_once_t start_key_once
= PTHREAD_ONCE_INIT
;
56 void initialize_curl(void)
59 if ((ret
= pthread_once(&start_key_once
, initialize_curl_startup
)) != 0)
61 fatal_message(strerror(ret
));
68 #define YATL_USERAGENT "YATL/1.0"
71 http_get_result_callback(void *ptr
, size_t size
, size_t nmemb
, void *data
)
73 vchar_t
*_body
= (vchar_t
*)data
;
75 _body
->resize(size
* nmemb
);
76 memcpy(&_body
[0], ptr
, _body
->size());
82 static void init(CURL
*curl
, const std::string
& url
)
86 #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
88 curl_easy_setopt(curl
, CURLOPT_URL
, url
.c_str());
89 curl_easy_setopt(curl
, CURLOPT_USERAGENT
, YATL_USERAGENT
);
94 HTTP::HTTP(const std::string
& url_arg
) :
107 #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
108 CURL
*curl
= curl_easy_init();
112 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, http_get_result_callback
);
113 curl_easy_setopt(curl
, CURLOPT_WRITEDATA
, (void *)&_body
);
115 CURLcode retref
= curl_easy_perform(curl
);
116 curl_easy_getinfo(curl
, CURLINFO_RESPONSE_CODE
, _response
);
118 curl_easy_cleanup(curl
);
120 return retref
== CURLE_OK
;
131 #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
132 CURL
*curl
= curl_easy_init();;
136 curl_easy_setopt(curl
, CURLOPT_POSTFIELDSIZE
, _body
.size());
137 curl_easy_setopt(curl
, CURLOPT_POSTFIELDS
, (void *)&_body
[0]);
139 CURLcode retref
= curl_easy_perform(curl
);
140 curl_easy_getinfo(curl
, CURLINFO_RESPONSE_CODE
, _response
);
142 curl_easy_cleanup(curl
);
149 bool TRACE::execute()
153 #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
154 CURL
*curl
= curl_easy_init();;
158 curl_easy_setopt(curl
, CURLOPT_CUSTOMREQUEST
, "TRACE");
159 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, http_get_result_callback
);
160 curl_easy_setopt(curl
, CURLOPT_WRITEDATA
, (void *)&_body
[0]);
162 CURLcode retref
= curl_easy_perform(curl
);
163 curl_easy_getinfo(curl
, CURLINFO_RESPONSE_CODE
, _response
);
165 curl_easy_cleanup(curl
);
167 return retref
== CURLE_OK
;
178 #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
179 CURL
*curl
= curl_easy_init();;
183 curl_easy_setopt(curl
, CURLOPT_CUSTOMREQUEST
, "HEAD");
184 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, http_get_result_callback
);
186 CURLcode retref
= curl_easy_perform(curl
);
187 curl_easy_getinfo(curl
, CURLINFO_RESPONSE_CODE
, _response
);
189 curl_easy_cleanup(curl
);
191 return retref
== CURLE_OK
;
199 } // namespace libtest