2 This file is part of GNUnet.
3 Copyright (C) 2012-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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @author Philippe Buschmann
22 * @file gns/plugin_rest_gns.c
23 * @brief GNUnet Gns REST plugin
27 #include "gnunet_rest_plugin.h"
28 #include "gnunet_rest_lib.h"
29 #include "gnunet_json_lib.h"
30 #include "gnunet_gnsrecord_lib.h"
31 #include "gnunet_gns_service.h"
32 #include "microhttpd.h"
36 * Rest API GNS Namespace
38 #define GNUNET_REST_API_NS_GNS "/gns"
41 * Rest API GNS Parameter record_type
43 #define GNUNET_REST_GNS_PARAM_RECORD_TYPE "record_type"
46 * Rest API GNS ERROR Unknown Error
48 #define GNUNET_REST_GNS_ERROR_UNKNOWN "Unknown Error"
51 * Rest API GNS ERROR Record not found
53 #define GNUNET_REST_GNS_NOT_FOUND "Record not found"
56 * The configuration handle
58 const struct GNUNET_CONFIGURATION_Handle *cfg;
61 * HTTP methods allows for this plugin
63 static char *allow_methods;
66 * @brief struct returned by the initialization function of the plugin
70 const struct GNUNET_CONFIGURATION_Handle *cfg;
81 struct GNUNET_GNS_Handle *gns;
86 struct GNUNET_GNS_LookupWithTldRequest *gns_lookup;
94 * Record type to look up
101 struct GNUNET_REST_RequestHandle *rest_handle;
104 * Desired timeout for the lookup (default is no timeout).
106 struct GNUNET_TIME_Relative timeout;
109 * ID of a task associated with the resolution process.
111 struct GNUNET_SCHEDULER_Task *timeout_task;
114 * The plugin result processor
116 GNUNET_REST_ResultProcessor proc;
119 * The closure of the result processor
129 * Error response message
141 * Cleanup lookup handle
142 * @param handle Handle to clean up
145 cleanup_handle (void *cls)
147 struct RequestHandle *handle = cls;
149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
151 if (NULL != handle->gns_lookup)
153 GNUNET_GNS_lookup_with_tld_cancel (handle->gns_lookup);
154 handle->gns_lookup = NULL;
156 if (NULL != handle->gns)
158 GNUNET_GNS_disconnect (handle->gns);
162 if (NULL != handle->timeout_task)
164 GNUNET_SCHEDULER_cancel (handle->timeout_task);
165 handle->timeout_task = NULL;
167 if (NULL != handle->url)
168 GNUNET_free (handle->url);
169 if (NULL != handle->name)
170 GNUNET_free (handle->name);
171 if (NULL != handle->emsg)
172 GNUNET_free (handle->emsg);
174 GNUNET_free (handle);
179 * Task run on errors. Reports an error and cleans up everything.
181 * @param cls the `struct RequestHandle`
186 struct RequestHandle *handle = cls;
187 struct MHD_Response *resp;
188 json_t *json_error = json_object ();
191 if (NULL != handle->timeout_task)
192 GNUNET_SCHEDULER_cancel (handle->timeout_task);
193 handle->timeout_task = NULL;
194 if (NULL == handle->emsg)
195 handle->emsg = GNUNET_strdup (GNUNET_REST_GNS_ERROR_UNKNOWN);
197 json_object_set_new (json_error, "error", json_string (handle->emsg));
199 if (0 == handle->response_code)
200 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
201 response = json_dumps (json_error, 0);
202 resp = GNUNET_REST_create_response (response);
203 handle->proc (handle->proc_cls, resp, handle->response_code);
204 json_decref (json_error);
205 GNUNET_free (response);
206 GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
211 do_timeout (void *cls)
213 struct RequestHandle *handle = cls;
215 handle->timeout_task = NULL;
216 handle->response_code = MHD_HTTP_REQUEST_TIMEOUT;
222 * Iterator called on obtained result for a GNS lookup.
224 * @param cls closure with the object
225 * @param was_gns #GNUNET_NO if name was not a GNS name
226 * @param rd_count number of records in @a rd
227 * @param rd the records in reply
230 handle_gns_response (void *cls,
233 const struct GNUNET_GNSRECORD_Data *rd)
235 struct RequestHandle *handle = cls;
236 struct MHD_Response *resp;
240 handle->gns_lookup = NULL;
242 if (GNUNET_NO == was_gns)
244 handle->response_code = MHD_HTTP_NOT_FOUND;
245 handle->emsg = GNUNET_strdup (GNUNET_REST_GNS_NOT_FOUND);
246 GNUNET_SCHEDULER_add_now (&do_error, handle);
250 result_obj = GNUNET_JSON_from_gnsrecord (handle->name, rd, rd_count);
252 result = json_dumps (result_obj, 0);
253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result);
254 resp = GNUNET_REST_create_response (result);
255 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
256 GNUNET_free (result);
257 json_decref (result_obj);
258 GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
263 * Handle gns GET request
265 * @param con_handle the connection handle
267 * @param cls the RequestHandle
270 get_gns_cont (struct GNUNET_REST_RequestHandle *con_handle,
274 struct RequestHandle *handle = cls;
275 struct GNUNET_HashCode key;
281 if (strlen (GNUNET_REST_API_NS_GNS) < strlen (handle->url))
283 name = &handle->url[strlen (GNUNET_REST_API_NS_GNS) + 1];
288 handle->response_code = MHD_HTTP_NOT_FOUND;
289 handle->emsg = GNUNET_strdup (GNUNET_REST_GNS_NOT_FOUND);
290 GNUNET_SCHEDULER_add_now (&do_error, handle);
293 if (0 >= strlen (name))
295 handle->response_code = MHD_HTTP_NOT_FOUND;
296 handle->emsg = GNUNET_strdup (GNUNET_REST_GNS_NOT_FOUND);
297 GNUNET_SCHEDULER_add_now (&do_error, handle);
300 handle->name = GNUNET_strdup (name);
302 handle->record_type = UINT32_MAX;
303 GNUNET_CRYPTO_hash (GNUNET_REST_GNS_PARAM_RECORD_TYPE,
304 strlen (GNUNET_REST_GNS_PARAM_RECORD_TYPE),
307 GNUNET_CONTAINER_multihashmap_contains (con_handle->url_param_map, &key))
310 GNUNET_CONTAINER_multihashmap_get (con_handle->url_param_map, &key);
311 handle->record_type = GNUNET_GNSRECORD_typename_to_number (record_type);
314 if (UINT32_MAX == handle->record_type)
316 handle->record_type = GNUNET_GNSRECORD_TYPE_ANY;
319 handle->gns_lookup = GNUNET_GNS_lookup_with_tld (handle->gns,
323 &handle_gns_response,
329 * Respond to OPTIONS request
331 * @param con_handle the connection handle
333 * @param cls the RequestHandle
336 options_cont (struct GNUNET_REST_RequestHandle *con_handle,
340 struct MHD_Response *resp;
341 struct RequestHandle *handle = cls;
343 // independent of path return all options
344 resp = GNUNET_REST_create_response (NULL);
345 MHD_add_response_header (resp, "Access-Control-Allow-Methods", allow_methods);
346 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
347 GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
353 * Handle rest request
355 * @param handle the request handle
358 init_cont (struct RequestHandle *handle)
360 struct GNUNET_REST_RequestHandlerError err;
361 static const struct GNUNET_REST_RequestHandler handlers[] =
362 { { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_GNS, &get_gns_cont },
363 { MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_GNS, &options_cont },
364 GNUNET_REST_HANDLER_END };
367 GNUNET_REST_handle_request (handle->rest_handle, handlers, &err, handle))
369 handle->response_code = err.error_code;
370 GNUNET_SCHEDULER_add_now (&do_error, handle);
376 * Function processing the REST call
378 * @param method HTTP method
379 * @param url URL of the HTTP request
380 * @param data body of the HTTP request (optional)
381 * @param data_size length of the body
382 * @param proc callback function for the result
383 * @param proc_cls closure for callback function
384 * @return GNUNET_OK if request accepted
387 rest_process_request (struct GNUNET_REST_RequestHandle *rest_handle,
388 GNUNET_REST_ResultProcessor proc,
391 struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
393 handle->response_code = 0;
395 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60);
396 handle->proc_cls = proc_cls;
398 handle->rest_handle = rest_handle;
400 handle->url = GNUNET_strdup (rest_handle->url);
401 if (handle->url[strlen (handle->url) - 1] == '/')
402 handle->url[strlen (handle->url) - 1] = '\0';
403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting...\n");
404 handle->gns = GNUNET_GNS_connect (cfg);
407 handle->timeout_task =
408 GNUNET_SCHEDULER_add_delayed (handle->timeout, &do_timeout, handle);
410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected\n");
415 * Entry point for the plugin.
417 * @param cls Config info
418 * @return NULL on error, otherwise the plugin context
421 libgnunet_plugin_rest_gns_init (void *cls)
423 static struct Plugin plugin;
424 struct GNUNET_REST_Plugin *api;
427 if (NULL != plugin.cfg)
428 return NULL; /* can only initialize once! */
429 memset (&plugin, 0, sizeof(struct Plugin));
431 api = GNUNET_new (struct GNUNET_REST_Plugin);
433 api->name = GNUNET_REST_API_NS_GNS;
434 api->process_request = &rest_process_request;
435 GNUNET_asprintf (&allow_methods,
436 "%s, %s, %s, %s, %s",
438 MHD_HTTP_METHOD_POST,
440 MHD_HTTP_METHOD_DELETE,
441 MHD_HTTP_METHOD_OPTIONS);
443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _ ("Gns REST API initialized\n"));
449 * Exit point from the plugin.
451 * @param cls the plugin context (as returned by "init")
452 * @return always NULL
455 libgnunet_plugin_rest_gns_done (void *cls)
457 struct GNUNET_REST_Plugin *api = cls;
458 struct Plugin *plugin = api->cls;
462 GNUNET_free_non_null (allow_methods);
464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Gns REST plugin is finished\n");
469 /* end of plugin_rest_gns.c */