diff options
author | Martin Schanzenbach <mschanzenbach@posteo.de> | 2012-06-03 10:46:07 +0000 |
---|---|---|
committer | Martin Schanzenbach <mschanzenbach@posteo.de> | 2012-06-03 10:46:07 +0000 |
commit | f525315e636a438e398e5363d6e43994faf90ead (patch) | |
tree | b95c47f2a34885fe8a855576897834fdd1178ac1 /src | |
parent | 2f0d0091c69569d8eb2d4b294679f97084950325 (diff) | |
download | gnunet-f525315e636a438e398e5363d6e43994faf90ead.tar.gz gnunet-f525315e636a438e398e5363d6e43994faf90ead.zip |
-fixes
Diffstat (limited to 'src')
-rw-r--r-- | src/gns/gnunet-gns-proxy.c | 169 |
1 files changed, 121 insertions, 48 deletions
diff --git a/src/gns/gnunet-gns-proxy.c b/src/gns/gnunet-gns-proxy.c index 45492935d..9a8172bb9 100644 --- a/src/gns/gnunet-gns-proxy.c +++ b/src/gns/gnunet-gns-proxy.c | |||
@@ -85,6 +85,7 @@ struct ProxyCurlTask | |||
85 | struct ProxyCurlTask *next; | 85 | struct ProxyCurlTask *next; |
86 | 86 | ||
87 | CURL *curl; | 87 | CURL *curl; |
88 | char url[2048]; | ||
88 | char buffer[CURL_MAX_WRITE_SIZE]; | 89 | char buffer[CURL_MAX_WRITE_SIZE]; |
89 | int buf_status; | 90 | int buf_status; |
90 | unsigned int bytes_downloaded; | 91 | unsigned int bytes_downloaded; |
@@ -123,6 +124,14 @@ con_val_iter (void *cls, | |||
123 | return MHD_YES; | 124 | return MHD_YES; |
124 | } | 125 | } |
125 | 126 | ||
127 | |||
128 | /** | ||
129 | * schedule mhd | ||
130 | */ | ||
131 | static void | ||
132 | run_httpd (void); | ||
133 | |||
134 | |||
126 | /** | 135 | /** |
127 | * Process cURL download bits | 136 | * Process cURL download bits |
128 | * | 137 | * |
@@ -173,6 +182,25 @@ callback_download (void *ptr, size_t size, size_t nmemb, void *ctx) | |||
173 | return total; | 182 | return total; |
174 | } | 183 | } |
175 | 184 | ||
185 | |||
186 | |||
187 | /** | ||
188 | * Callback to free content | ||
189 | * | ||
190 | * @param cls content to free | ||
191 | */ | ||
192 | static void | ||
193 | mhd_content_free (void *cls) | ||
194 | { | ||
195 | struct ProxyCurlTask *ctask = cls; | ||
196 | |||
197 | if (ctask->curl != NULL) | ||
198 | curl_easy_cleanup (ctask->curl); | ||
199 | |||
200 | GNUNET_free (ctask); | ||
201 | |||
202 | } | ||
203 | |||
176 | /** | 204 | /** |
177 | * Callback for MHD response | 205 | * Callback for MHD response |
178 | * | 206 | * |
@@ -195,6 +223,8 @@ mhd_content_cb (void *cls, | |||
195 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 223 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
196 | "MHD: sending response\n"); | 224 | "MHD: sending response\n"); |
197 | ctask->download_in_progress = GNUNET_NO; | 225 | ctask->download_in_progress = GNUNET_NO; |
226 | curl_multi_remove_handle (curl_multi, ctask->curl); | ||
227 | curl_easy_cleanup (ctask->curl); | ||
198 | return MHD_CONTENT_READER_END_OF_STREAM; | 228 | return MHD_CONTENT_READER_END_OF_STREAM; |
199 | } | 229 | } |
200 | 230 | ||
@@ -202,8 +232,10 @@ mhd_content_cb (void *cls, | |||
202 | (ctask->buf_status == BUF_WAIT_FOR_CURL)) | 232 | (ctask->buf_status == BUF_WAIT_FOR_CURL)) |
203 | { | 233 | { |
204 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 234 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
205 | "MHD: error sending response\n"); | 235 | "MHD: sending error response\n"); |
206 | ctask->download_in_progress = GNUNET_NO; | 236 | ctask->download_in_progress = GNUNET_NO; |
237 | curl_multi_remove_handle (curl_multi, ctask->curl); | ||
238 | curl_easy_cleanup (ctask->curl); | ||
207 | return MHD_CONTENT_READER_END_WITH_ERROR; | 239 | return MHD_CONTENT_READER_END_WITH_ERROR; |
208 | } | 240 | } |
209 | 241 | ||
@@ -234,11 +266,6 @@ mhd_content_cb (void *cls, | |||
234 | } | 266 | } |
235 | 267 | ||
236 | 268 | ||
237 | /** | ||
238 | * schedule mhd | ||
239 | */ | ||
240 | static void | ||
241 | run_httpd (void); | ||
242 | 269 | ||
243 | /** | 270 | /** |
244 | * Task that is run when we are ready to receive more data | 271 | * Task that is run when we are ready to receive more data |
@@ -324,6 +351,7 @@ curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
324 | struct CURLMsg *msg; | 351 | struct CURLMsg *msg; |
325 | CURLMcode mret; | 352 | CURLMcode mret; |
326 | struct ProxyCurlTask *ctask; | 353 | struct ProxyCurlTask *ctask; |
354 | int num_ctasks; | ||
327 | 355 | ||
328 | curl_download_task = GNUNET_SCHEDULER_NO_TASK; | 356 | curl_download_task = GNUNET_SCHEDULER_NO_TASK; |
329 | 357 | ||
@@ -340,11 +368,27 @@ curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
340 | do | 368 | do |
341 | { | 369 | { |
342 | running = 0; | 370 | running = 0; |
371 | num_ctasks = 0; | ||
343 | 372 | ||
344 | mret = curl_multi_perform (curl_multi, &running); | 373 | mret = curl_multi_perform (curl_multi, &running); |
345 | 374 | ||
346 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 375 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
347 | "Running curl tasks: %d\n", running); | 376 | "Running curl tasks: %d\n", running); |
377 | |||
378 | ctask = ctasks_head; | ||
379 | for (; ctask != NULL; ctask = ctask->next) | ||
380 | { | ||
381 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
382 | "CTask: %s\n", ctask->url); | ||
383 | num_ctasks++; | ||
384 | } | ||
385 | |||
386 | if (num_ctasks != running) | ||
387 | { | ||
388 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
389 | "%d ctasks, %d curl running\n", num_ctasks, running); | ||
390 | } | ||
391 | |||
348 | do | 392 | do |
349 | { | 393 | { |
350 | ctask = ctasks_head; | 394 | ctask = ctasks_head; |
@@ -360,25 +404,27 @@ curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
360 | if ((msg->data.result != CURLE_OK) && | 404 | if ((msg->data.result != CURLE_OK) && |
361 | (msg->data.result != CURLE_GOT_NOTHING)) | 405 | (msg->data.result != CURLE_GOT_NOTHING)) |
362 | { | 406 | { |
363 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | 407 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
364 | "Download curl failed %s\n", | 408 | "Download curl failed"); |
365 | curl_easy_strerror (msg->data.result)); | ||
366 | 409 | ||
367 | for (; ctask != NULL; ctask = ctask->next) | 410 | for (; ctask != NULL; ctask = ctask->next) |
368 | { | 411 | { |
369 | if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) == 0) | 412 | if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0) |
370 | { | 413 | continue; |
371 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 414 | |
372 | "cURL task found.\n"); | 415 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
373 | ctask->download_successful = GNUNET_NO; | 416 | "Download curl failed for task %s: %s.\n", |
374 | ctask->download_error = GNUNET_YES; | 417 | ctask->url, |
375 | curl_multi_remove_handle (curl_multi, ctask->curl); | 418 | curl_easy_strerror (msg->data.result)); |
376 | curl_easy_cleanup (ctask->curl); | 419 | ctask->download_successful = GNUNET_NO; |
377 | GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail, | 420 | ctask->download_error = GNUNET_YES; |
378 | ctask); | 421 | //curl_multi_remove_handle (curl_multi, ctask->curl); |
379 | break; | 422 | //curl_easy_cleanup (ctask->curl); |
380 | } | 423 | GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail, |
424 | ctask); | ||
425 | break; | ||
381 | } | 426 | } |
427 | GNUNET_assert (ctask != NULL); | ||
382 | } | 428 | } |
383 | else | 429 | else |
384 | { | 430 | { |
@@ -387,37 +433,54 @@ curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
387 | 433 | ||
388 | for (; ctask != NULL; ctask = ctask->next) | 434 | for (; ctask != NULL; ctask = ctask->next) |
389 | { | 435 | { |
390 | if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) == 0) | 436 | if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0) |
391 | { | 437 | continue; |
392 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 438 | |
393 | "cURL task found.\n"); | 439 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
394 | ctask->download_successful = GNUNET_YES; | 440 | "cURL task %s found.\n", ctask->url); |
395 | curl_multi_remove_handle (curl_multi, ctask->curl); | 441 | ctask->download_successful = GNUNET_YES; |
396 | curl_easy_cleanup (ctask->curl); | 442 | //curl_multi_remove_handle (curl_multi, ctask->curl); |
397 | GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail, | 443 | //curl_easy_cleanup (ctask->curl); |
398 | ctask); | 444 | GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail, |
399 | break; | 445 | ctask); |
400 | } | 446 | break; |
401 | else | ||
402 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
403 | "cURL task skipped.\n"); | ||
404 | } | 447 | } |
405 | run_httpd (); | 448 | GNUNET_assert (ctask != NULL); |
406 | //TODO iterate list, find ctask | ||
407 | } | 449 | } |
450 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
451 | "curl end %s\n", curl_easy_strerror(msg->data.result)); | ||
452 | run_httpd (); | ||
408 | break; | 453 | break; |
409 | default: | 454 | default: |
455 | GNUNET_assert (0); | ||
410 | break; | 456 | break; |
411 | } | 457 | } |
412 | } while (msgnum > 0); | 458 | } while (msgnum > 0); |
413 | } while (mret == CURLM_CALL_MULTI_PERFORM); | 459 | |
460 | num_ctasks=0; | ||
461 | for (ctask=ctasks_head; ctask != NULL; ctask = ctask->next) | ||
462 | { | ||
463 | num_ctasks++; | ||
464 | } | ||
465 | |||
466 | if (num_ctasks != running) | ||
467 | { | ||
468 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
469 | "%d ctasks, %d curl running\n", num_ctasks, running); | ||
470 | } | ||
471 | |||
472 | GNUNET_assert ( num_ctasks == running ); | ||
414 | 473 | ||
474 | run_httpd (); | ||
475 | |||
476 | } while (mret == CURLM_CALL_MULTI_PERFORM); | ||
477 | |||
478 | |||
415 | if (mret != CURLM_OK) | 479 | if (mret != CURLM_OK) |
416 | { | 480 | { |
417 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s failed at %s:%d: `%s'\n", | 481 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s failed at %s:%d: `%s'\n", |
418 | "curl_multi_perform", __FILE__, __LINE__, | 482 | "curl_multi_perform", __FILE__, __LINE__, |
419 | curl_multi_strerror (mret)); | 483 | curl_multi_strerror (mret)); |
420 | //TODO cleanup | ||
421 | } | 484 | } |
422 | curl_download_prepare(); | 485 | curl_download_prepare(); |
423 | } | 486 | } |
@@ -520,13 +583,14 @@ create_response (void *cls, | |||
520 | curl_easy_setopt (ctask->curl, CURLOPT_MAXREDIRS, 4); | 583 | curl_easy_setopt (ctask->curl, CURLOPT_MAXREDIRS, 4); |
521 | /* no need to abort if the above failed */ | 584 | /* no need to abort if the above failed */ |
522 | sprintf (curlurl, "http://%s%s", host, url); | 585 | sprintf (curlurl, "http://%s%s", host, url); |
586 | strcpy (ctask->url, curlurl); | ||
523 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 587 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
524 | "Adding new curl task for %s\n", curlurl); | 588 | "Adding new curl task for %s\n", curlurl); |
525 | 589 | ||
526 | curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl); | 590 | curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl); |
527 | curl_easy_setopt (ctask->curl, CURLOPT_FAILONERROR, 1); | 591 | curl_easy_setopt (ctask->curl, CURLOPT_FAILONERROR, 1); |
528 | curl_easy_setopt (ctask->curl, CURLOPT_CONNECTTIMEOUT, 60L); | 592 | curl_easy_setopt (ctask->curl, CURLOPT_CONNECTTIMEOUT, 600L); |
529 | curl_easy_setopt (ctask->curl, CURLOPT_TIMEOUT, 60L); | 593 | curl_easy_setopt (ctask->curl, CURLOPT_TIMEOUT, 600L); |
530 | 594 | ||
531 | mret = curl_multi_add_handle (curl_multi, ctask->curl); | 595 | mret = curl_multi_add_handle (curl_multi, ctask->curl); |
532 | 596 | ||
@@ -536,9 +600,16 @@ create_response (void *cls, | |||
536 | "%s failed at %s:%d: `%s'\n", | 600 | "%s failed at %s:%d: `%s'\n", |
537 | "curl_multi_add_handle", __FILE__, __LINE__, | 601 | "curl_multi_add_handle", __FILE__, __LINE__, |
538 | curl_multi_strerror (mret)); | 602 | curl_multi_strerror (mret)); |
603 | response = MHD_create_response_from_buffer (strlen (page), | ||
604 | (void*)page, | ||
605 | MHD_RESPMEM_PERSISTENT); | ||
606 | ret = MHD_queue_response (con, | ||
607 | MHD_HTTP_OK, | ||
608 | response); | ||
609 | MHD_destroy_response (response); | ||
610 | |||
539 | curl_easy_cleanup (ctask->curl); | 611 | curl_easy_cleanup (ctask->curl); |
540 | GNUNET_free (ctask); | 612 | GNUNET_free (ctask); |
541 | //TODO maybe error display here | ||
542 | return ret; | 613 | return ret; |
543 | } | 614 | } |
544 | 615 | ||
@@ -549,9 +620,11 @@ create_response (void *cls, | |||
549 | response = MHD_create_response_from_callback (-1, -1, | 620 | response = MHD_create_response_from_callback (-1, -1, |
550 | &mhd_content_cb, | 621 | &mhd_content_cb, |
551 | ctask, | 622 | ctask, |
552 | NULL); //TODO Destroy resp here | 623 | &mhd_content_free); |
553 | 624 | ||
554 | ret = MHD_queue_response (con, MHD_HTTP_OK, response); | 625 | ret = MHD_queue_response (con, MHD_HTTP_OK, response); |
626 | |||
627 | //MHD_destroy_response (response); | ||
555 | 628 | ||
556 | return ret; | 629 | return ret; |
557 | } | 630 | } |
@@ -669,8 +742,8 @@ do_write_remote (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
669 | 742 | ||
670 | if ((NULL != tc->read_ready) && | 743 | if ((NULL != tc->read_ready) && |
671 | (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->remote_sock)) && | 744 | (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->remote_sock)) && |
672 | (len = GNUNET_NETWORK_socket_send (s5r->remote_sock, s5r->rbuf, | 745 | ((len = GNUNET_NETWORK_socket_send (s5r->remote_sock, s5r->rbuf, |
673 | s5r->rbuf_len))) | 746 | s5r->rbuf_len)>0))) |
674 | { | 747 | { |
675 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 748 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
676 | "Successfully sent %d bytes to remote socket\n", | 749 | "Successfully sent %d bytes to remote socket\n", |
@@ -715,8 +788,8 @@ do_write (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
715 | 788 | ||
716 | if ((NULL != tc->read_ready) && | 789 | if ((NULL != tc->read_ready) && |
717 | (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->sock)) && | 790 | (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->sock)) && |
718 | (len = GNUNET_NETWORK_socket_send (s5r->sock, s5r->wbuf, | 791 | ((len = GNUNET_NETWORK_socket_send (s5r->sock, s5r->wbuf, |
719 | s5r->wbuf_len))) | 792 | s5r->wbuf_len)>0))) |
720 | { | 793 | { |
721 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 794 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
722 | "Successfully sent %d bytes to socket\n", | 795 | "Successfully sent %d bytes to socket\n", |