2 This file is part of GNUnet
3 Copyright (C) 2010-2015 GNUnet e.V.
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.
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.
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/>.
21 * @brief helper library to create JSON REST Objects and handle REST
23 * @author Martin Schanzenbach
26 #include "gnunet_util_lib.h"
27 #include "gnunet_rest_lib.h"
28 #include "microhttpd.h"
35 * Check if namespace is in URL.
37 * @param url URL to check
38 * @param namespace namespace to check against
39 * @retun GNUNET_YES if namespace matches
42 GNUNET_REST_namespace_match (const char *url, const char *namespace)
44 return 0 == strncmp (namespace, url, strlen (namespace));
54 GNUNET_REST_create_response (const char *data)
56 struct MHD_Response *resp;
66 resp = MHD_create_response_from_buffer (len,
68 MHD_RESPMEM_MUST_COPY);
74 GNUNET_REST_handle_request (struct GNUNET_REST_RequestHandle *conn,
75 const struct GNUNET_REST_RequestHandler *handlers,
76 struct GNUNET_REST_RequestHandlerError *err,
84 while (NULL != handlers[count].method)
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++)
92 if (0 != strcasecmp (conn->method, handlers[i].method))
94 if (strlen (url) < strlen (handlers[i].namespace))
96 if (GNUNET_NO == GNUNET_REST_namespace_match (url, handlers[i].namespace))
99 handlers[i].proc (conn, (const char*)url, cls);
104 err->error_code = MHD_HTTP_BAD_REQUEST;