1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 * Data Differential YATL (i.e. libtest) library
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include "libtest/yatlcon.h"
39 #include <libtest/common.h>
41 #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
42 #include <curl/curl.h>
48 static void cleanup_curl(void)
50 #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
51 curl_global_cleanup();
55 static void initialize_curl_startup()
57 #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
58 if (curl_global_init(CURL_GLOBAL_ALL
))
60 fatal_message("curl_global_init(CURL_GLOBAL_ALL) failed");
64 if (atexit(cleanup_curl
))
66 fatal_message("atexit() failed");
70 static pthread_once_t start_key_once
= PTHREAD_ONCE_INIT
;
71 static void initialize_curl(void)
74 if ((ret
= pthread_once(&start_key_once
, initialize_curl_startup
)) != 0)
76 fatal_message(strerror(ret
));
83 #define YATL_USERAGENT "YATL/1.0"
85 static size_t http_get_result_callback(void *ptr
, size_t size
, size_t nmemb
, void *data
)
87 vchar_t
*_body
= (vchar_t
*)data
;
89 _body
->resize(size
* nmemb
);
90 memcpy(&_body
[0], ptr
, _body
->size());
95 static void init(CURL
*curl
, const std::string
& url
)
97 (void)http_get_result_callback
;
102 #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
104 curl_easy_setopt(curl
, CURLOPT_URL
, url
.c_str());
105 curl_easy_setopt(curl
, CURLOPT_USERAGENT
, YATL_USERAGENT
);
110 HTTP::HTTP(const std::string
& url_arg
) :
123 #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
124 CURL
*curl
= curl_easy_init();
128 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, http_get_result_callback
);
129 curl_easy_setopt(curl
, CURLOPT_WRITEDATA
, (void *)&_body
);
131 CURLcode retref
= curl_easy_perform(curl
);
132 curl_easy_getinfo(curl
, CURLINFO_RESPONSE_CODE
, _response
);
134 curl_easy_cleanup(curl
);
136 return bool(retref
== CURLE_OK
);
147 #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
148 CURL
*curl
= curl_easy_init();;
152 curl_easy_setopt(curl
, CURLOPT_POSTFIELDSIZE
, _body
.size());
153 curl_easy_setopt(curl
, CURLOPT_POSTFIELDS
, (void *)&_body
[0]);
155 CURLcode retref
= curl_easy_perform(curl
);
156 curl_easy_getinfo(curl
, CURLINFO_RESPONSE_CODE
, _response
);
158 curl_easy_cleanup(curl
);
160 return bool(retref
== CURLE_OK
);
167 bool TRACE::execute()
171 #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
172 CURL
*curl
= curl_easy_init();;
176 curl_easy_setopt(curl
, CURLOPT_CUSTOMREQUEST
, "TRACE");
177 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, http_get_result_callback
);
178 curl_easy_setopt(curl
, CURLOPT_WRITEDATA
, (void *)&_body
[0]);
180 CURLcode retref
= curl_easy_perform(curl
);
181 curl_easy_getinfo(curl
, CURLINFO_RESPONSE_CODE
, _response
);
183 curl_easy_cleanup(curl
);
185 return retref
== CURLE_OK
;
196 #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
197 CURL
*curl
= curl_easy_init();;
201 curl_easy_setopt(curl
, CURLOPT_CUSTOMREQUEST
, "HEAD");
202 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, http_get_result_callback
);
204 CURLcode retref
= curl_easy_perform(curl
);
205 curl_easy_getinfo(curl
, CURLINFO_RESPONSE_CODE
, _response
);
207 curl_easy_cleanup(curl
);
209 return retref
== CURLE_OK
;
217 } // namespace libtest