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
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.
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.
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.
23 * @brief helper library to create JSON REST Objects and handle REST
25 * @author Martin Schanzenbach
28 #include "gnunet_util_lib.h"
29 #include "gnunet_rest_lib.h"
30 #include "microhttpd.h"
38 * Check if namespace is in URL.
40 * @param url URL to check
41 * @param namespace namespace to check against
42 * @retun GNUNET_YES if namespace matches
45 GNUNET_REST_namespace_match (const char *url, const char *namespace)
47 return 0 == strncmp (namespace, url, strlen (namespace));
51 * Create JSON API MHD response
53 * @param data JSON result
57 GNUNET_REST_create_json_response (const char *data)
59 struct MHD_Response *resp;
69 resp = MHD_create_response_from_buffer (len,
71 MHD_RESPMEM_MUST_COPY);
72 MHD_add_response_header (resp,MHD_HTTP_HEADER_CONTENT_TYPE,"application/json");
78 GNUNET_REST_handle_request (struct RestConnectionDataHandle *conn,
79 const struct GNUNET_REST_RestConnectionHandler *handlers,
87 while (NULL != handlers[count].method)
90 GNUNET_asprintf (&url, "%s", conn->url);
91 if (url[strlen (url)-1] == '/')
92 url[strlen (url)-1] = '\0';
93 for (i = 0; i < count; i++)
95 if (0 != strcasecmp (conn->method, handlers[i].method))
97 if (strlen (url) < strlen (handlers[i].namespace))
99 if (GNUNET_NO == GNUNET_REST_namespace_match (url, handlers[i].namespace))
102 handlers[i].proc (conn, (const char*)url, cls);