Merge branch 'master' of ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / rest / gnunet-rest-server.c
1 /*
2    This file is part of GNUnet.
3    Copyright (C) 2012-2015 GNUnet e.V.
4
5    GNUnet is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published
7    by the Free Software Foundation; either version 3, or (at your
8    option) any later version.
9
10    GNUnet is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with GNUnet; see the file COPYING.  If not, write to the
17    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18    Boston, MA 02110-1301, USA.
19    */
20 /**
21  * @author Martin Schanzenbach
22  * @file src/rest/gnunet-rest-server.c
23  * @brief REST service for GNUnet services
24  *
25  */
26 #include "platform.h"
27 #include <microhttpd.h>
28 #include "gnunet_util_lib.h"
29 #include "gnunet_rest_plugin.h"
30
31
32 /**
33  * Default Socks5 listen port.
34  */
35 #define GNUNET_REST_SERVICE_PORT 7776
36
37 /**
38  * Maximum supported length for a URI.
39  * Should die. @deprecated
40  */
41 #define MAX_HTTP_URI_LENGTH 2048
42
43 /**
44  * Port for plaintext HTTP.
45  */
46 #define HTTP_PORT 80
47
48 /**
49  * Port for HTTPS.
50  */
51 #define HTTPS_PORT 443
52
53 /**
54  * After how long do we clean up unused MHD SSL/TLS instances?
55  */
56 #define MHD_CACHE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
57
58 #define GN_REST_STATE_INIT 0
59 #define GN_REST_STATE_PROCESSING 1
60
61 /**
62  * The task ID
63  */
64 static struct GNUNET_SCHEDULER_Task *httpd_task;
65
66 /**
67  * The port the service is running on (default 7776)
68  */
69 static unsigned long long port = GNUNET_REST_SERVICE_PORT;
70
71 /**
72  * The listen socket of the service for IPv4
73  */
74 static struct GNUNET_NETWORK_Handle *lsock4;
75
76 /**
77  * The listen socket of the service for IPv6
78  */
79 static struct GNUNET_NETWORK_Handle *lsock6;
80
81 /**
82  * The listen task ID for IPv4
83  */
84 static struct GNUNET_SCHEDULER_Task * ltask4;
85
86 /**
87  * The listen task ID for IPv6
88  */
89 static struct GNUNET_SCHEDULER_Task * ltask6;
90
91 /**
92  * Daemon for HTTP
93  */
94 static struct MHD_Daemon *httpd;
95
96 /**
97  * Response we return on failures.
98  */
99 static struct MHD_Response *failure_response;
100
101 /**
102  * Our configuration.
103  */
104 static const struct GNUNET_CONFIGURATION_Handle *cfg;
105
106 /**
107  * Map of loaded plugins.
108  */
109 static struct GNUNET_CONTAINER_MultiHashMap *plugin_map;
110
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 /**
122  * MHD Connection handle
123  */
124 struct MhdConnectionHandle
125 {
126   struct MHD_Connection *con;
127
128   struct MHD_Response *response;
129
130   struct GNUNET_REST_Plugin *plugin;
131
132   struct GNUNET_REST_RequestHandle *data_handle;
133
134   int status;
135
136   int state;
137 };
138
139 /* ************************* Global helpers ********************* */
140
141
142 /**
143  * Task run whenever HTTP server operations are pending.
144  *
145  * @param cls NULL
146  */
147 static void
148 do_httpd (void *cls);
149
150
151 /**
152  * Run MHD now, we have extra data ready for the callback.
153  */
154 static void
155 run_mhd_now ()
156 {
157   if (NULL != httpd_task)
158   {
159     GNUNET_SCHEDULER_cancel (httpd_task);
160     httpd_task = NULL;
161   }
162   httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd,
163                                          NULL);
164
165 }
166
167 /**
168  * Plugin result callback
169  *
170  * @param cls closure (MHD connection handle)
171  * @param data the data to return to the caller
172  * @param len length of the data
173  * @param status #GNUNET_OK if successful
174  */
175 static void
176 plugin_callback (void *cls,
177                  struct MHD_Response *resp,
178                  int status)
179 {
180   struct MhdConnectionHandle *handle = cls;
181   handle->status = status;
182   handle->response = resp;
183   run_mhd_now();
184 }
185
186
187 static int
188 cleanup_url_map (void *cls,
189                  const struct GNUNET_HashCode *key,
190                  void *value)
191 {
192   GNUNET_free_non_null (value);
193   return GNUNET_YES;
194 }
195
196
197 static void
198 cleanup_handle (struct MhdConnectionHandle *handle)
199 {
200   if (NULL != handle->response)
201     MHD_destroy_response (handle->response);
202   if (NULL != handle->data_handle)
203   {
204     if (NULL != handle->data_handle->url_param_map)
205     {
206       GNUNET_CONTAINER_multihashmap_iterate (handle->data_handle->url_param_map,
207                                              &cleanup_url_map,
208                                              NULL);
209       GNUNET_CONTAINER_multihashmap_destroy (handle->data_handle->url_param_map);
210     }
211     GNUNET_free (handle->data_handle);
212   }
213   GNUNET_free (handle);
214 }
215
216
217 static int
218 url_iterator (void *cls,
219               enum MHD_ValueKind kind,
220               const char *key,
221               const char *value)
222 {
223   struct GNUNET_REST_RequestHandle *handle = cls;
224   struct GNUNET_HashCode hkey;
225   char *val;
226
227   GNUNET_CRYPTO_hash (key, strlen (key), &hkey);
228   GNUNET_asprintf (&val, "%s", value);
229   if (GNUNET_OK !=
230       GNUNET_CONTAINER_multihashmap_put (handle->url_param_map,
231                                          &hkey,
232                                          val,
233                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
234   {
235     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
236                 "Could not load add url param `%s'=%s\n",
237                 key, value);
238   }
239   return MHD_YES;
240 }
241
242 /* ********************************* MHD response generation ******************* */
243
244 /**
245  * Main MHD callback for handling requests.
246  *
247  * @param cls unused
248  * @param con MHD connection handle
249  * @param url the url in the request
250  * @param meth the HTTP method used ("GET", "PUT", etc.)
251  * @param ver the HTTP version string (i.e. "HTTP/1.1")
252  * @param upload_data the data being uploaded (excluding HEADERS,
253  *        for a POST that fits into memory and that is encoded
254  *        with a supported encoding, the POST data will NOT be
255  *        given in upload_data and is instead available as
256  *        part of MHD_get_connection_values; very large POST
257  *        data *will* be made available incrementally in
258  *        upload_data)
259  * @param upload_data_size set initially to the size of the
260  *        @a upload_data provided; the method must update this
261  *        value to the number of bytes NOT processed;
262  * @param con_cls pointer to location where we store the 'struct Request'
263  * @return MHD_YES if the connection was handled successfully,
264  *         MHD_NO if the socket must be closed due to a serious
265  *         error while handling the request
266  */
267 static int
268 create_response (void *cls,
269                  struct MHD_Connection *con,
270                  const char *url,
271                  const char *meth,
272                  const char *ver,
273                  const char *upload_data,
274                  size_t *upload_data_size,
275                  void **con_cls)
276 {
277   char *plugin_name;
278   struct GNUNET_HashCode key;
279   struct MhdConnectionHandle *con_handle;
280   struct GNUNET_REST_RequestHandle *rest_conndata_handle;
281
282   con_handle = *con_cls;
283
284   if (NULL == *con_cls)
285   {
286     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
287                 "New connection %s\n", url);
288     char tmp_url[strlen(url)+1];
289     strcpy (tmp_url, url);
290     con_handle = GNUNET_new (struct MhdConnectionHandle);
291     con_handle->con = con;
292     con_handle->state = GN_REST_STATE_INIT;
293     *con_cls = con_handle;
294
295     plugin_name = strtok(tmp_url, "/");
296
297     if (NULL != plugin_name)
298     {
299       GNUNET_CRYPTO_hash (plugin_name, strlen (plugin_name), &key);
300
301       con_handle->plugin = GNUNET_CONTAINER_multihashmap_get (plugin_map,
302                                                               &key);
303     }
304     if (NULL == con_handle->plugin)
305     {
306       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
307                   "Queueing response with MHD\n");
308       GNUNET_free (con_handle);
309       return MHD_queue_response (con,
310                                  MHD_HTTP_NOT_FOUND,
311                                  failure_response);
312     }
313     return MHD_YES;
314   }
315   if (GN_REST_STATE_INIT == con_handle->state)
316   {
317     rest_conndata_handle = GNUNET_new (struct GNUNET_REST_RequestHandle);
318     rest_conndata_handle->method = meth;
319     rest_conndata_handle->url = url;
320     rest_conndata_handle->data = upload_data;
321     rest_conndata_handle->data_size = *upload_data_size;
322     rest_conndata_handle->url_param_map = GNUNET_CONTAINER_multihashmap_create (16,
323                                                                                 GNUNET_NO);
324     con_handle->data_handle = rest_conndata_handle;
325     MHD_get_connection_values (con,
326                                MHD_GET_ARGUMENT_KIND,
327                                &url_iterator,
328                                rest_conndata_handle);
329     con_handle->state = GN_REST_STATE_PROCESSING;
330     con_handle->plugin->process_request (rest_conndata_handle,
331                                          &plugin_callback,
332                                          con_handle);
333     *upload_data_size = 0;
334   }
335   if (NULL != con_handle->response)
336   {
337     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
338                 "Queueing response from plugin with MHD\n");
339     //Handle Preflights
340     if (NULL != allow_origin)
341     {
342       MHD_add_response_header (con_handle->response,
343                                MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN,
344                                allow_origin);
345     }
346     if (NULL != allow_headers)
347     {
348       MHD_add_response_header (con_handle->response,
349                                "Access-Control-Allow-Headers",
350                                allow_headers);
351     }
352     //Always add JSONAPI content type. TODO
353     MHD_add_response_header (con_handle->response,
354                              MHD_HTTP_HEADER_CONTENT_TYPE,
355                              "application/vnd.api+json");
356     int ret = MHD_queue_response (con,
357                                   con_handle->status,
358                                   con_handle->response);
359     cleanup_handle (con_handle);
360     return ret;
361   }
362   return MHD_YES;
363 }
364
365
366 /* ******************** MHD HTTP setup and event loop ******************** */
367
368 /**
369  * Function called when MHD decides that we are done with a connection.
370  *
371  * @param cls NULL
372  * @param connection connection handle
373  * @param con_cls value as set by the last call to
374  *        the MHD_AccessHandlerCallback, should be our handle
375  * @param toe reason for request termination (ignored)
376  */
377 static void
378 mhd_completed_cb (void *cls,
379                   struct MHD_Connection *connection,
380                   void **con_cls,
381                   enum MHD_RequestTerminationCode toe)
382 {
383   if (MHD_REQUEST_TERMINATED_COMPLETED_OK != toe)
384     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
385                 "MHD encountered error handling request: %d\n",
386                 toe);
387 }
388
389
390 /**
391  * Kill the MHD daemon.
392  */
393 static void
394 kill_httpd ()
395 {
396   if (NULL != httpd)
397   {
398     MHD_stop_daemon (httpd);
399     httpd = NULL;
400   }
401   if (NULL != httpd_task)
402   {
403     GNUNET_SCHEDULER_cancel (httpd_task);
404     httpd_task = NULL;
405   }
406   if (NULL != ltask4)
407   {
408     GNUNET_SCHEDULER_cancel (ltask4);
409     ltask4 = NULL;
410   }
411   if (NULL != ltask6)
412   {
413     GNUNET_SCHEDULER_cancel (ltask6);
414     ltask6 = NULL;
415   }
416
417   if (NULL != lsock4)
418   {
419     GNUNET_NETWORK_socket_close (lsock4);
420     lsock4 = NULL;
421   }
422   if (NULL != lsock6)
423   {
424     GNUNET_NETWORK_socket_close (lsock6);
425     lsock6 = NULL;
426   }
427   }
428
429
430 /**
431  * Schedule MHD.  This function should be called initially when an
432  * MHD is first getting its client socket, and will then automatically
433  * always be called later whenever there is work to be done.
434  *
435  * @param hd the daemon to schedule
436  */
437 static void
438 schedule_httpd ()
439 {
440   fd_set rs;
441   fd_set ws;
442   fd_set es;
443   struct GNUNET_NETWORK_FDSet *wrs;
444   struct GNUNET_NETWORK_FDSet *wws;
445   int max;
446   int haveto;
447   MHD_UNSIGNED_LONG_LONG timeout;
448   struct GNUNET_TIME_Relative tv;
449
450   FD_ZERO (&rs);
451   FD_ZERO (&ws);
452   FD_ZERO (&es);
453   max = -1;
454   if (MHD_YES != MHD_get_fdset (httpd, &rs, &ws, &es, &max))
455   {
456     kill_httpd ();
457     return;
458   }
459   haveto = MHD_get_timeout (httpd, &timeout);
460   if (MHD_YES == haveto)
461     tv.rel_value_us = (uint64_t) timeout * 1000LL;
462   else
463     tv = GNUNET_TIME_UNIT_FOREVER_REL;
464   if (-1 != max)
465   {
466     wrs = GNUNET_NETWORK_fdset_create ();
467     wws = GNUNET_NETWORK_fdset_create ();
468     GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
469     GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
470   }
471   else
472   {
473     wrs = NULL;
474     wws = NULL;
475   }
476   if (NULL != httpd_task)
477   {
478     GNUNET_SCHEDULER_cancel (httpd_task);
479     httpd_task = NULL;
480   }
481   if ( (MHD_YES == haveto) ||
482        (-1 != max))
483   {
484     httpd_task =
485       GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
486                                    tv, wrs, wws,
487                                    &do_httpd, NULL);
488     
489   }
490   if (NULL != wrs)
491     GNUNET_NETWORK_fdset_destroy (wrs);
492   if (NULL != wws)
493     GNUNET_NETWORK_fdset_destroy (wws);
494 }
495
496 /**
497  * Task run whenever HTTP server operations are pending.
498  *
499  * @param cls NULL
500  */
501 static void
502 do_httpd (void *cls)
503 {
504   httpd_task = NULL;
505   MHD_run (httpd);
506   schedule_httpd ();
507 }
508
509
510 /**
511  * Accept new incoming connections
512  *
513  * @param cls the closure with the lsock4 or lsock6
514  * @param tc the scheduler context
515  */
516 static void
517 do_accept (void *cls)
518 {
519   struct GNUNET_NETWORK_Handle *lsock = cls;
520   struct GNUNET_NETWORK_Handle *s;
521   int fd;
522   const struct sockaddr *addr;
523   socklen_t len;
524
525   GNUNET_assert (NULL != lsock);
526   if (lsock == lsock4)
527   {
528     ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
529                                             lsock,
530                                             &do_accept, lsock);
531
532   }
533   else if (lsock == lsock6)
534   {
535     ltask6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
536                                             lsock,
537                                             &do_accept, lsock);
538
539   }
540   else
541     GNUNET_assert (0);
542   s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL);
543   if (NULL == s)
544   {
545     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "accept");
546     return;
547   }
548   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
549               "Got an inbound connection, waiting for data\n");
550   fd = GNUNET_NETWORK_get_fd (s);
551   addr = GNUNET_NETWORK_get_addr (s);
552   len = GNUNET_NETWORK_get_addrlen (s);
553   if (MHD_YES != MHD_add_connection (httpd, fd, addr, len))
554   {
555     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
556                 _("Failed to pass client to MHD\n"));
557     return;
558   }
559
560   schedule_httpd ();
561 }
562
563
564 /**
565  * Task run on shutdown
566  *
567  * @param cls closure
568  */
569 static void
570 do_shutdown (void *cls)
571 {
572   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
573               "Shutting down...\n");
574   kill_httpd ();
575   GNUNET_free_non_null (allow_origin);
576   GNUNET_free_non_null (allow_headers);
577 }
578
579
580 /**
581  * Create an IPv4 listen socket bound to our port.
582  *
583  * @return NULL on error
584  */
585 static struct GNUNET_NETWORK_Handle *
586 bind_v4 ()
587 {
588   struct GNUNET_NETWORK_Handle *ls;
589   struct sockaddr_in sa4;
590   int eno;
591
592   memset (&sa4, 0, sizeof (sa4));
593   sa4.sin_family = AF_INET;
594   sa4.sin_port = htons (port);
595 #if HAVE_SOCKADDR_IN_SIN_LEN
596   sa4.sin_len = sizeof (sa4);
597 #endif
598   ls = GNUNET_NETWORK_socket_create (AF_INET,
599                                      SOCK_STREAM,
600                                      0);
601   if (NULL == ls)
602     return NULL;
603   if (GNUNET_OK !=
604       GNUNET_NETWORK_socket_bind (ls, (const struct sockaddr *) &sa4,
605                                   sizeof (sa4)))
606   {
607     eno = errno;
608     GNUNET_NETWORK_socket_close (ls);
609     errno = eno;
610     return NULL;
611   }
612   return ls;
613 }
614
615
616 /**
617  * Create an IPv6 listen socket bound to our port.
618  *
619  * @return NULL on error
620  */
621 static struct GNUNET_NETWORK_Handle *
622 bind_v6 ()
623 {
624   struct GNUNET_NETWORK_Handle *ls;
625   struct sockaddr_in6 sa6;
626   int eno;
627
628   memset (&sa6, 0, sizeof (sa6));
629   sa6.sin6_family = AF_INET6;
630   sa6.sin6_port = htons (port);
631 #if HAVE_SOCKADDR_IN_SIN_LEN
632   sa6.sin6_len = sizeof (sa6);
633 #endif
634   ls = GNUNET_NETWORK_socket_create (AF_INET6,
635                                      SOCK_STREAM,
636                                      0);
637   if (NULL == ls)
638     return NULL;
639   if (GNUNET_OK !=
640       GNUNET_NETWORK_socket_bind (ls, (const struct sockaddr *) &sa6,
641                                   sizeof (sa6)))
642   {
643     eno = errno;
644     GNUNET_NETWORK_socket_close (ls);
645     errno = eno;
646     return NULL;
647   }
648   return ls;
649 }
650
651
652 /**
653  * Callback for plugin load
654  *
655  * @param cls NULL
656  * @param libname the name of the library loaded
657  * @param lib_ret the object returned by the plugin initializer
658  */
659 static void
660 load_plugin (void *cls,
661              const char *libname,
662              void *lib_ret)
663 {
664   struct GNUNET_REST_Plugin *plugin = lib_ret;
665   struct GNUNET_HashCode key;
666   if (NULL == lib_ret)
667   {
668     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
669                 "Could not load plugin `%s'\n",
670                 libname);
671     return;
672   }
673   GNUNET_assert (1 < strlen (plugin->name));
674   GNUNET_assert ('/' == *plugin->name);
675   GNUNET_CRYPTO_hash (plugin->name+1, strlen (plugin->name+1), &key);
676   if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (plugin_map,
677                                                       &key,
678                                                       plugin,
679                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
680   {
681     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
682                 "Could not load add plugin `%s'\n",
683                 libname);
684     return;
685   }
686   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
687               "Loaded plugin `%s'\n",
688               libname);
689 }
690
691
692 /**
693  * Main function that will be run
694  *
695  * @param cls closure
696  * @param args remaining command-line arguments
697  * @param cfgfile name of the configuration file used (for saving, can be NULL)
698  * @param c configuration
699  */
700 static void
701 run (void *cls,
702      char *const *args,
703      const char *cfgfile,
704      const struct GNUNET_CONFIGURATION_Handle *c)
705 {
706   cfg = c;
707   plugin_map = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
708
709   /* Get CORS data from cfg */
710   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "rest",
711                                                           "REST_ALLOW_ORIGIN",
712                                                           &allow_origin))
713   {
714     //No origin specified
715     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
716                 "No CORS Access-Control-Allow-Origin Header will be sent...\n");
717   }
718
719   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "rest",
720                                                           "REST_ALLOW_HEADERS",
721                                                           &allow_headers))
722   {
723     //No origin specified
724     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
725                 "No CORS Access-Control-Allow-Headers Header will be sent...\n");
726   }
727
728   /* Open listen socket proxy */
729   lsock6 = bind_v6 ();
730   if (NULL == lsock6)
731   {
732     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
733   }
734   else
735   {
736     if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock6, 5))
737     {
738       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
739       GNUNET_NETWORK_socket_close (lsock6);
740       lsock6 = NULL;
741     }
742     else
743     {
744       ltask6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
745                                               lsock6, &do_accept, lsock6);
746
747     }
748   }
749   lsock4 = bind_v4 ();
750   if (NULL == lsock4)
751   {
752     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
753   }
754   else
755   {
756     if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock4, 5))
757     {
758       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
759       GNUNET_NETWORK_socket_close (lsock4);
760       lsock4 = NULL;
761     }
762     else
763     {
764       ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
765                                               lsock4, &do_accept, lsock4);
766
767     }
768   }
769   if ( (NULL == lsock4) &&
770        (NULL == lsock6) )
771   {
772     GNUNET_SCHEDULER_shutdown ();
773     return;
774   }
775   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
776               "Service listens on port %llu\n",
777               port);
778   httpd = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_NO_LISTEN_SOCKET,
779                             0,
780                             NULL, NULL,
781                             &create_response, NULL,
782                             MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
783                             MHD_OPTION_NOTIFY_COMPLETED, &mhd_completed_cb, NULL,
784                             MHD_OPTION_END);
785   if (NULL == httpd)
786   {
787     GNUNET_SCHEDULER_shutdown ();
788     return;
789   }
790   /* Load plugins */
791   GNUNET_PLUGIN_load_all ("libgnunet_plugin_rest",
792                           (void *) cfg,
793                           &load_plugin,
794                           NULL);
795   GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
796 }
797
798
799 /**
800  *
801  * The main function for gnunet-rest-service
802  *
803  * @param argc number of arguments from the cli
804  * @param argv command line arguments
805  * @return 0 ok, 1 on error
806  *
807  */
808 int
809 main (int argc, char *const *argv)
810 {
811   struct GNUNET_GETOPT_CommandLineOption options[] = {
812     GNUNET_GETOPT_option_ulong ('p',
813                                 "port",
814                                 "PORT",
815                                 gettext_noop ("listen on specified port (default: 7776)"),
816                                 &port),
817     GNUNET_GETOPT_OPTION_END
818   };
819   static const char* err_page =
820     "{}";
821   int ret;
822
823   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
824     return 2;
825   GNUNET_log_setup ("gnunet-rest-server", "WARNING", NULL);
826   failure_response = MHD_create_response_from_buffer (strlen(err_page),
827                                                       (void*)err_page,
828                                                       MHD_RESPMEM_PERSISTENT);
829   ret =
830     (GNUNET_OK ==
831      GNUNET_PROGRAM_run (argc, argv, "gnunet-rest-server",
832                          _("GNUnet REST server"),
833                          options,
834                          &run, NULL)) ? 0: 1;
835   MHD_destroy_response (failure_response);
836   GNUNET_free_non_null ((char *) argv);
837   return ret;
838 }
839
840 /* end of gnunet-rest-server.c */