diff options
Diffstat (limited to 'src/daemon/daemontest1.c')
-rw-r--r-- | src/daemon/daemontest1.c | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/src/daemon/daemontest1.c b/src/daemon/daemontest1.c index 1982de31..354da630 100644 --- a/src/daemon/daemontest1.c +++ b/src/daemon/daemontest1.c | |||
@@ -31,6 +31,7 @@ | |||
31 | #include <stdlib.h> | 31 | #include <stdlib.h> |
32 | #include <unistd.h> | 32 | #include <unistd.h> |
33 | #include <string.h> | 33 | #include <string.h> |
34 | #include <time.h> | ||
34 | 35 | ||
35 | static int apc_all(void * cls, | 36 | static int apc_all(void * cls, |
36 | const struct sockaddr * addr, | 37 | const struct sockaddr * addr, |
@@ -209,6 +210,152 @@ static int testMultithreadedGet() { | |||
209 | return 0; | 210 | return 0; |
210 | } | 211 | } |
211 | 212 | ||
213 | |||
214 | static int testExternalGet() { | ||
215 | struct MHD_Daemon * d; | ||
216 | CURL * c; | ||
217 | char buf[2048]; | ||
218 | struct CBC cbc; | ||
219 | CURLM * multi; | ||
220 | CURLMcode mret; | ||
221 | fd_set rs; | ||
222 | fd_set ws; | ||
223 | fd_set es; | ||
224 | int max; | ||
225 | int running; | ||
226 | struct CURLMsg * msg; | ||
227 | time_t start; | ||
228 | struct timeval tv; | ||
229 | |||
230 | multi = NULL; | ||
231 | cbc.buf = buf; | ||
232 | cbc.size = 2048; | ||
233 | cbc.pos = 0; | ||
234 | d = MHD_start_daemon(MHD_USE_IPv4 | MHD_USE_DEBUG, | ||
235 | 1082, | ||
236 | &apc_all, | ||
237 | NULL, | ||
238 | &ahc_echo, | ||
239 | "GET"); | ||
240 | if (d == NULL) | ||
241 | return 256; | ||
242 | c = curl_easy_init(); | ||
243 | curl_easy_setopt(c, | ||
244 | CURLOPT_URL, | ||
245 | "http://localhost:1082/hello_world"); | ||
246 | curl_easy_setopt(c, | ||
247 | CURLOPT_WRITEFUNCTION, | ||
248 | ©Buffer); | ||
249 | curl_easy_setopt(c, | ||
250 | CURLOPT_WRITEDATA, | ||
251 | &cbc); | ||
252 | curl_easy_setopt(c, | ||
253 | CURLOPT_FAILONERROR, | ||
254 | 1); | ||
255 | curl_easy_setopt(c, | ||
256 | CURLOPT_TIMEOUT, | ||
257 | 5L); | ||
258 | curl_easy_setopt(c, | ||
259 | CURLOPT_CONNECTTIMEOUT, | ||
260 | 5L); | ||
261 | // NOTE: use of CONNECTTIMEOUT without also | ||
262 | // setting NOSIGNAL results in really weird | ||
263 | // crashes on my system! | ||
264 | curl_easy_setopt(c, | ||
265 | CURLOPT_NOSIGNAL, | ||
266 | 1); | ||
267 | |||
268 | |||
269 | multi = curl_multi_init(); | ||
270 | if (multi == NULL) { | ||
271 | curl_easy_cleanup(c); | ||
272 | MHD_stop_daemon(d); | ||
273 | return 512; | ||
274 | } | ||
275 | mret = curl_multi_add_handle(multi, c); | ||
276 | if (mret != CURLM_OK) { | ||
277 | curl_multi_cleanup(multi); | ||
278 | curl_easy_cleanup(c); | ||
279 | MHD_stop_daemon(d); | ||
280 | return 1024; | ||
281 | } | ||
282 | start = time(NULL); | ||
283 | while ( (time(NULL) - start < 5) && | ||
284 | (multi != NULL) ) { | ||
285 | max = 0; | ||
286 | FD_ZERO(&rs); | ||
287 | FD_ZERO(&ws); | ||
288 | FD_ZERO(&es); | ||
289 | curl_multi_perform(multi, &running); | ||
290 | mret = curl_multi_fdset(multi, | ||
291 | &rs, | ||
292 | &ws, | ||
293 | &es, | ||
294 | &max); | ||
295 | if (mret != CURLM_OK) { | ||
296 | curl_multi_remove_handle(multi, c); | ||
297 | curl_multi_cleanup(multi); | ||
298 | curl_easy_cleanup(c); | ||
299 | MHD_stop_daemon(d); | ||
300 | return 2048; | ||
301 | } | ||
302 | if (MHD_YES != MHD_get_fdset(d, | ||
303 | &rs, | ||
304 | &ws, | ||
305 | &es, | ||
306 | &max)) { | ||
307 | curl_multi_remove_handle(multi, c); | ||
308 | curl_multi_cleanup(multi); | ||
309 | curl_easy_cleanup(c); | ||
310 | MHD_stop_daemon(d); | ||
311 | return 4096; | ||
312 | } | ||
313 | tv.tv_sec = 0; | ||
314 | tv.tv_usec = 1000; | ||
315 | select(max + 1, | ||
316 | &rs, | ||
317 | &ws, | ||
318 | &es, | ||
319 | &tv); | ||
320 | curl_multi_perform(multi, &running); | ||
321 | if (running == 0) { | ||
322 | msg = curl_multi_info_read(multi, | ||
323 | &running); | ||
324 | if (msg == NULL) | ||
325 | break; | ||
326 | if (msg->msg == CURLMSG_DONE) { | ||
327 | if (msg->data.result != CURLE_OK) | ||
328 | printf("%s failed at %s:%d: `%s'\n", | ||
329 | "curl_multi_perform", | ||
330 | __FILE__, | ||
331 | __LINE__, | ||
332 | curl_easy_strerror(msg->data.result)); | ||
333 | curl_multi_remove_handle(multi, c); | ||
334 | curl_multi_cleanup(multi); | ||
335 | curl_easy_cleanup(c); | ||
336 | c = NULL; | ||
337 | multi = NULL; | ||
338 | } | ||
339 | } | ||
340 | MHD_run(d); | ||
341 | } | ||
342 | if (multi != NULL) { | ||
343 | curl_multi_remove_handle(multi, c); | ||
344 | curl_easy_cleanup(c); | ||
345 | curl_multi_cleanup(multi); | ||
346 | } | ||
347 | MHD_stop_daemon(d); | ||
348 | if (cbc.pos != strlen("/hello_world")) | ||
349 | return 8192; | ||
350 | if (0 != strncmp("/hello_world", | ||
351 | cbc.buf, | ||
352 | strlen("/hello_world"))) | ||
353 | return 16384; | ||
354 | return 0; | ||
355 | } | ||
356 | |||
357 | |||
358 | |||
212 | int main(int argc, | 359 | int main(int argc, |
213 | char * const * argv) { | 360 | char * const * argv) { |
214 | unsigned int errorCount = 0; | 361 | unsigned int errorCount = 0; |
@@ -217,6 +364,7 @@ int main(int argc, | |||
217 | return 2; | 364 | return 2; |
218 | errorCount += testInternalGet(); | 365 | errorCount += testInternalGet(); |
219 | errorCount += testMultithreadedGet(); | 366 | errorCount += testMultithreadedGet(); |
367 | errorCount += testExternalGet(); | ||
220 | if (errorCount != 0) | 368 | if (errorCount != 0) |
221 | fprintf(stderr, | 369 | fprintf(stderr, |
222 | "Error (code: %u)\n", | 370 | "Error (code: %u)\n", |