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 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.
18 * @brief helper library to create JSON REST Objects and handle REST
20 * @author Martin Schanzenbach
23 #include "gnunet_util_lib.h"
24 #include "gnunet_rest_lib.h"
25 #include "microhttpd.h"
32 * Check if namespace is in URL.
34 * @param url URL to check
35 * @param namespace namespace to check against
36 * @retun GNUNET_YES if namespace matches
39 GNUNET_REST_namespace_match (const char *url, const char *namespace)
41 return 0 == strncmp (namespace, url, strlen (namespace));
51 GNUNET_REST_create_response (const char *data)
53 struct MHD_Response *resp;
63 resp = MHD_create_response_from_buffer (len,
65 MHD_RESPMEM_MUST_COPY);
71 GNUNET_REST_handle_request (struct GNUNET_REST_RequestHandle *conn,
72 const struct GNUNET_REST_RequestHandler *handlers,
73 struct GNUNET_REST_RequestHandlerError *err,
81 while (NULL != handlers[count].method)
84 GNUNET_asprintf (&url, "%s", conn->url);
85 if (url[strlen (url)-1] == '/')
86 url[strlen (url)-1] = '\0';
87 for (i = 0; i < count; i++)
89 if (0 != strcasecmp (conn->method, handlers[i].method))
91 if (strlen (url) < strlen (handlers[i].namespace))
93 if (GNUNET_NO == GNUNET_REST_namespace_match (url, handlers[i].namespace))
96 handlers[i].proc (conn, (const char*)url, cls);
101 err->error_code = MHD_HTTP_BAD_REQUEST;