diff options
author | Martin Schanzenbach <mschanzenbach@posteo.de> | 2012-06-05 13:06:59 +0000 |
---|---|---|
committer | Martin Schanzenbach <mschanzenbach@posteo.de> | 2012-06-05 13:06:59 +0000 |
commit | c602a3cbe1dc18ea29e9567b8be4e574c97bf4ae (patch) | |
tree | a6ff5b765c7360fdfc06bf05cf57f26f93e22dc5 /src | |
parent | f485f3e374f7ef1faa6762633797cf4401ffa862 (diff) | |
download | gnunet-c602a3cbe1dc18ea29e9567b8be4e574c97bf4ae.tar.gz gnunet-c602a3cbe1dc18ea29e9567b8be4e574c97bf4ae.zip |
-add GNS, fix mhd response creation bugs
Diffstat (limited to 'src')
-rw-r--r-- | src/gns/gnunet-gns-proxy.c | 118 |
1 files changed, 104 insertions, 14 deletions
diff --git a/src/gns/gnunet-gns-proxy.c b/src/gns/gnunet-gns-proxy.c index 2b561d811..050f38c13 100644 --- a/src/gns/gnunet-gns-proxy.c +++ b/src/gns/gnunet-gns-proxy.c | |||
@@ -20,6 +20,7 @@ | |||
20 | 20 | ||
21 | #include "platform.h" | 21 | #include "platform.h" |
22 | #include <gnunet_util_lib.h> | 22 | #include <gnunet_util_lib.h> |
23 | #include <gnunet_gns_service.h> | ||
23 | #include <microhttpd.h> | 24 | #include <microhttpd.h> |
24 | #include <curl/curl.h> | 25 | #include <curl/curl.h> |
25 | #include <regex.h> | 26 | #include <regex.h> |
@@ -104,10 +105,13 @@ struct ProxyCurlTask | |||
104 | struct MHD_Connection *connection; | 105 | struct MHD_Connection *connection; |
105 | int parse_content; | 106 | int parse_content; |
106 | int is_postprocessing; | 107 | int is_postprocessing; |
108 | int pp_finished; | ||
107 | 109 | ||
108 | GNUNET_SCHEDULER_TaskIdentifier pp_task; | 110 | GNUNET_SCHEDULER_TaskIdentifier pp_task; |
109 | 111 | ||
110 | char pp_buf[256]; | 112 | char pp_buf[256]; |
113 | char authority[256]; | ||
114 | char host[256]; | ||
111 | 115 | ||
112 | }; | 116 | }; |
113 | 117 | ||
@@ -117,10 +121,12 @@ GNUNET_SCHEDULER_TaskIdentifier ltask; | |||
117 | GNUNET_SCHEDULER_TaskIdentifier curl_download_task; | 121 | GNUNET_SCHEDULER_TaskIdentifier curl_download_task; |
118 | static struct MHD_Daemon *httpd; | 122 | static struct MHD_Daemon *httpd; |
119 | static GNUNET_SCHEDULER_TaskIdentifier httpd_task; | 123 | static GNUNET_SCHEDULER_TaskIdentifier httpd_task; |
120 | CURLM *curl_multi; | 124 | static CURLM *curl_multi; |
121 | 125 | ||
122 | struct ProxyCurlTask *ctasks_head; | 126 | static struct GNUNET_GNS_Handle *gns_handle; |
123 | struct ProxyCurlTask *ctasks_tail; | 127 | |
128 | static struct ProxyCurlTask *ctasks_head; | ||
129 | static struct ProxyCurlTask *ctasks_tail; | ||
124 | 130 | ||
125 | static regex_t re_dotplus; | 131 | static regex_t re_dotplus; |
126 | 132 | ||
@@ -262,20 +268,54 @@ mhd_content_free (void *cls) | |||
262 | } | 268 | } |
263 | 269 | ||
264 | static void | 270 | static void |
271 | run_mhd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
272 | { | ||
273 | MHD_run (httpd); | ||
274 | } | ||
275 | |||
276 | static void | ||
277 | process_shorten (void* cls, const char* short_name) | ||
278 | { | ||
279 | struct ProxyCurlTask *ctask = cls; | ||
280 | |||
281 | char tmp[strlen(ctask->pp_buf)]; //TODO length | ||
282 | |||
283 | if (NULL == short_name) | ||
284 | { | ||
285 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
286 | "MHD PP: Unable to shorten %s\n", | ||
287 | ctask->pp_buf); | ||
288 | return; | ||
289 | } | ||
290 | |||
291 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
292 | "MHD PP: Shorten %s -> %s\n", | ||
293 | ctask->pp_buf, | ||
294 | short_name); | ||
295 | |||
296 | sprintf (tmp, "<a href=http://%s", short_name); | ||
297 | strcpy (ctask->pp_buf, tmp); | ||
298 | |||
299 | ctask->pp_finished = GNUNET_YES; | ||
300 | |||
301 | GNUNET_SCHEDULER_add_now (&run_mhd, NULL); | ||
302 | } | ||
303 | |||
304 | static void | ||
265 | postprocess_name (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | 305 | postprocess_name (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) |
266 | { | 306 | { |
267 | struct ProxyCurlTask *ctask = cls; | 307 | struct ProxyCurlTask *ctask = cls; |
268 | char tmp[strlen(ctask->pp_buf)]; | 308 | char tmp[strlen(ctask->pp_buf)]; |
269 | 309 | ||
270 | sprintf ( tmp, "<a href=http://%sxxx.gnunet", ctask->pp_buf); | ||
271 | 310 | ||
272 | strcpy (ctask->pp_buf, tmp); | ||
273 | 311 | ||
274 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 312 | sprintf ( tmp, "%s%s", ctask->pp_buf, ctask->authority); |
275 | "Created %s\n", tmp); | 313 | |
314 | GNUNET_GNS_shorten (gns_handle, | ||
315 | tmp, | ||
316 | &process_shorten, | ||
317 | ctask); | ||
276 | 318 | ||
277 | run_httpd(); | ||
278 | //MHD_run (httpd); | ||
279 | } | 319 | } |
280 | 320 | ||
281 | /** | 321 | /** |
@@ -310,6 +350,7 @@ mhd_content_cb (void *cls, | |||
310 | ctask->download_in_progress = GNUNET_NO; | 350 | ctask->download_in_progress = GNUNET_NO; |
311 | curl_multi_remove_handle (curl_multi, ctask->curl); | 351 | curl_multi_remove_handle (curl_multi, ctask->curl); |
312 | curl_easy_cleanup (ctask->curl); | 352 | curl_easy_cleanup (ctask->curl); |
353 | GNUNET_SCHEDULER_add_now (&run_mhd, NULL); | ||
313 | return MHD_CONTENT_READER_END_OF_STREAM; | 354 | return MHD_CONTENT_READER_END_OF_STREAM; |
314 | } | 355 | } |
315 | 356 | ||
@@ -321,6 +362,7 @@ mhd_content_cb (void *cls, | |||
321 | ctask->download_in_progress = GNUNET_NO; | 362 | ctask->download_in_progress = GNUNET_NO; |
322 | curl_multi_remove_handle (curl_multi, ctask->curl); | 363 | curl_multi_remove_handle (curl_multi, ctask->curl); |
323 | curl_easy_cleanup (ctask->curl); | 364 | curl_easy_cleanup (ctask->curl); |
365 | GNUNET_SCHEDULER_add_now (&run_mhd, NULL); | ||
324 | return MHD_CONTENT_READER_END_WITH_ERROR; | 366 | return MHD_CONTENT_READER_END_WITH_ERROR; |
325 | } | 367 | } |
326 | 368 | ||
@@ -360,8 +402,12 @@ mhd_content_cb (void *cls, | |||
360 | { | 402 | { |
361 | 403 | ||
362 | /*Done?*/ | 404 | /*Done?*/ |
363 | if ( 0 == strcmp (ctask->pp_buf, "") ) | 405 | if ( ctask->pp_finished == GNUNET_NO ) |
406 | { | ||
407 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
408 | "MHD PP: Waiting for PP of %s\n", ctask->pp_buf); | ||
364 | return 0; | 409 | return 0; |
410 | } | ||
365 | 411 | ||
366 | ctask->is_postprocessing = GNUNET_NO; | 412 | ctask->is_postprocessing = GNUNET_NO; |
367 | 413 | ||
@@ -370,6 +416,8 @@ mhd_content_cb (void *cls, | |||
370 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 416 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
371 | "Skipping next %d bytes in buffer\n", m[0].rm_eo); | 417 | "Skipping next %d bytes in buffer\n", m[0].rm_eo); |
372 | 418 | ||
419 | GNUNET_SCHEDULER_add_now (&run_mhd, NULL); | ||
420 | |||
373 | if ( strlen (ctask->pp_buf) <= max ) | 421 | if ( strlen (ctask->pp_buf) <= max ) |
374 | { | 422 | { |
375 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 423 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
@@ -388,9 +436,10 @@ mhd_content_cb (void *cls, | |||
388 | memcpy (ctask->pp_buf, hostptr, (m[1].rm_eo-m[1].rm_so)); | 436 | memcpy (ctask->pp_buf, hostptr, (m[1].rm_eo-m[1].rm_so)); |
389 | 437 | ||
390 | ctask->is_postprocessing = GNUNET_YES; | 438 | ctask->is_postprocessing = GNUNET_YES; |
439 | ctask->pp_finished = GNUNET_NO; | ||
391 | 440 | ||
392 | postprocess_name(ctask, NULL); | 441 | //postprocess_name(ctask, NULL); |
393 | //ctask->pp_task = GNUNET_SCHEDULER_add_now (&postprocess_name, ctask); | 442 | ctask->pp_task = GNUNET_SCHEDULER_add_now (&postprocess_name, ctask); |
394 | 443 | ||
395 | return 0; | 444 | return 0; |
396 | } | 445 | } |
@@ -426,9 +475,12 @@ mhd_content_cb (void *cls, | |||
426 | ctask->buf_status = BUF_WAIT_FOR_CURL; | 475 | ctask->buf_status = BUF_WAIT_FOR_CURL; |
427 | ctask->buffer_ptr = ctask->buffer; | 476 | ctask->buffer_ptr = ctask->buffer; |
428 | curl_easy_pause (ctask->curl, CURLPAUSE_CONT); | 477 | curl_easy_pause (ctask->curl, CURLPAUSE_CONT); |
478 | GNUNET_SCHEDULER_add_now (&run_mhd, NULL); | ||
429 | } | 479 | } |
430 | } | 480 | } |
431 | 481 | ||
482 | GNUNET_SCHEDULER_add_now (&run_mhd, NULL); | ||
483 | |||
432 | return copied; | 484 | return copied; |
433 | } | 485 | } |
434 | 486 | ||
@@ -651,6 +703,29 @@ curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
651 | curl_download_prepare(); | 703 | curl_download_prepare(); |
652 | } | 704 | } |
653 | 705 | ||
706 | /** | ||
707 | * Initialize download and trigger curl | ||
708 | * | ||
709 | */ | ||
710 | static void | ||
711 | process_get_authority (void* cls, | ||
712 | const char* auth_name) | ||
713 | { | ||
714 | struct ProxyCurlTask *ctask = cls; | ||
715 | |||
716 | if (NULL == auth_name) | ||
717 | { | ||
718 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
719 | "Get authority failed!\n"); | ||
720 | strcpy (ctask->authority, ""); | ||
721 | } | ||
722 | |||
723 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
724 | "Get authority yielded %s\n", auth_name); | ||
725 | strcpy (ctask->authority, auth_name); | ||
726 | |||
727 | curl_download_prepare (); | ||
728 | } | ||
654 | 729 | ||
655 | /** | 730 | /** |
656 | * Main MHD callback for handling requests. | 731 | * Main MHD callback for handling requests. |
@@ -752,6 +827,7 @@ create_response (void *cls, | |||
752 | curl_easy_setopt (ctask->curl, CURLOPT_MAXREDIRS, 4); | 827 | curl_easy_setopt (ctask->curl, CURLOPT_MAXREDIRS, 4); |
753 | /* no need to abort if the above failed */ | 828 | /* no need to abort if the above failed */ |
754 | sprintf (curlurl, "http://%s%s", host, url); | 829 | sprintf (curlurl, "http://%s%s", host, url); |
830 | strcpy (ctask->host, host); | ||
755 | strcpy (ctask->url, curlurl); | 831 | strcpy (ctask->url, curlurl); |
756 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 832 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
757 | "Adding new curl task for %s\n", curlurl); | 833 | "Adding new curl task for %s\n", curlurl); |
@@ -783,8 +859,13 @@ create_response (void *cls, | |||
783 | } | 859 | } |
784 | 860 | ||
785 | GNUNET_CONTAINER_DLL_insert (ctasks_head, ctasks_tail, ctask); | 861 | GNUNET_CONTAINER_DLL_insert (ctasks_head, ctasks_tail, ctask); |
786 | 862 | ||
787 | curl_download_prepare (); | 863 | GNUNET_GNS_get_authority (gns_handle, |
864 | ctask->host, | ||
865 | &process_get_authority, | ||
866 | ctask); | ||
867 | //download_prepare (ctask); | ||
868 | //curl_download_prepare (); | ||
788 | 869 | ||
789 | response = MHD_create_response_from_callback (-1, -1, | 870 | response = MHD_create_response_from_callback (-1, -1, |
790 | &mhd_content_cb, | 871 | &mhd_content_cb, |
@@ -1429,6 +1510,15 @@ run (void *cls, char *const *args, const char *cfgfile, | |||
1429 | 1510 | ||
1430 | compile_regex (&re_dotplus, (char*) RE_DOTPLUS); | 1511 | compile_regex (&re_dotplus, (char*) RE_DOTPLUS); |
1431 | 1512 | ||
1513 | gns_handle = GNUNET_GNS_connect (cfg); | ||
1514 | |||
1515 | if (NULL == gns_handle) | ||
1516 | { | ||
1517 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
1518 | "Unable to connect to GNS!\n"); | ||
1519 | return; | ||
1520 | } | ||
1521 | |||
1432 | memset (&sa, 0, sizeof (sa)); | 1522 | memset (&sa, 0, sizeof (sa)); |
1433 | sa.sin_family = AF_INET; | 1523 | sa.sin_family = AF_INET; |
1434 | sa.sin_port = htons (port); | 1524 | sa.sin_port = htons (port); |