diff options
author | Martin Schanzenbach <mschanzenbach@posteo.de> | 2015-06-29 14:33:38 +0000 |
---|---|---|
committer | Martin Schanzenbach <mschanzenbach@posteo.de> | 2015-06-29 14:33:38 +0000 |
commit | 13aba2e2ba02476410ccee95974c7c5181f83fcf (patch) | |
tree | dd0285f8b00635452759ceebdd104c648b2e2f2a /src/rest/gnunet-rest-server.c | |
parent | d953fdc6293a9468019eebbcfed92c3b5387ec48 (diff) | |
download | gnunet-13aba2e2ba02476410ccee95974c7c5181f83fcf.tar.gz gnunet-13aba2e2ba02476410ccee95974c7c5181f83fcf.zip |
- add CORS logic
Diffstat (limited to 'src/rest/gnunet-rest-server.c')
-rw-r--r-- | src/rest/gnunet-rest-server.c | 61 |
1 files changed, 55 insertions, 6 deletions
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; | |||
109 | static struct GNUNET_CONTAINER_MultiHashMap *plugin_map; | 109 | static struct GNUNET_CONTAINER_MultiHashMap *plugin_map; |
110 | 110 | ||
111 | /** | 111 | /** |
112 | * Allowed Origins (CORS) | ||
113 | */ | ||
114 | static char* allow_origin; | ||
115 | |||
116 | /** | ||
117 | * Allowed Headers (CORS) | ||
118 | */ | ||
119 | static char* allow_headers; | ||
120 | |||
121 | /** | ||
112 | * MHD Connection handle | 122 | * MHD Connection handle |
113 | */ | 123 | */ |
114 | struct MhdConnectionHandle | 124 | struct 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) |