- build rest before jsonapi
[oweals/gnunet.git] / src / rest / rest.c
1 /*
2       This file is part of GNUnet
3       Copyright (C) 2010-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 /**
22  * @file rest/rest.c
23  * @brief helper library to create JSON REST Objects and handle REST
24  * responses/requests.
25  * @author Martin Schanzenbach
26  */
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_rest_lib.h"
30 #include "microhttpd.h"
31 #include <jansson.h>
32
33 /**
34  * REST Utilities
35  */
36
37   /**
38    * Check if namespace is in URL.
39    *
40    * @param url URL to check
41    * @param namespace namespace to check against
42    * @retun GNUNET_YES if namespace matches
43    */
44 int
45 GNUNET_REST_namespace_match (const char *url, const char *namespace)
46 {
47   return 0 == strncmp (namespace, url, strlen (namespace));
48 }
49
50 /**
51  * Create JSON API MHD response
52  *
53  * @param data JSON result
54  * @retun MHD response
55  */
56 struct MHD_Response*
57 GNUNET_REST_create_json_response (const char *data)
58 {
59   struct MHD_Response *resp;
60   size_t len;
61
62   if (NULL == data)
63   {
64     len = 0;
65     data = "";
66   }
67   else
68     len = strlen (data);
69   resp = MHD_create_response_from_buffer (len,
70                                           (void*)data,
71                                           MHD_RESPMEM_MUST_COPY);
72   MHD_add_response_header (resp,MHD_HTTP_HEADER_CONTENT_TYPE,"application/json");
73   return resp;
74
75 }
76
77 int
78 GNUNET_REST_handle_request (struct GNUNET_REST_RequestHandle *conn,
79                             const struct GNUNET_REST_RequestHandler *handlers,
80                             struct GNUNET_REST_RequestHandlerError *err,
81                             void *cls)
82 {
83   int count;
84   int i;
85   char *url;
86
87   count = 0;
88   while (NULL != handlers[count].method)
89     count++;
90
91   GNUNET_asprintf (&url, "%s", conn->url);
92   if (url[strlen (url)-1] == '/')
93     url[strlen (url)-1] = '\0';
94   for (i = 0; i < count; i++)
95   {
96     if (0 != strcasecmp (conn->method, handlers[i].method))
97       continue;
98     if (strlen (url) < strlen (handlers[i].namespace))
99       continue;
100     if (GNUNET_NO == GNUNET_REST_namespace_match (url, handlers[i].namespace))
101       continue;
102     //Match
103     handlers[i].proc (conn, (const char*)url, cls);
104     GNUNET_free (url);
105     return GNUNET_YES;
106   }
107   GNUNET_free (url);
108   err->error_code = MHD_HTTP_BAD_REQUEST;
109   return GNUNET_NO;
110 }
111
112 /* end of rest.c */