diff options
author | Christian Grothoff <christian@grothoff.org> | 2015-03-13 21:50:54 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2015-03-13 21:50:54 +0000 |
commit | 022d19fda1ae9d0f0aa7e83c4ed4a8d6455480c4 (patch) | |
tree | 728f5424330063c910e80b4b346478d61bcc3e6e /src/rest/gnunet-rest-server.c | |
parent | 14f993de57d834b4743d22cd952cfd7ca3aef097 (diff) | |
download | gnunet-022d19fda1ae9d0f0aa7e83c4ed4a8d6455480c4.tar.gz gnunet-022d19fda1ae9d0f0aa7e83c4ed4a8d6455480c4.zip |
-minor fixes
Diffstat (limited to 'src/rest/gnunet-rest-server.c')
-rw-r--r-- | src/rest/gnunet-rest-server.c | 73 |
1 files changed, 52 insertions, 21 deletions
diff --git a/src/rest/gnunet-rest-server.c b/src/rest/gnunet-rest-server.c index 10577b451..4dc1c591d 100644 --- a/src/rest/gnunet-rest-server.c +++ b/src/rest/gnunet-rest-server.c | |||
@@ -61,12 +61,12 @@ | |||
61 | /** | 61 | /** |
62 | * The task ID | 62 | * The task ID |
63 | */ | 63 | */ |
64 | struct GNUNET_SCHEDULER_Task * httpd_task; | 64 | static struct GNUNET_SCHEDULER_Task * httpd_task; |
65 | 65 | ||
66 | /** | 66 | /** |
67 | * is this an ssl daemon? //TODO | 67 | * is this an ssl daemon? //TODO |
68 | */ | 68 | */ |
69 | int is_ssl; | 69 | static int is_ssl; |
70 | 70 | ||
71 | /** | 71 | /** |
72 | * The port the service is running on (default 7776) | 72 | * The port the service is running on (default 7776) |
@@ -111,7 +111,7 @@ static const struct GNUNET_CONFIGURATION_Handle *cfg; | |||
111 | /** | 111 | /** |
112 | * Map of loaded plugins. | 112 | * Map of loaded plugins. |
113 | */ | 113 | */ |
114 | struct GNUNET_CONTAINER_MultiHashMap *plugin_map; | 114 | static struct GNUNET_CONTAINER_MultiHashMap *plugin_map; |
115 | 115 | ||
116 | /** | 116 | /** |
117 | * MHD Connection handle | 117 | * MHD Connection handle |
@@ -164,20 +164,27 @@ run_mhd_now () | |||
164 | * @param cls closure (MHD connection handle) | 164 | * @param cls closure (MHD connection handle) |
165 | * @param data the data to return to the caller | 165 | * @param data the data to return to the caller |
166 | * @param len length of the data | 166 | * @param len length of the data |
167 | * @param status GNUNET_OK if successful | 167 | * @param status #GNUNET_OK if successful |
168 | */ | 168 | */ |
169 | void | 169 | static void |
170 | plugin_callback (void *cls, | 170 | plugin_callback (void *cls, |
171 | struct MHD_Response *resp, | 171 | struct MHD_Response *resp, |
172 | int status) | 172 | int status) |
173 | { | 173 | { |
174 | struct MhdConnectionHandle *handle = cls; | 174 | struct MhdConnectionHandle *handle = cls; |
175 | struct MHD_Response *resp = MHD_create_response_from_buffer (len, | ||
176 | (void*)data, | ||
177 | MHD_RESPMEM_MUST_COPY); | ||
178 | (void) MHD_add_response_header (resp, | ||
179 | MHD_HTTP_HEADER_CONTENT_TYPE, | ||
180 | "application/json"); | ||
175 | handle->status = status; | 181 | handle->status = status; |
176 | handle->response = resp; | 182 | handle->response = resp; |
177 | run_mhd_now(); | 183 | run_mhd_now(); |
178 | } | 184 | } |
179 | 185 | ||
180 | int | 186 | |
187 | static int | ||
181 | cleanup_url_map (void *cls, | 188 | cleanup_url_map (void *cls, |
182 | const struct GNUNET_HashCode *key, | 189 | const struct GNUNET_HashCode *key, |
183 | void *value) | 190 | void *value) |
@@ -186,7 +193,8 @@ cleanup_url_map (void *cls, | |||
186 | return GNUNET_YES; | 193 | return GNUNET_YES; |
187 | } | 194 | } |
188 | 195 | ||
189 | void | 196 | |
197 | static void | ||
190 | cleanup_handle (struct MhdConnectionHandle *handle) | 198 | cleanup_handle (struct MhdConnectionHandle *handle) |
191 | { | 199 | { |
192 | if (NULL != handle->response) | 200 | if (NULL != handle->response) |
@@ -203,10 +211,10 @@ cleanup_handle (struct MhdConnectionHandle *handle) | |||
203 | GNUNET_free (handle->data_handle); | 211 | GNUNET_free (handle->data_handle); |
204 | } | 212 | } |
205 | GNUNET_free (handle); | 213 | GNUNET_free (handle); |
206 | |||
207 | } | 214 | } |
208 | 215 | ||
209 | int | 216 | |
217 | static int | ||
210 | url_iterator (void *cls, | 218 | url_iterator (void *cls, |
211 | enum MHD_ValueKind kind, | 219 | enum MHD_ValueKind kind, |
212 | const char *key, | 220 | const char *key, |
@@ -293,17 +301,14 @@ create_response (void *cls, | |||
293 | con_handle->plugin = GNUNET_CONTAINER_multihashmap_get (plugin_map, | 301 | con_handle->plugin = GNUNET_CONTAINER_multihashmap_get (plugin_map, |
294 | &key); | 302 | &key); |
295 | } | 303 | } |
296 | else | ||
297 | con_handle->plugin = NULL; | ||
298 | |||
299 | if (NULL == con_handle->plugin) | 304 | if (NULL == con_handle->plugin) |
300 | { | 305 | { |
301 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 306 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
302 | "Queueing response with MHD\n"); | 307 | "Queueing response with MHD\n"); |
303 | GNUNET_free (con_handle); | 308 | GNUNET_free (con_handle); |
304 | MHD_queue_response (con, | 309 | return MHD_queue_response (con, |
305 | MHD_HTTP_INTERNAL_SERVER_ERROR, | 310 | MHD_HTTP_NOT_FOUND, |
306 | failure_response); | 311 | failure_response); |
307 | } | 312 | } |
308 | return MHD_YES; | 313 | return MHD_YES; |
309 | } | 314 | } |
@@ -326,14 +331,15 @@ create_response (void *cls, | |||
326 | &plugin_callback, | 331 | &plugin_callback, |
327 | con_handle); | 332 | con_handle); |
328 | *upload_data_size = 0; | 333 | *upload_data_size = 0; |
329 | |||
330 | } | 334 | } |
331 | if (NULL != con_handle->response) | 335 | if (NULL != con_handle->response) |
332 | { | 336 | { |
333 | |||
334 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 337 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
335 | "Queueing response from plugin with MHD\n"); | 338 | "Queueing response from plugin with MHD\n"); |
336 | MHD_add_response_header (con_handle->response,"Access-Control-Allow-Origin","*"); | 339 | /* FIXME: this is a bit dangerous... */ |
340 | MHD_add_response_header (con_handle->response, | ||
341 | "Access-Control-Allow-Origin", | ||
342 | "*"); | ||
337 | int ret = MHD_queue_response (con, | 343 | int ret = MHD_queue_response (con, |
338 | con_handle->status, | 344 | con_handle->status, |
339 | con_handle->response); | 345 | con_handle->response); |
@@ -343,6 +349,7 @@ create_response (void *cls, | |||
343 | return MHD_YES; | 349 | return MHD_YES; |
344 | } | 350 | } |
345 | 351 | ||
352 | |||
346 | /* ******************** MHD HTTP setup and event loop ******************** */ | 353 | /* ******************** MHD HTTP setup and event loop ******************** */ |
347 | 354 | ||
348 | /** | 355 | /** |
@@ -360,14 +367,22 @@ mhd_completed_cb (void *cls, | |||
360 | void **con_cls, | 367 | void **con_cls, |
361 | enum MHD_RequestTerminationCode toe) | 368 | enum MHD_RequestTerminationCode toe) |
362 | { | 369 | { |
370 | struct MhdConnectionHandle *con_handle; | ||
363 | 371 | ||
372 | con_handle = *con_cls; | ||
373 | if (NULL != con_handle) | ||
374 | { | ||
375 | MHD_destroy_response (con_handle->response); | ||
376 | GNUNET_free (con_handle); | ||
377 | *con_cls = NULL; | ||
378 | } | ||
364 | if (MHD_REQUEST_TERMINATED_COMPLETED_OK != toe) | 379 | if (MHD_REQUEST_TERMINATED_COMPLETED_OK != toe) |
365 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 380 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
366 | "MHD encountered error handling request: %d\n", | 381 | "MHD encountered error handling request: %d\n", |
367 | toe); | 382 | toe); |
368 | *con_cls = NULL; | ||
369 | } | 383 | } |
370 | 384 | ||
385 | |||
371 | /** | 386 | /** |
372 | * Kill the MHD daemon. | 387 | * Kill the MHD daemon. |
373 | */ | 388 | */ |
@@ -386,6 +401,7 @@ kill_httpd () | |||
386 | } | 401 | } |
387 | } | 402 | } |
388 | 403 | ||
404 | |||
389 | /** | 405 | /** |
390 | * Task run whenever HTTP server is idle for too long. Kill it. | 406 | * Task run whenever HTTP server is idle for too long. Kill it. |
391 | * | 407 | * |
@@ -399,6 +415,8 @@ kill_httpd_task (void *cls, | |||
399 | httpd_task = NULL; | 415 | httpd_task = NULL; |
400 | kill_httpd (); | 416 | kill_httpd (); |
401 | } | 417 | } |
418 | |||
419 | |||
402 | /** | 420 | /** |
403 | * Schedule MHD. This function should be called initially when an | 421 | * Schedule MHD. This function should be called initially when an |
404 | * MHD is first getting its client socket, and will then automatically | 422 | * MHD is first getting its client socket, and will then automatically |
@@ -483,6 +501,7 @@ do_httpd (void *cls, | |||
483 | schedule_httpd (); | 501 | schedule_httpd (); |
484 | } | 502 | } |
485 | 503 | ||
504 | |||
486 | /** | 505 | /** |
487 | * Accept new incoming connections | 506 | * Accept new incoming connections |
488 | * | 507 | * |
@@ -534,6 +553,7 @@ do_accept (void *cls, | |||
534 | schedule_httpd (); | 553 | schedule_httpd (); |
535 | } | 554 | } |
536 | 555 | ||
556 | |||
537 | /** | 557 | /** |
538 | * Task run on shutdown | 558 | * Task run on shutdown |
539 | * | 559 | * |
@@ -549,6 +569,7 @@ do_shutdown (void *cls, | |||
549 | kill_httpd (); | 569 | kill_httpd (); |
550 | } | 570 | } |
551 | 571 | ||
572 | |||
552 | /** | 573 | /** |
553 | * Create an IPv4 listen socket bound to our port. | 574 | * Create an IPv4 listen socket bound to our port. |
554 | * | 575 | * |
@@ -584,6 +605,7 @@ bind_v4 () | |||
584 | return ls; | 605 | return ls; |
585 | } | 606 | } |
586 | 607 | ||
608 | |||
587 | /** | 609 | /** |
588 | * Create an IPv6 listen socket bound to our port. | 610 | * Create an IPv6 listen socket bound to our port. |
589 | * | 611 | * |
@@ -619,6 +641,7 @@ bind_v6 () | |||
619 | return ls; | 641 | return ls; |
620 | } | 642 | } |
621 | 643 | ||
644 | |||
622 | /** | 645 | /** |
623 | * Callback for plugin load | 646 | * Callback for plugin load |
624 | * | 647 | * |
@@ -626,7 +649,7 @@ bind_v6 () | |||
626 | * @param libname the name of the library loaded | 649 | * @param libname the name of the library loaded |
627 | * @param lib_ret the object returned by the plugin initializer | 650 | * @param lib_ret the object returned by the plugin initializer |
628 | */ | 651 | */ |
629 | void | 652 | static void |
630 | load_plugin (void *cls, | 653 | load_plugin (void *cls, |
631 | const char *libname, | 654 | const char *libname, |
632 | void *lib_ret) | 655 | void *lib_ret) |
@@ -658,6 +681,7 @@ load_plugin (void *cls, | |||
658 | libname); | 681 | libname); |
659 | } | 682 | } |
660 | 683 | ||
684 | |||
661 | /** | 685 | /** |
662 | * Main function that will be run | 686 | * Main function that will be run |
663 | * | 687 | * |
@@ -667,7 +691,9 @@ load_plugin (void *cls, | |||
667 | * @param c configuration | 691 | * @param c configuration |
668 | */ | 692 | */ |
669 | static void | 693 | static void |
670 | run (void *cls, char *const *args, const char *cfgfile, | 694 | run (void *cls, |
695 | char *const *args, | ||
696 | const char *cfgfile, | ||
671 | const struct GNUNET_CONFIGURATION_Handle *c) | 697 | const struct GNUNET_CONFIGURATION_Handle *c) |
672 | { | 698 | { |
673 | cfg = c; | 699 | cfg = c; |
@@ -676,7 +702,9 @@ run (void *cls, char *const *args, const char *cfgfile, | |||
676 | /* Open listen socket proxy */ | 702 | /* Open listen socket proxy */ |
677 | lsock6 = bind_v6 (); | 703 | lsock6 = bind_v6 (); |
678 | if (NULL == lsock6) | 704 | if (NULL == lsock6) |
705 | { | ||
679 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind"); | 706 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind"); |
707 | } | ||
680 | else | 708 | else |
681 | { | 709 | { |
682 | if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock6, 5)) | 710 | if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock6, 5)) |
@@ -693,7 +721,9 @@ run (void *cls, char *const *args, const char *cfgfile, | |||
693 | } | 721 | } |
694 | lsock4 = bind_v4 (); | 722 | lsock4 = bind_v4 (); |
695 | if (NULL == lsock4) | 723 | if (NULL == lsock4) |
724 | { | ||
696 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind"); | 725 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind"); |
726 | } | ||
697 | else | 727 | else |
698 | { | 728 | { |
699 | if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock4, 5)) | 729 | if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock4, 5)) |
@@ -738,6 +768,7 @@ run (void *cls, char *const *args, const char *cfgfile, | |||
738 | &do_shutdown, NULL); | 768 | &do_shutdown, NULL); |
739 | } | 769 | } |
740 | 770 | ||
771 | |||
741 | /** | 772 | /** |
742 | * | 773 | * |
743 | * The main function for gnunet-rest-service | 774 | * The main function for gnunet-rest-service |