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