d6f5de8952ec01692371b3109e2c51095008d5ff
[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 it
6       under the terms of the GNU Affero General Public License as published
7       by the Free Software Foundation, either version 3 of the License,
8       or (at your 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       Affero General Public License for more details.
14      
15       You should have received a copy of the GNU Affero General Public License
16       along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 /**
20  * @file rest/rest.c
21  * @brief helper library to create JSON REST Objects and handle REST
22  * responses/requests.
23  * @author Martin Schanzenbach
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_rest_lib.h"
28 #include "microhttpd.h"
29
30 /**
31  * REST Utilities
32  */
33
34   /**
35    * Check if namespace is in URL.
36    *
37    * @param url URL to check
38    * @param namespace namespace to check against
39    * @retun GNUNET_YES if namespace matches
40    */
41 int
42 GNUNET_REST_namespace_match (const char *url, const char *namespace)
43 {
44   return 0 == strncmp (namespace, url, strlen (namespace));
45 }
46
47 /**
48  * Create MHD response
49  *
50  * @param data result
51  * @retun MHD response
52  */
53 struct MHD_Response*
54 GNUNET_REST_create_response (const char *data)
55 {
56   struct MHD_Response *resp;
57   size_t len;
58
59   if (NULL == data)
60   {
61     len = 0;
62     data = "";
63   }
64   else
65     len = strlen (data);
66   resp = MHD_create_response_from_buffer (len,
67                                           (void*)data,
68                                           MHD_RESPMEM_MUST_COPY);
69   return resp;
70
71 }
72
73 int
74 GNUNET_REST_handle_request (struct GNUNET_REST_RequestHandle *conn,
75                             const struct GNUNET_REST_RequestHandler *handlers,
76                             struct GNUNET_REST_RequestHandlerError *err,
77                             void *cls)
78 {
79   int count;
80   int i;
81   char *url;
82
83   count = 0;
84   while (NULL != handlers[count].method)
85     count++;
86
87   GNUNET_asprintf (&url, "%s", conn->url);
88   if (url[strlen (url)-1] == '/')
89     url[strlen (url)-1] = '\0';
90   for (i = 0; i < count; i++)
91   {
92     if (0 != strcasecmp (conn->method, handlers[i].method))
93       continue;
94     if (strlen (url) < strlen (handlers[i].namespace))
95       continue;
96     if (GNUNET_NO == GNUNET_REST_namespace_match (url, handlers[i].namespace))
97       continue;
98     //Match
99     handlers[i].proc (conn, (const char*)url, cls);
100     GNUNET_free (url);
101     return GNUNET_YES;
102   }
103   GNUNET_free (url);
104   err->error_code = MHD_HTTP_BAD_REQUEST;
105   return GNUNET_NO;
106 }
107
108 /* end of rest.c */