aboutsummaryrefslogtreecommitdiff
path: root/src/transport/plugin_transport_http_server.c
diff options
context:
space:
mode:
authorDavid Barksdale <amatus.amongus@gmail.com>2014-05-26 16:46:53 +0000
committerDavid Barksdale <amatus.amongus@gmail.com>2014-05-26 16:46:53 +0000
commitc6e5027466dbe379d4fad8795ca3a40da5bdb493 (patch)
tree9a6c953954b4df0015986497d20bb991f64175fb /src/transport/plugin_transport_http_server.c
parent52255e6f9663a192bd8fe567bf14eb6f4ee136c4 (diff)
downloadgnunet-c6e5027466dbe379d4fad8795ca3a40da5bdb493.tar.gz
gnunet-c6e5027466dbe379d4fad8795ca3a40da5bdb493.zip
Add CORS support to HTTP transport
Diffstat (limited to 'src/transport/plugin_transport_http_server.c')
-rw-r--r--src/transport/plugin_transport_http_server.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/transport/plugin_transport_http_server.c b/src/transport/plugin_transport_http_server.c
index fcad5f43b..950f02f8f 100644
--- a/src/transport/plugin_transport_http_server.c
+++ b/src/transport/plugin_transport_http_server.c
@@ -22,6 +22,7 @@
22 * @file transport/plugin_transport_http_server.c 22 * @file transport/plugin_transport_http_server.c
23 * @brief HTTP/S server transport plugin 23 * @brief HTTP/S server transport plugin
24 * @author Matthias Wachs 24 * @author Matthias Wachs
25 * @author David Barksdale
25 */ 26 */
26#include "platform.h" 27#include "platform.h"
27#include "gnunet_util_lib.h" 28#include "gnunet_util_lib.h"
@@ -1371,6 +1372,23 @@ server_receive_mst_cb (void *cls, void *client,
1371 return GNUNET_OK; 1372 return GNUNET_OK;
1372} 1373}
1373 1374
1375/**
1376 * Add headers to a request indicating that we allow Cross-Origin Resource
1377 * Sharing.
1378 */
1379static void
1380add_cors_headers(struct MHD_Response *response)
1381{
1382 MHD_add_response_header (response,
1383 "Access-Control-Allow-Origin",
1384 "*");
1385 MHD_add_response_header (response,
1386 "Access-Control-Allow-Methods",
1387 "GET, PUT, OPTIONS");
1388 MHD_add_response_header (response,
1389 "Access-Control-Max-Age",
1390 "86400");
1391}
1374 1392
1375/** 1393/**
1376 * MHD callback for a new incoming connection 1394 * MHD callback for a new incoming connection
@@ -1407,6 +1425,16 @@ server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
1407 GNUNET_assert (cls != NULL); 1425 GNUNET_assert (cls != NULL);
1408 if (sc == NULL) 1426 if (sc == NULL)
1409 { 1427 {
1428 /* CORS pre-flight request */
1429 if (0 == strcmp (MHD_HTTP_METHOD_OPTIONS, method))
1430 {
1431 response = MHD_create_response_from_buffer (0, NULL,
1432 MHD_RESPMEM_PERSISTENT);
1433 add_cors_headers(response);
1434 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1435 MHD_destroy_response (response);
1436 return res;
1437 }
1410 /* new connection */ 1438 /* new connection */
1411 sc = server_lookup_connection (plugin, mhd_connection, url, method); 1439 sc = server_lookup_connection (plugin, mhd_connection, url, method);
1412 if (sc != NULL) 1440 if (sc != NULL)
@@ -1419,6 +1447,7 @@ server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
1419 MHD_add_response_header (response, 1447 MHD_add_response_header (response,
1420 MHD_HTTP_HEADER_CONTENT_TYPE, 1448 MHD_HTTP_HEADER_CONTENT_TYPE,
1421 "text/html"); 1449 "text/html");
1450 add_cors_headers(response);
1422 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response); 1451 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1423 MHD_destroy_response (response); 1452 MHD_destroy_response (response);
1424 return res; 1453 return res;
@@ -1445,6 +1474,7 @@ server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
1445 response = MHD_create_response_from_data (strlen ("Thank you!"), 1474 response = MHD_create_response_from_data (strlen ("Thank you!"),
1446 "Thank you!", 1475 "Thank you!",
1447 MHD_NO, MHD_NO); 1476 MHD_NO, MHD_NO);
1477 add_cors_headers(response);
1448 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response); 1478 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1449 MHD_destroy_response (response); 1479 MHD_destroy_response (response);
1450 return MHD_YES; 1480 return MHD_YES;
@@ -1457,6 +1487,7 @@ server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
1457 32 * 1024, 1487 32 * 1024,
1458 &server_send_callback, s, 1488 &server_send_callback, s,
1459 NULL); 1489 NULL);
1490 add_cors_headers(response);
1460 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response); 1491 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1461 MHD_destroy_response (response); 1492 MHD_destroy_response (response);
1462 return MHD_YES; 1493 return MHD_YES;
@@ -1491,6 +1522,7 @@ server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
1491 response = MHD_create_response_from_data (strlen ("Thank you!"), 1522 response = MHD_create_response_from_data (strlen ("Thank you!"),
1492 "Thank you!", 1523 "Thank you!",
1493 MHD_NO, MHD_NO); 1524 MHD_NO, MHD_NO);
1525 add_cors_headers(response);
1494 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response); 1526 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1495 MHD_destroy_response (response); 1527 MHD_destroy_response (response);
1496 return MHD_YES; 1528 return MHD_YES;