aboutsummaryrefslogtreecommitdiff
path: root/src/daemon/daemontest_get.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/daemon/daemontest_get.c')
-rw-r--r--src/daemon/daemontest_get.c470
1 files changed, 197 insertions, 273 deletions
diff --git a/src/daemon/daemontest_get.c b/src/daemon/daemontest_get.c
index 08de296d..f200a4c8 100644
--- a/src/daemon/daemontest_get.c
+++ b/src/daemon/daemontest_get.c
@@ -35,210 +35,167 @@
35 35
36static int oneone; 36static int oneone;
37 37
38struct CBC { 38struct CBC
39 char * buf; 39{
40 char *buf;
40 size_t pos; 41 size_t pos;
41 size_t size; 42 size_t size;
42}; 43};
43 44
44static size_t copyBuffer(void * ptr, 45static size_t
45 size_t size, 46copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
46 size_t nmemb, 47{
47 void * ctx) { 48 struct CBC *cbc = ctx;
48 struct CBC * cbc = ctx;
49 49
50 if (cbc->pos + size * nmemb > cbc->size) 50 if (cbc->pos + size * nmemb > cbc->size)
51 return 0; /* overflow */ 51 return 0; /* overflow */
52 memcpy(&cbc->buf[cbc->pos], 52 memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
53 ptr,
54 size * nmemb);
55 cbc->pos += size * nmemb; 53 cbc->pos += size * nmemb;
56 return size * nmemb; 54 return size * nmemb;
57} 55}
58 56
59static int ahc_echo(void * cls, 57static int
60 struct MHD_Connection * connection, 58ahc_echo (void *cls,
61 const char * url, 59 struct MHD_Connection *connection,
62 const char * method, 60 const char *url,
63 const char * version, 61 const char *method,
64 const char * upload_data, 62 const char *version,
65 unsigned int * upload_data_size) { 63 const char *upload_data, unsigned int *upload_data_size)
66 const char * me = cls; 64{
67 struct MHD_Response * response; 65 const char *me = cls;
66 struct MHD_Response *response;
68 int ret; 67 int ret;
69 68
70 if (0 != strcmp(me, method)) 69 if (0 != strcmp (me, method))
71 return MHD_NO; /* unexpected method */ 70 return MHD_NO; /* unexpected method */
72 response = MHD_create_response_from_data(strlen(url), 71 response = MHD_create_response_from_data (strlen (url),
73 (void*) url, 72 (void *) url, MHD_NO, MHD_YES);
74 MHD_NO, 73 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
75 MHD_YES); 74 MHD_destroy_response (response);
76 ret = MHD_queue_response(connection,
77 MHD_HTTP_OK,
78 response);
79 MHD_destroy_response(response);
80 return ret; 75 return ret;
81} 76}
82 77
83 78
84static int testInternalGet() { 79static int
85 struct MHD_Daemon * d; 80testInternalGet ()
86 CURL * c; 81{
82 struct MHD_Daemon *d;
83 CURL *c;
87 char buf[2048]; 84 char buf[2048];
88 struct CBC cbc; 85 struct CBC cbc;
89 86
90 cbc.buf = buf; 87 cbc.buf = buf;
91 cbc.size = 2048; 88 cbc.size = 2048;
92 cbc.pos = 0; 89 cbc.pos = 0;
93 d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG, 90 d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
94 1080, 91 1080, NULL, NULL, &ahc_echo, "GET", MHD_OPTION_END);
95 NULL,
96 NULL,
97 &ahc_echo,
98 "GET",
99 MHD_OPTION_END);
100 if (d == NULL) 92 if (d == NULL)
101 return 1; 93 return 1;
102 c = curl_easy_init(); 94 c = curl_easy_init ();
103 curl_easy_setopt(c, 95 curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1080/hello_world");
104 CURLOPT_URL, 96 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
105 "http://localhost:1080/hello_world"); 97 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
106 curl_easy_setopt(c, 98 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
107 CURLOPT_WRITEFUNCTION, 99 curl_easy_setopt (c, CURLOPT_TIMEOUT, 2L);
108 &copyBuffer); 100 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 2L);
109 curl_easy_setopt(c,
110 CURLOPT_WRITEDATA,
111 &cbc);
112 curl_easy_setopt(c,
113 CURLOPT_FAILONERROR,
114 1);
115 curl_easy_setopt(c,
116 CURLOPT_TIMEOUT,
117 2L);
118 curl_easy_setopt(c,
119 CURLOPT_CONNECTTIMEOUT,
120 2L);
121 if (oneone) 101 if (oneone)
122 curl_easy_setopt(c, 102 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
123 CURLOPT_HTTP_VERSION,
124 CURL_HTTP_VERSION_1_1);
125 else 103 else
126 curl_easy_setopt(c, 104 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
127 CURLOPT_HTTP_VERSION, 105 // NOTE: use of CONNECTTIMEOUT without also
128 CURL_HTTP_VERSION_1_0);
129 // NOTE: use of CONNECTTIMEOUT without also
130 // setting NOSIGNAL results in really weird 106 // setting NOSIGNAL results in really weird
131 // crashes on my system! 107 // crashes on my system!
132 curl_easy_setopt(c, 108 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
133 CURLOPT_NOSIGNAL, 109 if (CURLE_OK != curl_easy_perform (c))
134 1); 110 {
135 if (CURLE_OK != curl_easy_perform(c)) { 111 curl_easy_cleanup (c);
136 curl_easy_cleanup(c); 112 MHD_stop_daemon (d);
137 MHD_stop_daemon(d); 113 return 2;
138 return 2; 114 }
139 } 115 curl_easy_cleanup (c);
140 curl_easy_cleanup(c); 116 if (cbc.pos != strlen ("/hello_world"))
141 if (cbc.pos != strlen("/hello_world")) { 117 {
142 MHD_stop_daemon(d); 118 MHD_stop_daemon (d);
143 return 4; 119 return 4;
144 } 120 }
145 121
146 if (0 != strncmp("/hello_world", 122 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
147 cbc.buf, 123 {
148 strlen("/hello_world"))) { 124 MHD_stop_daemon (d);
149 MHD_stop_daemon(d); 125 return 8;
150 return 8; 126 }
151 } 127 MHD_stop_daemon (d);
152 MHD_stop_daemon(d);
153 128
154 return 0; 129 return 0;
155} 130}
156 131
157static int testMultithreadedGet() { 132static int
158 struct MHD_Daemon * d; 133testMultithreadedGet ()
159 CURL * c; 134{
135 struct MHD_Daemon *d;
136 CURL *c;
160 char buf[2048]; 137 char buf[2048];
161 struct CBC cbc; 138 struct CBC cbc;
162 139
163 cbc.buf = buf; 140 cbc.buf = buf;
164 cbc.size = 2048; 141 cbc.size = 2048;
165 cbc.pos = 0; 142 cbc.pos = 0;
166 d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, 143 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
167 1081, 144 1081, NULL, NULL, &ahc_echo, "GET", MHD_OPTION_END);
168 NULL,
169 NULL,
170 &ahc_echo,
171 "GET",
172 MHD_OPTION_END);
173 if (d == NULL) 145 if (d == NULL)
174 return 16; 146 return 16;
175 c = curl_easy_init(); 147 c = curl_easy_init ();
176 curl_easy_setopt(c, 148 curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1081/hello_world");
177 CURLOPT_URL, 149 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
178 "http://localhost:1081/hello_world"); 150 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
179 curl_easy_setopt(c, 151 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
180 CURLOPT_WRITEFUNCTION, 152 curl_easy_setopt (c, CURLOPT_TIMEOUT, 2L);
181 &copyBuffer);
182 curl_easy_setopt(c,
183 CURLOPT_WRITEDATA,
184 &cbc);
185 curl_easy_setopt(c,
186 CURLOPT_FAILONERROR,
187 1);
188 curl_easy_setopt(c,
189 CURLOPT_TIMEOUT,
190 2L);
191 if (oneone) 153 if (oneone)
192 curl_easy_setopt(c, 154 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
193 CURLOPT_HTTP_VERSION,
194 CURL_HTTP_VERSION_1_1);
195 else 155 else
196 curl_easy_setopt(c, 156 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
197 CURLOPT_HTTP_VERSION, 157 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 2L);
198 CURL_HTTP_VERSION_1_0);
199 curl_easy_setopt(c,
200 CURLOPT_CONNECTTIMEOUT,
201 2L);
202 // NOTE: use of CONNECTTIMEOUT without also 158 // NOTE: use of CONNECTTIMEOUT without also
203 // setting NOSIGNAL results in really weird 159 // setting NOSIGNAL results in really weird
204 // crashes on my system! 160 // crashes on my system!
205 curl_easy_setopt(c, 161 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
206 CURLOPT_NOSIGNAL, 162 if (CURLE_OK != curl_easy_perform (c))
207 1); 163 {
208 if (CURLE_OK != curl_easy_perform(c)) { 164 MHD_stop_daemon (d);
209 MHD_stop_daemon(d); 165 return 32;
210 return 32; 166 }
211 } 167 curl_easy_cleanup (c);
212 curl_easy_cleanup(c); 168 if (cbc.pos != strlen ("/hello_world"))
213 if (cbc.pos != strlen("/hello_world")) { 169 {
214 MHD_stop_daemon(d); 170 MHD_stop_daemon (d);
215 return 64; 171 return 64;
216 } 172 }
217 if (0 != strncmp("/hello_world", 173 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
218 cbc.buf, 174 {
219 strlen("/hello_world"))) { 175 MHD_stop_daemon (d);
220 MHD_stop_daemon(d); 176 return 128;
221 return 128; 177 }
222 } 178 MHD_stop_daemon (d);
223 MHD_stop_daemon(d);
224 179
225 return 0; 180 return 0;
226} 181}
227 182
228 183
229static int testExternalGet() { 184static int
230 struct MHD_Daemon * d; 185testExternalGet ()
231 CURL * c; 186{
187 struct MHD_Daemon *d;
188 CURL *c;
232 char buf[2048]; 189 char buf[2048];
233 struct CBC cbc; 190 struct CBC cbc;
234 CURLM * multi; 191 CURLM *multi;
235 CURLMcode mret; 192 CURLMcode mret;
236 fd_set rs; 193 fd_set rs;
237 fd_set ws; 194 fd_set ws;
238 fd_set es; 195 fd_set es;
239 int max; 196 int max;
240 int running; 197 int running;
241 struct CURLMsg * msg; 198 struct CURLMsg *msg;
242 time_t start; 199 time_t start;
243 struct timeval tv; 200 struct timeval tv;
244 201
@@ -246,154 +203,121 @@ static int testExternalGet() {
246 cbc.buf = buf; 203 cbc.buf = buf;
247 cbc.size = 2048; 204 cbc.size = 2048;
248 cbc.pos = 0; 205 cbc.pos = 0;
249 d = MHD_start_daemon(MHD_USE_DEBUG, 206 d = MHD_start_daemon (MHD_USE_DEBUG,
250 1082, 207 1082, NULL, NULL, &ahc_echo, "GET", MHD_OPTION_END);
251 NULL,
252 NULL,
253 &ahc_echo,
254 "GET",
255 MHD_OPTION_END);
256 if (d == NULL) 208 if (d == NULL)
257 return 256; 209 return 256;
258 c = curl_easy_init(); 210 c = curl_easy_init ();
259 curl_easy_setopt(c, 211 curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1082/hello_world");
260 CURLOPT_URL, 212 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
261 "http://localhost:1082/hello_world"); 213 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
262 curl_easy_setopt(c, 214 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
263 CURLOPT_WRITEFUNCTION,
264 &copyBuffer);
265 curl_easy_setopt(c,
266 CURLOPT_WRITEDATA,
267 &cbc);
268 curl_easy_setopt(c,
269 CURLOPT_FAILONERROR,
270 1);
271 if (oneone) 215 if (oneone)
272 curl_easy_setopt(c, 216 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
273 CURLOPT_HTTP_VERSION,
274 CURL_HTTP_VERSION_1_1);
275 else 217 else
276 curl_easy_setopt(c, 218 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
277 CURLOPT_HTTP_VERSION, 219 curl_easy_setopt (c, CURLOPT_TIMEOUT, 5L);
278 CURL_HTTP_VERSION_1_0); 220 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 5L);
279 curl_easy_setopt(c,
280 CURLOPT_TIMEOUT,
281 5L);
282 curl_easy_setopt(c,
283 CURLOPT_CONNECTTIMEOUT,
284 5L);
285 // NOTE: use of CONNECTTIMEOUT without also 221 // NOTE: use of CONNECTTIMEOUT without also
286 // setting NOSIGNAL results in really weird 222 // setting NOSIGNAL results in really weird
287 // crashes on my system! 223 // crashes on my system!
288 curl_easy_setopt(c, 224 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
289 CURLOPT_NOSIGNAL,
290 1);
291 225
292 226
293 multi = curl_multi_init(); 227 multi = curl_multi_init ();
294 if (multi == NULL) { 228 if (multi == NULL)
295 curl_easy_cleanup(c); 229 {
296 MHD_stop_daemon(d); 230 curl_easy_cleanup (c);
297 return 512; 231 MHD_stop_daemon (d);
298 } 232 return 512;
299 mret = curl_multi_add_handle(multi, c); 233 }
300 if (mret != CURLM_OK) { 234 mret = curl_multi_add_handle (multi, c);
301 curl_multi_cleanup(multi); 235 if (mret != CURLM_OK)
302 curl_easy_cleanup(c); 236 {
303 MHD_stop_daemon(d); 237 curl_multi_cleanup (multi);
304 return 1024; 238 curl_easy_cleanup (c);
305 } 239 MHD_stop_daemon (d);
306 start = time(NULL); 240 return 1024;
307 while ( (time(NULL) - start < 5) && 241 }
308 (multi != NULL) ) { 242 start = time (NULL);
309 max = 0; 243 while ((time (NULL) - start < 5) && (multi != NULL))
310 FD_ZERO(&rs); 244 {
311 FD_ZERO(&ws); 245 max = 0;
312 FD_ZERO(&es); 246 FD_ZERO (&rs);
313 curl_multi_perform(multi, &running); 247 FD_ZERO (&ws);
314 mret = curl_multi_fdset(multi, 248 FD_ZERO (&es);
315 &rs, 249 curl_multi_perform (multi, &running);
316 &ws, 250 mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
317 &es, 251 if (mret != CURLM_OK)
318 &max); 252 {
319 if (mret != CURLM_OK) { 253 curl_multi_remove_handle (multi, c);
320 curl_multi_remove_handle(multi, c); 254 curl_multi_cleanup (multi);
321 curl_multi_cleanup(multi); 255 curl_easy_cleanup (c);
322 curl_easy_cleanup(c); 256 MHD_stop_daemon (d);
323 MHD_stop_daemon(d); 257 return 2048;
324 return 2048; 258 }
259 if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max))
260 {
261 curl_multi_remove_handle (multi, c);
262 curl_multi_cleanup (multi);
263 curl_easy_cleanup (c);
264 MHD_stop_daemon (d);
265 return 4096;
266 }
267 tv.tv_sec = 0;
268 tv.tv_usec = 1000;
269 select (max + 1, &rs, &ws, &es, &tv);
270 curl_multi_perform (multi, &running);
271 if (running == 0)
272 {
273 msg = curl_multi_info_read (multi, &running);
274 if (msg == NULL)
275 break;
276 if (msg->msg == CURLMSG_DONE)
277 {
278 if (msg->data.result != CURLE_OK)
279 printf ("%s failed at %s:%d: `%s'\n",
280 "curl_multi_perform",
281 __FILE__,
282 __LINE__, curl_easy_strerror (msg->data.result));
283 curl_multi_remove_handle (multi, c);
284 curl_multi_cleanup (multi);
285 curl_easy_cleanup (c);
286 c = NULL;
287 multi = NULL;
288 }
289 }
290 MHD_run (d);
325 } 291 }
326 if (MHD_YES != MHD_get_fdset(d, 292 if (multi != NULL)
327 &rs, 293 {
328 &ws, 294 curl_multi_remove_handle (multi, c);
329 &es, 295 curl_easy_cleanup (c);
330 &max)) { 296 curl_multi_cleanup (multi);
331 curl_multi_remove_handle(multi, c);
332 curl_multi_cleanup(multi);
333 curl_easy_cleanup(c);
334 MHD_stop_daemon(d);
335 return 4096;
336 } 297 }
337 tv.tv_sec = 0; 298 MHD_stop_daemon (d);
338 tv.tv_usec = 1000; 299 if (cbc.pos != strlen ("/hello_world"))
339 select(max + 1,
340 &rs,
341 &ws,
342 &es,
343 &tv);
344 curl_multi_perform(multi, &running);
345 if (running == 0) {
346 msg = curl_multi_info_read(multi,
347 &running);
348 if (msg == NULL)
349 break;
350 if (msg->msg == CURLMSG_DONE) {
351 if (msg->data.result != CURLE_OK)
352 printf("%s failed at %s:%d: `%s'\n",
353 "curl_multi_perform",
354 __FILE__,
355 __LINE__,
356 curl_easy_strerror(msg->data.result));
357 curl_multi_remove_handle(multi, c);
358 curl_multi_cleanup(multi);
359 curl_easy_cleanup(c);
360 c = NULL;
361 multi = NULL;
362 }
363 }
364 MHD_run(d);
365 }
366 if (multi != NULL) {
367 curl_multi_remove_handle(multi, c);
368 curl_easy_cleanup(c);
369 curl_multi_cleanup(multi);
370 }
371 MHD_stop_daemon(d);
372 if (cbc.pos != strlen("/hello_world"))
373 return 8192; 300 return 8192;
374 if (0 != strncmp("/hello_world", 301 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
375 cbc.buf,
376 strlen("/hello_world")))
377 return 16384; 302 return 16384;
378 return 0; 303 return 0;
379} 304}
380 305
381 306
382 307
383int main(int argc, 308int
384 char * const * argv) { 309main (int argc, char *const *argv)
310{
385 unsigned int errorCount = 0; 311 unsigned int errorCount = 0;
386 312
387 oneone = NULL != strstr(argv[0], "11"); 313 oneone = NULL != strstr (argv[0], "11");
388 if (0 != curl_global_init(CURL_GLOBAL_WIN32)) 314 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
389 return 2; 315 return 2;
390 errorCount += testInternalGet(); 316 errorCount += testInternalGet ();
391 errorCount += testMultithreadedGet(); 317 errorCount += testMultithreadedGet ();
392 errorCount += testExternalGet(); 318 errorCount += testExternalGet ();
393 if (errorCount != 0) 319 if (errorCount != 0)
394 fprintf(stderr, 320 fprintf (stderr, "Error (code: %u)\n", errorCount);
395 "Error (code: %u)\n", 321 curl_global_cleanup ();
396 errorCount); 322 return errorCount != 0; /* 0 == pass */
397 curl_global_cleanup();
398 return errorCount != 0; /* 0 == pass */
399} 323}