src: for every AGPL3.0 file, add SPDX identifier.
[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      SPDX-License-Identifier: AGPL3.0-or-later
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
32 /**
33  * REST Utilities
34  */
35
36   /**
37    * Check if namespace is in URL.
38    *
39    * @param url URL to check
40    * @param namespace namespace to check against
41    * @retun GNUNET_YES if namespace matches
42    */
43 int
44 GNUNET_REST_namespace_match (const char *url, const char *namespace)
45 {
46   return 0 == strncmp (namespace, url, strlen (namespace));
47 }
48
49 /**
50  * Create MHD response
51  *
52  * @param data result
53  * @retun MHD response
54  */
55 struct MHD_Response*
56 GNUNET_REST_create_response (const char *data)
57 {
58   struct MHD_Response *resp;
59   size_t len;
60
61   if (NULL == data)
62   {
63     len = 0;
64     data = "";
65   }
66   else
67     len = strlen (data);
68   resp = MHD_create_response_from_buffer (len,
69                                           (void*)data,
70                                           MHD_RESPMEM_MUST_COPY);
71   return resp;
72
73 }
74
75 int
76 GNUNET_REST_handle_request (struct GNUNET_REST_RequestHandle *conn,
77                             const struct GNUNET_REST_RequestHandler *handlers,
78                             struct GNUNET_REST_RequestHandlerError *err,
79                             void *cls)
80 {
81   int count;
82   int i;
83   char *url;
84
85   count = 0;
86   while (NULL != handlers[count].method)
87     count++;
88
89   GNUNET_asprintf (&url, "%s", conn->url);
90   if (url[strlen (url)-1] == '/')
91     url[strlen (url)-1] = '\0';
92   for (i = 0; i < count; i++)
93   {
94     if (0 != strcasecmp (conn->method, handlers[i].method))
95       continue;
96     if (strlen (url) < strlen (handlers[i].namespace))
97       continue;
98     if (GNUNET_NO == GNUNET_REST_namespace_match (url, handlers[i].namespace))
99       continue;
100     //Match
101     handlers[i].proc (conn, (const char*)url, cls);
102     GNUNET_free (url);
103     return GNUNET_YES;
104   }
105   GNUNET_free (url);
106   err->error_code = MHD_HTTP_BAD_REQUEST;
107   return GNUNET_NO;
108 }
109
110 /* end of rest.c */