Update of libtest
[m6w6/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 #include <libtest/common.h>
24
25 #if defined(HAVE_CURL_CURL_H) && HAVE_CURL_CURL_H
26 #include <curl/curl.h>
27 #else
28 class CURL;
29 #endif
30
31 namespace libtest {
32 namespace http {
33
34 #define YATL_USERAGENT "YATL/1.0"
35
36 extern "C" size_t
37 http_get_result_callback(void *ptr, size_t size, size_t nmemb, void *data)
38 {
39 size_t body_size= size * nmemb;
40
41 vchar_t *_body= (vchar_t*)data;
42
43 _body->resize(size * nmemb);
44 memcpy(&_body[0], ptr, _body->size());
45
46 return _body->size();
47 }
48
49
50 static void init(CURL *curl, const std::string& url)
51 {
52 if (HAVE_LIBCURL)
53 {
54 #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
55 assert(curl);
56 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
57 curl_easy_setopt(curl, CURLOPT_USERAGENT, YATL_USERAGENT);
58 #endif
59 }
60 }
61
62 bool GET::execute()
63 {
64 if (HAVE_LIBCURL)
65 {
66 #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
67 CURL *curl= curl_easy_init();
68
69 init(curl, url());
70
71 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, http_get_result_callback);
72 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&_body);
73
74 CURLcode retref= curl_easy_perform(curl);
75 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, _response);
76
77 curl_easy_cleanup(curl);
78
79 return retref == CURLE_OK;
80 #endif
81 }
82
83 return false;
84 }
85
86 bool POST::execute()
87 {
88 if (HAVE_LIBCURL)
89 {
90 #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
91 CURL *curl= curl_easy_init();;
92
93 init(curl, url());
94
95 curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, _body.size());
96 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, (void *)&_body[0]);
97
98 CURLcode retref= curl_easy_perform(curl);
99 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, _response);
100
101 curl_easy_cleanup(curl);
102 #endif
103 }
104
105 return false;
106 }
107
108 bool TRACE::execute()
109 {
110 if (HAVE_LIBCURL)
111 {
112 #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
113 CURL *curl= curl_easy_init();;
114
115 init(curl, url());
116
117 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "TRACE");
118 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, http_get_result_callback);
119 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&_body[0]);
120
121 CURLcode retref= curl_easy_perform(curl);
122 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, _response);
123
124 curl_easy_cleanup(curl);
125
126 return retref == CURLE_OK;
127 #endif
128 }
129
130 return false;
131 }
132
133 bool HEAD::execute()
134 {
135 if (HAVE_LIBCURL)
136 {
137 #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
138 CURL *curl= curl_easy_init();;
139
140 init(curl, url());
141
142 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "HEAD");
143 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, http_get_result_callback);
144
145 CURLcode retref= curl_easy_perform(curl);
146 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, _response);
147
148 curl_easy_cleanup(curl);
149
150 return retref == CURLE_OK;
151 #endif
152 }
153
154 return false;
155 }
156
157 } // namespace http
158 } // namespace libtest