9933e12f54eb4ac463c964d3e7e69dbd09b39db4
[awesomized/libmemcached] / libtest / http.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Data Differential YATL (i.e. libtest) library
4 *
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
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
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
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.
34 *
35 */
36
37 #include <config.h>
38
39 #include <libtest/common.h>
40
41 #if defined(HAVE_CURL_CURL_H) && HAVE_CURL_CURL_H
42 #include <curl/curl.h>
43 #else
44 class CURL;
45 #endif
46
47
48 static void cleanup_curl(void)
49 {
50 #if defined(HAVE_CURL_CURL_H) && HAVE_CURL_CURL_H
51 curl_global_cleanup();
52 #endif
53 }
54
55 static void initialize_curl_startup()
56 {
57 #if defined(HAVE_CURL_CURL_H) && HAVE_CURL_CURL_H
58 if (curl_global_init(CURL_GLOBAL_ALL))
59 {
60 fatal_message("curl_global_init(CURL_GLOBAL_ALL) failed");
61 }
62 #endif
63
64 if (atexit(cleanup_curl))
65 {
66 fatal_message("atexit() failed");
67 }
68 }
69
70 static pthread_once_t start_key_once= PTHREAD_ONCE_INIT;
71 void initialize_curl(void)
72 {
73 int ret;
74 if ((ret= pthread_once(&start_key_once, initialize_curl_startup)) != 0)
75 {
76 fatal_message(strerror(ret));
77 }
78 }
79
80 namespace libtest {
81 namespace http {
82
83 #define YATL_USERAGENT "YATL/1.0"
84
85 extern "C" size_t
86 http_get_result_callback(void *ptr, size_t size, size_t nmemb, void *data)
87 {
88 vchar_t *_body= (vchar_t*)data;
89
90 _body->resize(size * nmemb);
91 memcpy(&_body[0], ptr, _body->size());
92
93 return _body->size();
94 }
95
96
97 static void init(CURL *curl, const std::string& url)
98 {
99 (void)curl;
100 (void)url;
101 if (HAVE_LIBCURL)
102 {
103 #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
104 assert(curl);
105 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
106 curl_easy_setopt(curl, CURLOPT_USERAGENT, YATL_USERAGENT);
107 #endif
108 }
109 }
110
111 HTTP::HTTP(const std::string& url_arg) :
112 _url(url_arg),
113 _response(0)
114 {
115 initialize_curl();
116 }
117
118 bool GET::execute()
119 {
120 (void)init;
121
122 if (HAVE_LIBCURL)
123 {
124 #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
125 CURL *curl= curl_easy_init();
126
127 init(curl, url());
128
129 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, http_get_result_callback);
130 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&_body);
131
132 CURLcode retref= curl_easy_perform(curl);
133 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, _response);
134
135 curl_easy_cleanup(curl);
136
137 return retref == CURLE_OK;
138 #endif
139 }
140
141 return false;
142 }
143
144 bool POST::execute()
145 {
146 if (HAVE_LIBCURL)
147 {
148 #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
149 CURL *curl= curl_easy_init();;
150
151 init(curl, url());
152
153 curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, _body.size());
154 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, (void *)&_body[0]);
155
156 CURLcode retref= curl_easy_perform(curl);
157 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, _response);
158
159 curl_easy_cleanup(curl);
160 #endif
161 }
162
163 return false;
164 }
165
166 bool TRACE::execute()
167 {
168 if (HAVE_LIBCURL)
169 {
170 #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
171 CURL *curl= curl_easy_init();;
172
173 init(curl, url());
174
175 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "TRACE");
176 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, http_get_result_callback);
177 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&_body[0]);
178
179 CURLcode retref= curl_easy_perform(curl);
180 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, _response);
181
182 curl_easy_cleanup(curl);
183
184 return retref == CURLE_OK;
185 #endif
186 }
187
188 return false;
189 }
190
191 bool HEAD::execute()
192 {
193 if (HAVE_LIBCURL)
194 {
195 #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
196 CURL *curl= curl_easy_init();;
197
198 init(curl, url());
199
200 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "HEAD");
201 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, http_get_result_callback);
202
203 CURLcode retref= curl_easy_perform(curl);
204 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, _response);
205
206 curl_easy_cleanup(curl);
207
208 return retref == CURLE_OK;
209 #endif
210 }
211
212 return false;
213 }
214
215 } // namespace http
216 } // namespace libtest