gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r36022 - in gnunet/src: gns identity include rest


From: gnunet
Subject: [GNUnet-SVN] r36022 - in gnunet/src: gns identity include rest
Date: Mon, 29 Jun 2015 16:33:39 +0200

Author: schanzen
Date: 2015-06-29 16:33:38 +0200 (Mon, 29 Jun 2015)
New Revision: 36022

Modified:
   gnunet/src/gns/plugin_rest_gns.c
   gnunet/src/identity/plugin_rest_identity.c
   gnunet/src/include/gnunet_rest_plugin.h
   gnunet/src/rest/gnunet-rest-server.c
   gnunet/src/rest/rest.conf
Log:
- add CORS logic


Modified: gnunet/src/gns/plugin_rest_gns.c
===================================================================
--- gnunet/src/gns/plugin_rest_gns.c    2015-06-29 08:11:26 UTC (rev 36021)
+++ gnunet/src/gns/plugin_rest_gns.c    2015-06-29 14:33:38 UTC (rev 36022)
@@ -648,6 +648,7 @@
   api->cls = &plugin;
   api->name = API_NAMESPACE;
   api->process_request = &rest_gns_process_request;
+  GNUNET_asprintf (&api->allow_methods, "%s", MHD_HTTP_METHOD_GET);
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               _("GNS REST API initialized\n"));
   return api;
@@ -667,6 +668,7 @@
   struct Plugin *plugin = api->cls;
 
   plugin->cfg = NULL;
+  GNUNET_free_non_null (api->allow_methods);
   GNUNET_free (api);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "GNS REST plugin is finished\n");

Modified: gnunet/src/identity/plugin_rest_identity.c
===================================================================
--- gnunet/src/identity/plugin_rest_identity.c  2015-06-29 08:11:26 UTC (rev 
36021)
+++ gnunet/src/identity/plugin_rest_identity.c  2015-06-29 14:33:38 UTC (rev 
36022)
@@ -799,6 +799,14 @@
   api->cls = &plugin;
   api->name = GNUNET_REST_API_NS_IDENTITY;
   api->process_request = &rest_identity_process_request;
+  GNUNET_asprintf (&api->allow_methods,
+                   "%s, %s, %s, %s, %s",
+                   MHD_HTTP_METHOD_GET,
+                   MHD_HTTP_METHOD_POST,
+                   MHD_HTTP_METHOD_PUT,
+                   MHD_HTTP_METHOD_DELETE,
+                   MHD_HTTP_METHOD_OPTIONS);
+
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               _("Identity REST API initialized\n"));
   return api;
@@ -818,6 +826,7 @@
   struct Plugin *plugin = api->cls;
 
   plugin->cfg = NULL;
+  GNUNET_free_non_null (api->allow_methods);
   GNUNET_free (api);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Identity REST plugin is finished\n");

Modified: gnunet/src/include/gnunet_rest_plugin.h
===================================================================
--- gnunet/src/include/gnunet_rest_plugin.h     2015-06-29 08:11:26 UTC (rev 
36021)
+++ gnunet/src/include/gnunet_rest_plugin.h     2015-06-29 14:33:38 UTC (rev 
36022)
@@ -59,6 +59,11 @@
   char *name;
 
   /**
+   * Supported HTTP Methods
+   */
+  char *allow_methods;
+
+  /**
    * Function to process a REST call
    * 
    * @param method the HTTP method called

Modified: gnunet/src/rest/gnunet-rest-server.c
===================================================================
--- gnunet/src/rest/gnunet-rest-server.c        2015-06-29 08:11:26 UTC (rev 
36021)
+++ gnunet/src/rest/gnunet-rest-server.c        2015-06-29 14:33:38 UTC (rev 
36022)
@@ -109,6 +109,16 @@
 static struct GNUNET_CONTAINER_MultiHashMap *plugin_map;
 
 /**
+ * Allowed Origins (CORS)
+ */
+static char* allow_origin;
+
+/**
+ * Allowed Headers (CORS)
+ */
+static char* allow_headers;
+
+/**
  * MHD Connection handle 
  */
 struct MhdConnectionHandle
@@ -325,13 +335,31 @@
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "Queueing response from plugin with MHD\n");
-    /* FIXME: this is a bit dangerous... only for testing. */
-    MHD_add_response_header (con_handle->response,
-                            "Access-Control-Allow-Origin",
-                            "*");
+    //Handle Preflights
+    if (0 == strcmp(meth, MHD_HTTP_METHOD_OPTIONS))
+    {
+      if (NULL != allow_origin)
+      {
+        MHD_add_response_header (con_handle->response,
+                                 "Access-Control-Allow-Origin",
+                                 allow_origin);
+      }
+      if (NULL != allow_headers)
+      {
+        MHD_add_response_header (con_handle->response,
+                                 "Access-Control-Allow-Headers",
+                                 allow_headers);
+      }
+      if (NULL != con_handle->plugin)
+      {
+        MHD_add_response_header (con_handle->response,
+                                 "Access-Control-Allow-Methods",
+                                 con_handle->plugin->allow_methods);
+      }
+    }
     int ret = MHD_queue_response (con,
-                                 con_handle->status,
-                                 con_handle->response);
+                                  con_handle->status,
+                                  con_handle->response);
     cleanup_handle (con_handle);
     return ret;
   }
@@ -547,6 +575,8 @@
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "Shutting down...\n");
   kill_httpd ();
+  GNUNET_free_non_null (allow_origin);
+  GNUNET_free_non_null (allow_headers);
 }
 
 
@@ -679,6 +709,25 @@
   cfg = c;
   plugin_map = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
 
+  /* Get CORS data from cfg */
+  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "rest",
+                                                          "REST_ALLOW_ORIGIN",
+                                                          &allow_origin))
+  {
+    //No origin specified
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                "No CORS Access-Control-Allow-Origin Header will be 
sent...\n");
+  }
+
+  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "rest",
+                                                          "REST_ALLOW_HEADERS",
+                                                          &allow_headers))
+  {
+    //No origin specified
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                "No CORS Access-Control-Allow-Headers Header will be 
sent...\n");
+  }
+
   /* Open listen socket proxy */
   lsock6 = bind_v6 ();
   if (NULL == lsock6)

Modified: gnunet/src/rest/rest.conf
===================================================================
--- gnunet/src/rest/rest.conf   2015-06-29 08:11:26 UTC (rev 36021)
+++ gnunet/src/rest/rest.conf   2015-06-29 14:33:38 UTC (rev 36022)
@@ -1,3 +1,5 @@
 [rest]
 BINARY=gnunet-rest-server
 REST_PORT=7776
+REST_ALLOW_HEADERS=Authorization
+REST_ALLOW_ORIGIN=localhost




reply via email to

[Prev in Thread] Current Thread [Next in Thread]