Merging bzr://gaz.tangent.org/libmemcached/build/ to Build branch
[awesomized/libmemcached] / libtest / http.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * libtest
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include <config.h>
23
24 #include <libtest/common.h>
25
26 #if defined(HAVE_CURL_CURL_H) && HAVE_CURL_CURL_H
27 #include <curl/curl.h>
28 #else
29 class CURL;
30 #endif
31
32
33 static void cleanup_curl(void)
34 {
35 #if defined(HAVE_CURL_CURL_H) && HAVE_CURL_CURL_H
36 curl_global_cleanup();
37 #endif
38 }
39
40 static void initialize_curl_startup()
41 {
42 #if defined(HAVE_CURL_CURL_H) && HAVE_CURL_CURL_H
43 if (curl_global_init(CURL_GLOBAL_ALL))
44 {
45 fatal_message("curl_global_init(CURL_GLOBAL_ALL) failed");
46 }
47 #endif
48
49 if (atexit(cleanup_curl))
50 {
51 fatal_message("atexit() failed");
52 }
53 }
54
55 static pthread_once_t start_key_once= PTHREAD_ONCE_INIT;
56 void initialize_curl(void)
57 {
58 int ret;
59 if ((ret= pthread_once(&start_key_once, initialize_curl_startup)) != 0)
60 {
61 fatal_message(strerror(ret));
62 }
63 }
64
65 namespace libtest {
66 namespace http {
67
68 #define YATL_USERAGENT "YATL/1.0"
69
70 extern "C" size_t
71 http_get_result_callback(void *ptr, size_t size, size_t nmemb, void *data)
72 {
73 vchar_t *_body= (vchar_t*)data;
74
75 _body->resize(size * nmemb);
76 memcpy(&_body[0], ptr, _body->size());
77
78 return _body->size();
79 }
80
81
82 static void init(CURL *curl, const std::string& url)
83 {
84 if (HAVE_LIBCURL)
85 {
86 #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
87 assert(curl);
88 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
89 curl_easy_setopt(curl, CURLOPT_USERAGENT, YATL_USERAGENT);
90 #endif
91 }
92 }
93
94 HTTP::HTTP(const std::string& url_arg) :
95 _url(url_arg),
96 _response(0)
97 {
98 initialize_curl();
99 }
100
101 bool GET::execute()
102 {
103 (void)init;
104
105 if (HAVE_LIBCURL)
106 {
107 #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
108 CURL *curl= curl_easy_init();
109
110 init(curl, url());
111
112 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, http_get_result_callback);
113 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&_body);
114
115 CURLcode retref= curl_easy_perform(curl);
116 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, _response);
117
118 curl_easy_cleanup(curl);
119
120 return retref == CURLE_OK;
121 #endif
122 }
123
124 return false;
125 }
126
127 bool POST::execute()
128 {
129 if (HAVE_LIBCURL)
130 {
131 #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
132 CURL *curl= curl_easy_init();;
133
134 init(curl, url());
135
136 curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, _body.size());
137 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, (void *)&_body[0]);
138
139 CURLcode retref= curl_easy_perform(curl);
140 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, _response);
141
142 curl_easy_cleanup(curl);
143 #endif
144 }
145
146 return false;
147 }
148
149 bool TRACE::execute()
150 {
151 if (HAVE_LIBCURL)
152 {
153 #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
154 CURL *curl= curl_easy_init();;
155
156 init(curl, url());
157
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]);
161
162 CURLcode retref= curl_easy_perform(curl);
163 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, _response);
164
165 curl_easy_cleanup(curl);
166
167 return retref == CURLE_OK;
168 #endif
169 }
170
171 return false;
172 }
173
174 bool HEAD::execute()
175 {
176 if (HAVE_LIBCURL)
177 {
178 #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
179 CURL *curl= curl_easy_init();;
180
181 init(curl, url());
182
183 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "HEAD");
184 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, http_get_result_callback);
185
186 CURLcode retref= curl_easy_perform(curl);
187 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, _response);
188
189 curl_easy_cleanup(curl);
190
191 return retref == CURLE_OK;
192 #endif
193 }
194
195 return false;
196 }
197
198 } // namespace http
199 } // namespace libtest