aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/rest/gnunet-rest-server.c333
-rw-r--r--src/rest/rest.conf2
2 files changed, 162 insertions, 173 deletions
diff --git a/src/rest/gnunet-rest-server.c b/src/rest/gnunet-rest-server.c
index c7564614f..9bf7dec50 100644
--- a/src/rest/gnunet-rest-server.c
+++ b/src/rest/gnunet-rest-server.c
@@ -53,7 +53,8 @@
53/** 53/**
54 * After how long do we clean up unused MHD SSL/TLS instances? 54 * After how long do we clean up unused MHD SSL/TLS instances?
55 */ 55 */
56#define MHD_CACHE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5) 56#define MHD_CACHE_TIMEOUT \
57 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
57 58
58#define GN_REST_STATE_INIT 0 59#define GN_REST_STATE_INIT 0
59#define GN_REST_STATE_PROCESSING 1 60#define GN_REST_STATE_PROCESSING 1
@@ -91,12 +92,12 @@ static struct GNUNET_NETWORK_Handle *lsock6;
91/** 92/**
92 * The listen task ID for IPv4 93 * The listen task ID for IPv4
93 */ 94 */
94static struct GNUNET_SCHEDULER_Task * ltask4; 95static struct GNUNET_SCHEDULER_Task *ltask4;
95 96
96/** 97/**
97 * The listen task ID for IPv6 98 * The listen task ID for IPv6
98 */ 99 */
99static struct GNUNET_SCHEDULER_Task * ltask6; 100static struct GNUNET_SCHEDULER_Task *ltask6;
100 101
101/** 102/**
102 * Daemon for HTTP 103 * Daemon for HTTP
@@ -119,19 +120,19 @@ static const struct GNUNET_CONFIGURATION_Handle *cfg;
119static struct GNUNET_CONTAINER_MultiHashMap *plugin_map; 120static struct GNUNET_CONTAINER_MultiHashMap *plugin_map;
120 121
121/** 122/**
122 * Allowed Origins (CORS) 123 * Echo request Origin in CORS
123 */ 124 */
124static char* allow_origin; 125static int echo_origin;
125 126
126/** 127/**
127 * Allowed Headers (CORS) 128 * Allowed Headers (CORS)
128 */ 129 */
129static char* allow_headers; 130static char *allow_headers;
130 131
131/** 132/**
132 * Allowed Credentials (CORS) 133 * Allowed Credentials (CORS)
133 */ 134 */
134static char* allow_credentials; 135static char *allow_credentials;
135 136
136/** 137/**
137 * MHD Connection handle 138 * MHD Connection handle
@@ -176,9 +177,7 @@ run_mhd_now ()
176 GNUNET_SCHEDULER_cancel (httpd_task); 177 GNUNET_SCHEDULER_cancel (httpd_task);
177 httpd_task = NULL; 178 httpd_task = NULL;
178 } 179 }
179 httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd, 180 httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd, NULL);
180 NULL);
181
182} 181}
183 182
184/** 183/**
@@ -190,21 +189,17 @@ run_mhd_now ()
190 * @param status #GNUNET_OK if successful 189 * @param status #GNUNET_OK if successful
191 */ 190 */
192static void 191static void
193plugin_callback (void *cls, 192plugin_callback (void *cls, struct MHD_Response *resp, int status)
194 struct MHD_Response *resp,
195 int status)
196{ 193{
197 struct MhdConnectionHandle *handle = cls; 194 struct MhdConnectionHandle *handle = cls;
198 handle->status = status; 195 handle->status = status;
199 handle->response = resp; 196 handle->response = resp;
200 run_mhd_now(); 197 run_mhd_now ();
201} 198}
202 199
203 200
204static int 201static int
205cleanup_url_map (void *cls, 202cleanup_url_map (void *cls, const struct GNUNET_HashCode *key, void *value)
206 const struct GNUNET_HashCode *key,
207 void *value)
208{ 203{
209 GNUNET_free_non_null (value); 204 GNUNET_free_non_null (value);
210 return GNUNET_YES; 205 return GNUNET_YES;
@@ -221,17 +216,20 @@ cleanup_handle (struct MhdConnectionHandle *handle)
221 216
222 if (NULL != handle->data_handle->header_param_map) 217 if (NULL != handle->data_handle->header_param_map)
223 { 218 {
224 GNUNET_CONTAINER_multihashmap_iterate (handle->data_handle->header_param_map, 219 GNUNET_CONTAINER_multihashmap_iterate (handle->data_handle
220 ->header_param_map,
225 &cleanup_url_map, 221 &cleanup_url_map,
226 NULL); 222 NULL);
227 GNUNET_CONTAINER_multihashmap_destroy (handle->data_handle->header_param_map); 223 GNUNET_CONTAINER_multihashmap_destroy (
224 handle->data_handle->header_param_map);
228 } 225 }
229 if (NULL != handle->data_handle->url_param_map) 226 if (NULL != handle->data_handle->url_param_map)
230 { 227 {
231 GNUNET_CONTAINER_multihashmap_iterate (handle->data_handle->url_param_map, 228 GNUNET_CONTAINER_multihashmap_iterate (handle->data_handle->url_param_map,
232 &cleanup_url_map, 229 &cleanup_url_map,
233 NULL); 230 NULL);
234 GNUNET_CONTAINER_multihashmap_destroy (handle->data_handle->url_param_map); 231 GNUNET_CONTAINER_multihashmap_destroy (
232 handle->data_handle->url_param_map);
235 } 233 }
236 GNUNET_free (handle->data_handle); 234 GNUNET_free (handle->data_handle);
237 } 235 }
@@ -240,9 +238,9 @@ cleanup_handle (struct MhdConnectionHandle *handle)
240 238
241static int 239static int
242header_iterator (void *cls, 240header_iterator (void *cls,
243 enum MHD_ValueKind kind, 241 enum MHD_ValueKind kind,
244 const char *key, 242 const char *key,
245 const char *value) 243 const char *value)
246{ 244{
247 struct GNUNET_REST_RequestHandle *handle = cls; 245 struct GNUNET_REST_RequestHandle *handle = cls;
248 struct GNUNET_HashCode hkey; 246 struct GNUNET_HashCode hkey;
@@ -253,15 +251,16 @@ header_iterator (void *cls,
253 GNUNET_STRINGS_utf8_tolower (key, lowerkey); 251 GNUNET_STRINGS_utf8_tolower (key, lowerkey);
254 GNUNET_CRYPTO_hash (lowerkey, strlen (lowerkey), &hkey); 252 GNUNET_CRYPTO_hash (lowerkey, strlen (lowerkey), &hkey);
255 GNUNET_asprintf (&val, "%s", value); 253 GNUNET_asprintf (&val, "%s", value);
256 if (GNUNET_OK != 254 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (
257 GNUNET_CONTAINER_multihashmap_put (handle->header_param_map, 255 handle->header_param_map,
258 &hkey, 256 &hkey,
259 val, 257 val,
260 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) 258 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
261 { 259 {
262 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 260 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
263 "Could not load add header `%s'=%s\n", 261 "Could not load add header `%s'=%s\n",
264 lowerkey, value); 262 lowerkey,
263 value);
265 } 264 }
266 GNUNET_free (lowerkey); 265 GNUNET_free (lowerkey);
267 return MHD_YES; 266 return MHD_YES;
@@ -280,29 +279,30 @@ url_iterator (void *cls,
280 279
281 GNUNET_CRYPTO_hash (key, strlen (key), &hkey); 280 GNUNET_CRYPTO_hash (key, strlen (key), &hkey);
282 GNUNET_asprintf (&val, "%s", value); 281 GNUNET_asprintf (&val, "%s", value);
283 if (GNUNET_OK != 282 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (
284 GNUNET_CONTAINER_multihashmap_put (handle->url_param_map, 283 handle->url_param_map,
285 &hkey, 284 &hkey,
286 val, 285 val,
287 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) 286 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
288 { 287 {
289 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 288 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
290 "Could not load add url param `%s'=%s\n", 289 "Could not load add url param `%s'=%s\n",
291 key, value); 290 key,
291 value);
292 } 292 }
293 return MHD_YES; 293 return MHD_YES;
294} 294}
295 295
296static int 296static int
297post_data_iter (void *cls, 297post_data_iter (void *cls,
298 enum MHD_ValueKind kind, 298 enum MHD_ValueKind kind,
299 const char *key, 299 const char *key,
300 const char *filename, 300 const char *filename,
301 const char *content_type, 301 const char *content_type,
302 const char *transfer_encoding, 302 const char *transfer_encoding,
303 const char *data, 303 const char *data,
304 uint64_t off, 304 uint64_t off,
305 size_t size) 305 size_t size)
306{ 306{
307 struct GNUNET_REST_RequestHandle *handle = cls; 307 struct GNUNET_REST_RequestHandle *handle = cls;
308 struct GNUNET_HashCode hkey; 308 struct GNUNET_HashCode hkey;
@@ -313,19 +313,19 @@ post_data_iter (void *cls,
313 313
314 GNUNET_CRYPTO_hash (key, strlen (key), &hkey); 314 GNUNET_CRYPTO_hash (key, strlen (key), &hkey);
315 GNUNET_asprintf (&val, "%s", data); 315 GNUNET_asprintf (&val, "%s", data);
316 if (GNUNET_OK != 316 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (
317 GNUNET_CONTAINER_multihashmap_put (handle->url_param_map, 317 handle->url_param_map,
318 &hkey, 318 &hkey,
319 val, 319 val,
320 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) 320 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
321 { 321 {
322 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 322 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
323 "Could not load add url param '%s'=%s\n", 323 "Could not load add url param '%s'=%s\n",
324 key, data); 324 key,
325 GNUNET_free(val); 325 data);
326 GNUNET_free (val);
326 } 327 }
327 return MHD_YES; 328 return MHD_YES;
328
329} 329}
330 330
331/* ********************************* MHD response generation ******************* */ 331/* ********************************* MHD response generation ******************* */
@@ -364,6 +364,7 @@ create_response (void *cls,
364 void **con_cls) 364 void **con_cls)
365{ 365{
366 char *plugin_name; 366 char *plugin_name;
367 char *origin;
367 struct GNUNET_HashCode key; 368 struct GNUNET_HashCode key;
368 struct MhdConnectionHandle *con_handle; 369 struct MhdConnectionHandle *con_handle;
369 struct GNUNET_REST_RequestHandle *rest_conndata_handle; 370 struct GNUNET_REST_RequestHandle *rest_conndata_handle;
@@ -372,32 +373,27 @@ create_response (void *cls,
372 373
373 if (NULL == *con_cls) 374 if (NULL == *con_cls)
374 { 375 {
375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New connection %s\n", url);
376 "New connection %s\n", url); 377 char tmp_url[strlen (url) + 1];
377 char tmp_url[strlen(url)+1];
378 strcpy (tmp_url, url); 378 strcpy (tmp_url, url);
379 con_handle = GNUNET_new (struct MhdConnectionHandle); 379 con_handle = GNUNET_new (struct MhdConnectionHandle);
380 con_handle->con = con; 380 con_handle->con = con;
381 con_handle->state = GN_REST_STATE_INIT; 381 con_handle->state = GN_REST_STATE_INIT;
382 *con_cls = con_handle; 382 *con_cls = con_handle;
383 383
384 plugin_name = strtok(tmp_url, "/"); 384 plugin_name = strtok (tmp_url, "/");
385 385
386 if (NULL != plugin_name) 386 if (NULL != plugin_name)
387 { 387 {
388 GNUNET_CRYPTO_hash (plugin_name, strlen (plugin_name), &key); 388 GNUNET_CRYPTO_hash (plugin_name, strlen (plugin_name), &key);
389 389
390 con_handle->plugin = GNUNET_CONTAINER_multihashmap_get (plugin_map, 390 con_handle->plugin = GNUNET_CONTAINER_multihashmap_get (plugin_map, &key);
391 &key);
392 } 391 }
393 if (NULL == con_handle->plugin) 392 if (NULL == con_handle->plugin)
394 { 393 {
395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Queueing response with MHD\n");
396 "Queueing response with MHD\n");
397 GNUNET_free (con_handle); 395 GNUNET_free (con_handle);
398 return MHD_queue_response (con, 396 return MHD_queue_response (con, MHD_HTTP_NOT_FOUND, failure_response);
399 MHD_HTTP_NOT_FOUND,
400 failure_response);
401 } 397 }
402 398
403 return MHD_YES; 399 return MHD_YES;
@@ -409,10 +405,10 @@ create_response (void *cls,
409 rest_conndata_handle->url = url; 405 rest_conndata_handle->url = url;
410 rest_conndata_handle->data = upload_data; 406 rest_conndata_handle->data = upload_data;
411 rest_conndata_handle->data_size = *upload_data_size; 407 rest_conndata_handle->data_size = *upload_data_size;
412 rest_conndata_handle->url_param_map = GNUNET_CONTAINER_multihashmap_create (16, 408 rest_conndata_handle->url_param_map =
413 GNUNET_NO); 409 GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
414 rest_conndata_handle->header_param_map = GNUNET_CONTAINER_multihashmap_create (16, 410 rest_conndata_handle->header_param_map =
415 GNUNET_NO); 411 GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
416 con_handle->data_handle = rest_conndata_handle; 412 con_handle->data_handle = rest_conndata_handle;
417 MHD_get_connection_values (con, 413 MHD_get_connection_values (con,
418 MHD_GET_ARGUMENT_KIND, 414 MHD_GET_ARGUMENT_KIND,
@@ -422,15 +418,15 @@ create_response (void *cls,
422 MHD_HEADER_KIND, 418 MHD_HEADER_KIND,
423 (MHD_KeyValueIterator) &header_iterator, 419 (MHD_KeyValueIterator) &header_iterator,
424 rest_conndata_handle); 420 rest_conndata_handle);
425 con_handle->pp = MHD_create_post_processor(con, 421 con_handle->pp = MHD_create_post_processor (con,
426 65536, 422 65536,
427 post_data_iter, 423 post_data_iter,
428 rest_conndata_handle); 424 rest_conndata_handle);
429 if (*upload_data_size) 425 if (*upload_data_size)
430 { 426 {
431 MHD_post_process(con_handle->pp, upload_data, *upload_data_size); 427 MHD_post_process (con_handle->pp, upload_data, *upload_data_size);
432 } 428 }
433 MHD_destroy_post_processor(con_handle->pp); 429 MHD_destroy_post_processor (con_handle->pp);
434 430
435 con_handle->state = GN_REST_STATE_PROCESSING; 431 con_handle->state = GN_REST_STATE_PROCESSING;
436 con_handle->plugin->process_request (rest_conndata_handle, 432 con_handle->plugin->process_request (rest_conndata_handle,
@@ -443,11 +439,18 @@ create_response (void *cls,
443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
444 "Queueing response from plugin with MHD\n"); 440 "Queueing response from plugin with MHD\n");
445 //Handle Preflights 441 //Handle Preflights
446 if (NULL != allow_origin) 442 if (GNUNET_YES == echo_origin)
447 { 443 {
448 MHD_add_response_header (con_handle->response, 444 GNUNET_CRYPTO_hash (MHD_HTTP_HEADER_ORIGIN,
449 MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, 445 strlen (MHD_HTTP_HEADER_ORIGIN),
450 allow_origin); 446 &key);
447 origin = GNUNET_CONTAINER_multihashmap_get (con_handle->data_handle
448 ->header_param_map,
449 &key);
450 if (NULL != origin)
451 MHD_add_response_header (con_handle->response,
452 MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN,
453 origin);
451 } 454 }
452 if (NULL != allow_credentials) 455 if (NULL != allow_credentials)
453 { 456 {
@@ -461,9 +464,8 @@ create_response (void *cls,
461 "Access-Control-Allow-Headers", 464 "Access-Control-Allow-Headers",
462 allow_headers); 465 allow_headers);
463 } 466 }
464 int ret = MHD_queue_response (con, 467 int ret =
465 con_handle->status, 468 MHD_queue_response (con, con_handle->status, con_handle->response);
466 con_handle->response);
467 cleanup_handle (con_handle); 469 cleanup_handle (con_handle);
468 return ret; 470 return ret;
469 } 471 }
@@ -532,7 +534,7 @@ kill_httpd ()
532 GNUNET_NETWORK_socket_close (lsock6); 534 GNUNET_NETWORK_socket_close (lsock6);
533 lsock6 = NULL; 535 lsock6 = NULL;
534 } 536 }
535 } 537}
536 538
537 539
538/** 540/**
@@ -586,14 +588,14 @@ schedule_httpd ()
586 GNUNET_SCHEDULER_cancel (httpd_task); 588 GNUNET_SCHEDULER_cancel (httpd_task);
587 httpd_task = NULL; 589 httpd_task = NULL;
588 } 590 }
589 if ( (MHD_YES == haveto) || 591 if ((MHD_YES == haveto) || (-1 != max))
590 (-1 != max))
591 { 592 {
592 httpd_task = 593 httpd_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
593 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, 594 tv,
594 tv, wrs, wws, 595 wrs,
595 &do_httpd, NULL); 596 wws,
596 597 &do_httpd,
598 NULL);
597 } 599 }
598 if (NULL != wrs) 600 if (NULL != wrs)
599 GNUNET_NETWORK_fdset_destroy (wrs); 601 GNUNET_NETWORK_fdset_destroy (wrs);
@@ -635,15 +637,15 @@ do_accept (void *cls)
635 { 637 {
636 ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 638 ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
637 lsock, 639 lsock,
638 &do_accept, lsock); 640 &do_accept,
639 641 lsock);
640 } 642 }
641 else if (lsock == lsock6) 643 else if (lsock == lsock6)
642 { 644 {
643 ltask6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 645 ltask6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
644 lsock, 646 lsock,
645 &do_accept, lsock); 647 &do_accept,
646 648 lsock);
647 } 649 }
648 else 650 else
649 GNUNET_assert (0); 651 GNUNET_assert (0);
@@ -661,10 +663,10 @@ do_accept (void *cls)
661 if (MHD_YES != MHD_add_connection (httpd, fd, addr, len)) 663 if (MHD_YES != MHD_add_connection (httpd, fd, addr, len))
662 { 664 {
663 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 665 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
664 _("Failed to pass client to MHD\n")); 666 _ ("Failed to pass client to MHD\n"));
665 return; 667 return;
666 } 668 }
667 GNUNET_free(s); 669 GNUNET_free (s);
668 schedule_httpd (); 670 schedule_httpd ();
669} 671}
670 672
@@ -677,10 +679,8 @@ do_accept (void *cls)
677static void 679static void
678do_shutdown (void *cls) 680do_shutdown (void *cls)
679{ 681{
680 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 682 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down...\n");
681 "Shutting down...\n");
682 kill_httpd (); 683 kill_httpd ();
683 GNUNET_free_non_null (allow_origin);
684 GNUNET_free_non_null (allow_credentials); 684 GNUNET_free_non_null (allow_credentials);
685 GNUNET_free_non_null (allow_headers); 685 GNUNET_free_non_null (allow_headers);
686} 686}
@@ -705,14 +705,12 @@ bind_v4 ()
705#if HAVE_SOCKADDR_IN_SIN_LEN 705#if HAVE_SOCKADDR_IN_SIN_LEN
706 sa4.sin_len = sizeof (sa4); 706 sa4.sin_len = sizeof (sa4);
707#endif 707#endif
708 ls = GNUNET_NETWORK_socket_create (AF_INET, 708 ls = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0);
709 SOCK_STREAM,
710 0);
711 if (NULL == ls) 709 if (NULL == ls)
712 return NULL; 710 return NULL;
713 if (GNUNET_OK != 711 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (ls,
714 GNUNET_NETWORK_socket_bind (ls, (const struct sockaddr *) &sa4, 712 (const struct sockaddr *) &sa4,
715 sizeof (sa4))) 713 sizeof (sa4)))
716 { 714 {
717 eno = errno; 715 eno = errno;
718 GNUNET_NETWORK_socket_close (ls); 716 GNUNET_NETWORK_socket_close (ls);
@@ -742,14 +740,12 @@ bind_v6 ()
742#if HAVE_SOCKADDR_IN_SIN_LEN 740#if HAVE_SOCKADDR_IN_SIN_LEN
743 sa6.sin6_len = sizeof (sa6); 741 sa6.sin6_len = sizeof (sa6);
744#endif 742#endif
745 ls = GNUNET_NETWORK_socket_create (AF_INET6, 743 ls = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_STREAM, 0);
746 SOCK_STREAM,
747 0);
748 if (NULL == ls) 744 if (NULL == ls)
749 return NULL; 745 return NULL;
750 if (GNUNET_OK != 746 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (ls,
751 GNUNET_NETWORK_socket_bind (ls, (const struct sockaddr *) &sa6, 747 (const struct sockaddr *) &sa6,
752 sizeof (sa6))) 748 sizeof (sa6)))
753 { 749 {
754 eno = errno; 750 eno = errno;
755 GNUNET_NETWORK_socket_close (ls); 751 GNUNET_NETWORK_socket_close (ls);
@@ -768,9 +764,7 @@ bind_v6 ()
768 * @param lib_ret the object returned by the plugin initializer 764 * @param lib_ret the object returned by the plugin initializer
769 */ 765 */
770static void 766static void
771load_plugin (void *cls, 767load_plugin (void *cls, const char *libname, void *lib_ret)
772 const char *libname,
773 void *lib_ret)
774{ 768{
775 struct GNUNET_REST_Plugin *plugin = lib_ret; 769 struct GNUNET_REST_Plugin *plugin = lib_ret;
776 struct GNUNET_HashCode key; 770 struct GNUNET_HashCode key;
@@ -783,20 +777,19 @@ load_plugin (void *cls,
783 } 777 }
784 GNUNET_assert (1 < strlen (plugin->name)); 778 GNUNET_assert (1 < strlen (plugin->name));
785 GNUNET_assert ('/' == *plugin->name); 779 GNUNET_assert ('/' == *plugin->name);
786 GNUNET_CRYPTO_hash (plugin->name+1, strlen (plugin->name+1), &key); 780 GNUNET_CRYPTO_hash (plugin->name + 1, strlen (plugin->name + 1), &key);
787 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (plugin_map, 781 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (
788 &key, 782 plugin_map,
789 plugin, 783 &key,
790 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) 784 plugin,
785 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
791 { 786 {
792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
793 "Could not load add plugin `%s'\n", 788 "Could not load add plugin `%s'\n",
794 libname); 789 libname);
795 return; 790 return;
796 } 791 }
797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded plugin `%s'\n", libname);
798 "Loaded plugin `%s'\n",
799 libname);
800} 793}
801 794
802 795
@@ -814,29 +807,25 @@ run (void *cls,
814 const char *cfgfile, 807 const char *cfgfile,
815 const struct GNUNET_CONFIGURATION_Handle *c) 808 const struct GNUNET_CONFIGURATION_Handle *c)
816{ 809{
817 char* addr_str; 810 char *addr_str;
818 cfg = c; 811 cfg = c;
819 plugin_map = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO); 812 plugin_map = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
820 813
821 /* Get port to bind to */ 814 /* Get port to bind to */
822 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "rest", 815 if (GNUNET_OK !=
823 "HTTP_PORT", 816 GNUNET_CONFIGURATION_get_value_number (cfg, "rest", "HTTP_PORT", &port))
824 &port))
825 { 817 {
826 //No address specified 818 //No address specified
827 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 819 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Using default port...\n");
828 "Using default port...\n");
829 port = GNUNET_REST_SERVICE_PORT; 820 port = GNUNET_REST_SERVICE_PORT;
830 } 821 }
831 822
832 /* Get address to bind to */ 823 /* Get address to bind to */
833 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "rest", 824 if (GNUNET_OK !=
834 "BIND_TO", 825 GNUNET_CONFIGURATION_get_value_string (cfg, "rest", "BIND_TO", &addr_str))
835 &addr_str))
836 { 826 {
837 //No address specified 827 //No address specified
838 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 828 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Don't know what to bind to...\n");
839 "Don't know what to bind to...\n");
840 GNUNET_SCHEDULER_shutdown (); 829 GNUNET_SCHEDULER_shutdown ();
841 return; 830 return;
842 } 831 }
@@ -851,13 +840,13 @@ run (void *cls,
851 } 840 }
852 GNUNET_free (addr_str); 841 GNUNET_free (addr_str);
853 /* Get address to bind to */ 842 /* Get address to bind to */
854 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "rest", 843 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
844 "rest",
855 "BIND_TO6", 845 "BIND_TO6",
856 &addr_str)) 846 &addr_str))
857 { 847 {
858 //No address specified 848 //No address specified
859 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 849 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Don't know what to bind6 to...\n");
860 "Don't know what to bind6 to...\n");
861 GNUNET_SCHEDULER_shutdown (); 850 GNUNET_SCHEDULER_shutdown ();
862 return; 851 return;
863 } 852 }
@@ -874,25 +863,21 @@ run (void *cls,
874 863
875 864
876 /* Get CORS data from cfg */ 865 /* Get CORS data from cfg */
877 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "rest", 866 echo_origin =
878 "REST_ALLOW_ORIGIN", 867 GNUNET_CONFIGURATION_get_value_yesno (cfg, "rest", "REST_ECHO_ORIGIN");
879 &allow_origin)) 868 if (GNUNET_OK !=
880 { 869 GNUNET_CONFIGURATION_get_value_string (cfg,
881 //No origin specified 870 "rest",
882 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 871 "REST_ALLOW_CREDENTIALS",
883 "No CORS Access-Control-Allow-Origin Header will be sent...\n"); 872 &allow_credentials))
884 }
885
886 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "rest",
887 "REST_ALLOW_CREDENTIALS",
888 &allow_credentials))
889 { 873 {
890 //No origin specified 874 //No origin specified
891 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 875 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
892 "No CORS Access-Control-Allow-Origin Header will be sent...\n"); 876 "No CORS Access-Control-Allow-Origin Header will be sent...\n");
893 } 877 }
894 878
895 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "rest", 879 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
880 "rest",
896 "REST_ALLOW_HEADERS", 881 "REST_ALLOW_HEADERS",
897 &allow_headers)) 882 &allow_headers))
898 { 883 {
@@ -918,8 +903,9 @@ run (void *cls,
918 else 903 else
919 { 904 {
920 ltask6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 905 ltask6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
921 lsock6, &do_accept, lsock6); 906 lsock6,
922 907 &do_accept,
908 lsock6);
923 } 909 }
924 } 910 }
925 lsock4 = bind_v4 (); 911 lsock4 = bind_v4 ();
@@ -938,25 +924,28 @@ run (void *cls,
938 else 924 else
939 { 925 {
940 ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 926 ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
941 lsock4, &do_accept, lsock4); 927 lsock4,
942 928 &do_accept,
929 lsock4);
943 } 930 }
944 } 931 }
945 if ( (NULL == lsock4) && 932 if ((NULL == lsock4) && (NULL == lsock6))
946 (NULL == lsock6) )
947 { 933 {
948 GNUNET_SCHEDULER_shutdown (); 934 GNUNET_SCHEDULER_shutdown ();
949 return; 935 return;
950 } 936 }
951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service listens on port %llu\n", port);
952 "Service listens on port %llu\n",
953 port);
954 httpd = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_NO_LISTEN_SOCKET, 938 httpd = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_NO_LISTEN_SOCKET,
955 0, 939 0,
956 NULL, NULL, 940 NULL,
957 &create_response, NULL, 941 NULL,
958 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16, 942 &create_response,
959 MHD_OPTION_NOTIFY_COMPLETED, &mhd_completed_cb, NULL, 943 NULL,
944 MHD_OPTION_CONNECTION_TIMEOUT,
945 (unsigned int) 16,
946 MHD_OPTION_NOTIFY_COMPLETED,
947 &mhd_completed_cb,
948 NULL,
960 MHD_OPTION_END); 949 MHD_OPTION_END);
961 if (NULL == httpd) 950 if (NULL == httpd)
962 { 951 {
@@ -984,25 +973,25 @@ run (void *cls,
984int 973int
985main (int argc, char *const *argv) 974main (int argc, char *const *argv)
986{ 975{
987 struct GNUNET_GETOPT_CommandLineOption options[] = { 976 struct GNUNET_GETOPT_CommandLineOption options[] = {GNUNET_GETOPT_OPTION_END};
988 GNUNET_GETOPT_OPTION_END 977 static const char *err_page = "{}";
989 };
990 static const char* err_page =
991 "{}";
992 int ret; 978 int ret;
993 979
994 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) 980 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
995 return 2; 981 return 2;
996 GNUNET_log_setup ("gnunet-rest-server", "WARNING", NULL); 982 GNUNET_log_setup ("gnunet-rest-server", "WARNING", NULL);
997 failure_response = MHD_create_response_from_buffer (strlen(err_page), 983 failure_response = MHD_create_response_from_buffer (strlen (err_page),
998 (void*)err_page, 984 (void *) err_page,
999 MHD_RESPMEM_PERSISTENT); 985 MHD_RESPMEM_PERSISTENT);
1000 ret = 986 ret = (GNUNET_OK == GNUNET_PROGRAM_run (argc,
1001 (GNUNET_OK == 987 argv,
1002 GNUNET_PROGRAM_run (argc, argv, "gnunet-rest-server", 988 "gnunet-rest-server",
1003 _("GNUnet REST server"), 989 _ ("GNUnet REST server"),
1004 options, 990 options,
1005 &run, NULL)) ? 0: 1; 991 &run,
992 NULL))
993 ? 0
994 : 1;
1006 MHD_destroy_response (failure_response); 995 MHD_destroy_response (failure_response);
1007 GNUNET_free_non_null ((char *) argv); 996 GNUNET_free_non_null ((char *) argv);
1008 return ret; 997 return ret;
diff --git a/src/rest/rest.conf b/src/rest/rest.conf
index 71fef8aa1..504f82a1b 100644
--- a/src/rest/rest.conf
+++ b/src/rest/rest.conf
@@ -6,5 +6,5 @@ HTTP_PORT=7776
6BIND_TO=127.0.0.1 6BIND_TO=127.0.0.1
7BIND_TO6=::1 7BIND_TO6=::1
8REST_ALLOW_HEADERS=Authorization,Accept,Content-Type 8REST_ALLOW_HEADERS=Authorization,Accept,Content-Type
9REST_ALLOW_ORIGIN=* 9REST_ECHO_ORIGIN=YES
10REST_ALLOW_CREDENTIALS=true 10REST_ALLOW_CREDENTIALS=true