aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Schanzenbach <mschanzenbach@posteo.de>2015-06-29 14:33:38 +0000
committerMartin Schanzenbach <mschanzenbach@posteo.de>2015-06-29 14:33:38 +0000
commit13aba2e2ba02476410ccee95974c7c5181f83fcf (patch)
treedd0285f8b00635452759ceebdd104c648b2e2f2a
parentd953fdc6293a9468019eebbcfed92c3b5387ec48 (diff)
downloadgnunet-13aba2e2ba02476410ccee95974c7c5181f83fcf.tar.gz
gnunet-13aba2e2ba02476410ccee95974c7c5181f83fcf.zip
- add CORS logic
-rw-r--r--src/gns/plugin_rest_gns.c2
-rw-r--r--src/identity/plugin_rest_identity.c9
-rw-r--r--src/include/gnunet_rest_plugin.h5
-rw-r--r--src/rest/gnunet-rest-server.c61
-rw-r--r--src/rest/rest.conf2
5 files changed, 73 insertions, 6 deletions
diff --git a/src/gns/plugin_rest_gns.c b/src/gns/plugin_rest_gns.c
index d7fc31d4c..1a5ee9eea 100644
--- a/src/gns/plugin_rest_gns.c
+++ b/src/gns/plugin_rest_gns.c
@@ -648,6 +648,7 @@ libgnunet_plugin_rest_gns_init (void *cls)
648 api->cls = &plugin; 648 api->cls = &plugin;
649 api->name = API_NAMESPACE; 649 api->name = API_NAMESPACE;
650 api->process_request = &rest_gns_process_request; 650 api->process_request = &rest_gns_process_request;
651 GNUNET_asprintf (&api->allow_methods, "%s", MHD_HTTP_METHOD_GET);
651 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 652 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
652 _("GNS REST API initialized\n")); 653 _("GNS REST API initialized\n"));
653 return api; 654 return api;
@@ -667,6 +668,7 @@ libgnunet_plugin_rest_gns_done (void *cls)
667 struct Plugin *plugin = api->cls; 668 struct Plugin *plugin = api->cls;
668 669
669 plugin->cfg = NULL; 670 plugin->cfg = NULL;
671 GNUNET_free_non_null (api->allow_methods);
670 GNUNET_free (api); 672 GNUNET_free (api);
671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
672 "GNS REST plugin is finished\n"); 674 "GNS REST plugin is finished\n");
diff --git a/src/identity/plugin_rest_identity.c b/src/identity/plugin_rest_identity.c
index 53c74dcf2..8a2c24d1f 100644
--- a/src/identity/plugin_rest_identity.c
+++ b/src/identity/plugin_rest_identity.c
@@ -799,6 +799,14 @@ libgnunet_plugin_rest_identity_init (void *cls)
799 api->cls = &plugin; 799 api->cls = &plugin;
800 api->name = GNUNET_REST_API_NS_IDENTITY; 800 api->name = GNUNET_REST_API_NS_IDENTITY;
801 api->process_request = &rest_identity_process_request; 801 api->process_request = &rest_identity_process_request;
802 GNUNET_asprintf (&api->allow_methods,
803 "%s, %s, %s, %s, %s",
804 MHD_HTTP_METHOD_GET,
805 MHD_HTTP_METHOD_POST,
806 MHD_HTTP_METHOD_PUT,
807 MHD_HTTP_METHOD_DELETE,
808 MHD_HTTP_METHOD_OPTIONS);
809
802 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 810 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
803 _("Identity REST API initialized\n")); 811 _("Identity REST API initialized\n"));
804 return api; 812 return api;
@@ -818,6 +826,7 @@ libgnunet_plugin_rest_identity_done (void *cls)
818 struct Plugin *plugin = api->cls; 826 struct Plugin *plugin = api->cls;
819 827
820 plugin->cfg = NULL; 828 plugin->cfg = NULL;
829 GNUNET_free_non_null (api->allow_methods);
821 GNUNET_free (api); 830 GNUNET_free (api);
822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
823 "Identity REST plugin is finished\n"); 832 "Identity REST plugin is finished\n");
diff --git a/src/include/gnunet_rest_plugin.h b/src/include/gnunet_rest_plugin.h
index e1eabd813..abc0aea4c 100644
--- a/src/include/gnunet_rest_plugin.h
+++ b/src/include/gnunet_rest_plugin.h
@@ -59,6 +59,11 @@ struct GNUNET_REST_Plugin
59 char *name; 59 char *name;
60 60
61 /** 61 /**
62 * Supported HTTP Methods
63 */
64 char *allow_methods;
65
66 /**
62 * Function to process a REST call 67 * Function to process a REST call
63 * 68 *
64 * @param method the HTTP method called 69 * @param method the HTTP method called
diff --git a/src/rest/gnunet-rest-server.c b/src/rest/gnunet-rest-server.c
index c9c7d64bf..3ce177e9c 100644
--- a/src/rest/gnunet-rest-server.c
+++ b/src/rest/gnunet-rest-server.c
@@ -109,6 +109,16 @@ static const struct GNUNET_CONFIGURATION_Handle *cfg;
109static struct GNUNET_CONTAINER_MultiHashMap *plugin_map; 109static struct GNUNET_CONTAINER_MultiHashMap *plugin_map;
110 110
111/** 111/**
112 * Allowed Origins (CORS)
113 */
114static char* allow_origin;
115
116/**
117 * Allowed Headers (CORS)
118 */
119static char* allow_headers;
120
121/**
112 * MHD Connection handle 122 * MHD Connection handle
113 */ 123 */
114struct MhdConnectionHandle 124struct MhdConnectionHandle
@@ -325,13 +335,31 @@ create_response (void *cls,
325 { 335 {
326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
327 "Queueing response from plugin with MHD\n"); 337 "Queueing response from plugin with MHD\n");
328 /* FIXME: this is a bit dangerous... only for testing. */ 338 //Handle Preflights
329 MHD_add_response_header (con_handle->response, 339 if (0 == strcmp(meth, MHD_HTTP_METHOD_OPTIONS))
330 "Access-Control-Allow-Origin", 340 {
331 "*"); 341 if (NULL != allow_origin)
342 {
343 MHD_add_response_header (con_handle->response,
344 "Access-Control-Allow-Origin",
345 allow_origin);
346 }
347 if (NULL != allow_headers)
348 {
349 MHD_add_response_header (con_handle->response,
350 "Access-Control-Allow-Headers",
351 allow_headers);
352 }
353 if (NULL != con_handle->plugin)
354 {
355 MHD_add_response_header (con_handle->response,
356 "Access-Control-Allow-Methods",
357 con_handle->plugin->allow_methods);
358 }
359 }
332 int ret = MHD_queue_response (con, 360 int ret = MHD_queue_response (con,
333 con_handle->status, 361 con_handle->status,
334 con_handle->response); 362 con_handle->response);
335 cleanup_handle (con_handle); 363 cleanup_handle (con_handle);
336 return ret; 364 return ret;
337 } 365 }
@@ -547,6 +575,8 @@ do_shutdown (void *cls,
547 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 575 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
548 "Shutting down...\n"); 576 "Shutting down...\n");
549 kill_httpd (); 577 kill_httpd ();
578 GNUNET_free_non_null (allow_origin);
579 GNUNET_free_non_null (allow_headers);
550} 580}
551 581
552 582
@@ -679,6 +709,25 @@ run (void *cls,
679 cfg = c; 709 cfg = c;
680 plugin_map = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO); 710 plugin_map = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
681 711
712 /* Get CORS data from cfg */
713 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "rest",
714 "REST_ALLOW_ORIGIN",
715 &allow_origin))
716 {
717 //No origin specified
718 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
719 "No CORS Access-Control-Allow-Origin Header will be sent...\n");
720 }
721
722 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "rest",
723 "REST_ALLOW_HEADERS",
724 &allow_headers))
725 {
726 //No origin specified
727 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
728 "No CORS Access-Control-Allow-Headers Header will be sent...\n");
729 }
730
682 /* Open listen socket proxy */ 731 /* Open listen socket proxy */
683 lsock6 = bind_v6 (); 732 lsock6 = bind_v6 ();
684 if (NULL == lsock6) 733 if (NULL == lsock6)
diff --git a/src/rest/rest.conf b/src/rest/rest.conf
index febc51486..2ee3ba4b2 100644
--- a/src/rest/rest.conf
+++ b/src/rest/rest.conf
@@ -1,3 +1,5 @@
1[rest] 1[rest]
2BINARY=gnunet-rest-server 2BINARY=gnunet-rest-server
3REST_PORT=7776 3REST_PORT=7776
4REST_ALLOW_HEADERS=Authorization
5REST_ALLOW_ORIGIN=localhost