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 Martin Schanzenbach
22 * @author Philippe Buschmann
23 * @file namestore/plugin_rest_namestore.c
24 * @brief GNUnet Namestore REST plugin
28 #include "gnunet_rest_plugin.h"
29 #include "gnunet_gns_service.h"
30 #include "gnunet_namestore_service.h"
31 #include "gnunet_identity_service.h"
32 #include "gnunet_rest_lib.h"
33 #include "gnunet_json_lib.h"
34 #include "microhttpd.h"
40 #define GNUNET_REST_API_NS_NAMESTORE "/namestore"
43 * Error message Unknown Error
45 #define GNUNET_REST_NAMESTORE_ERROR_UNKNOWN "Unknown Error"
48 * Error message No identity found
50 #define GNUNET_REST_IDENTITY_NOT_FOUND "No identity found"
54 * Error message Failed request
56 #define GNUNET_REST_NAMESTORE_FAILED "Namestore action failed"
59 * Error message invalid data
61 #define GNUNET_REST_NAMESTORE_INVALID_DATA "Data invalid"
64 * Error message No data
66 #define GNUNET_REST_NAMESTORE_NO_DATA "No data"
69 * State while collecting all egos
71 #define ID_REST_STATE_INIT 0
74 * Done collecting egos
76 #define ID_REST_STATE_POST_INIT 1
78 * The configuration handle
80 const struct GNUNET_CONFIGURATION_Handle *cfg;
83 * HTTP methods allows for this plugin
85 static char *allow_methods;
88 * @brief struct returned by the initialization function of the plugin
92 const struct GNUNET_CONFIGURATION_Handle *cfg;
96 * The default namestore ego
103 struct EgoEntry *next;
108 struct EgoEntry *prev;
123 struct GNUNET_IDENTITY_Ego *ego;
129 UPDATE_STRATEGY_REPLACE,
130 UPDATE_STRATEGY_APPEND
146 uint32_t record_type;
149 * How to update the record set
151 enum UpdateStrategy update_strategy;
156 struct GNUNET_GNSRECORD_Data *rd;
159 * Number of records in rd
161 unsigned int rd_count;
164 * NAMESTORE Operation
166 struct GNUNET_NAMESTORE_QueueEntry *add_qe;
174 * The processing state
179 * Handle to NAMESTORE
181 struct GNUNET_NAMESTORE_Handle *ns_handle;
184 * Handle to NAMESTORE it
186 struct GNUNET_NAMESTORE_ZoneIterator *list_it;
189 * Private key for the zone
191 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_pkey;
196 struct EgoEntry *ego_entry;
201 struct EgoEntry *ego_head;
206 struct EgoEntry *ego_tail;
211 struct GNUNET_IDENTITY_Operation *op;
214 * Handle to Identity service.
216 struct GNUNET_IDENTITY_Handle *identity_handle;
221 struct GNUNET_REST_RequestHandle *rest_handle;
224 * Desired timeout for the lookup (default is no timeout).
226 struct GNUNET_TIME_Relative timeout;
229 * ID of a task associated with the resolution process.
231 struct GNUNET_SCHEDULER_Task *timeout_task;
234 * The plugin result processor
236 GNUNET_REST_ResultProcessor proc;
239 * The closure of the result processor
249 * Error response message
260 * Cleanup lookup handle
261 * @param handle Handle to clean up
264 cleanup_handle (void *cls)
266 struct RequestHandle *handle = cls;
267 struct EgoEntry *ego_entry;
268 struct EgoEntry *ego_tmp;
270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
271 if (NULL != handle->timeout_task)
273 GNUNET_SCHEDULER_cancel (handle->timeout_task);
274 handle->timeout_task = NULL;
276 if (NULL != handle->record_name)
277 GNUNET_free (handle->record_name);
278 if (NULL != handle->url)
279 GNUNET_free (handle->url);
280 if (NULL != handle->emsg)
281 GNUNET_free (handle->emsg);
282 if (NULL != handle->rd)
284 for (int i = 0; i < handle->rd_count; i++)
286 if (NULL != handle->rd[i].data)
287 GNUNET_free_nz ((void *) handle->rd[i].data);
289 GNUNET_free (handle->rd);
291 if (NULL != handle->timeout_task)
292 GNUNET_SCHEDULER_cancel (handle->timeout_task);
293 if (NULL != handle->list_it)
294 GNUNET_NAMESTORE_zone_iteration_stop (handle->list_it);
295 if (NULL != handle->add_qe)
296 GNUNET_NAMESTORE_cancel (handle->add_qe);
297 if (NULL != handle->identity_handle)
298 GNUNET_IDENTITY_disconnect (handle->identity_handle);
299 if (NULL != handle->ns_handle)
301 GNUNET_NAMESTORE_disconnect (handle->ns_handle);
304 for (ego_entry = handle->ego_head; NULL != ego_entry;)
307 ego_entry = ego_entry->next;
308 GNUNET_free (ego_tmp->identifier);
309 GNUNET_free (ego_tmp->keystring);
310 GNUNET_free (ego_tmp);
313 if (NULL != handle->resp_object)
315 json_decref (handle->resp_object);
318 GNUNET_free (handle);
323 * Task run on errors. Reports an error and cleans up everything.
325 * @param cls the `struct RequestHandle`
330 struct RequestHandle *handle = cls;
331 struct MHD_Response *resp;
332 json_t *json_error = json_object ();
335 if (NULL == handle->emsg)
336 handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_ERROR_UNKNOWN);
338 json_object_set_new (json_error, "error", json_string (handle->emsg));
340 if (0 == handle->response_code)
341 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
342 response = json_dumps (json_error, 0);
343 resp = GNUNET_REST_create_response (response);
344 MHD_add_response_header (resp, "Content-Type", "application/json");
345 handle->proc (handle->proc_cls, resp, handle->response_code);
346 json_decref (json_error);
347 GNUNET_free (response);
348 GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
353 * Get EgoEntry from list with either a public key or a name
354 * If public key and name are not NULL, it returns the public key result first
356 * @param handle the RequestHandle
357 * @param pubkey the public key of an identity (only one can be NULL)
358 * @param name the name of an identity (only one can be NULL)
359 * @return EgoEntry or NULL if not found
362 get_egoentry_namestore (struct RequestHandle *handle, char *name)
364 struct EgoEntry *ego_entry;
365 char *copy = GNUNET_strdup (name);
370 tmp = strtok (copy, "/");
371 for (ego_entry = handle->ego_head; NULL != ego_entry;
372 ego_entry = ego_entry->next)
374 if (0 != strcasecmp (tmp, ego_entry->identifier))
385 * Does internal server error when iteration failed.
387 * @param cls the `struct RequestHandle`
390 namestore_iteration_error (void *cls)
392 struct RequestHandle *handle = cls;
394 handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_FAILED);
395 GNUNET_SCHEDULER_add_now (&do_error, handle);
401 * Create finished callback
403 * @param cls the `struct RequestHandle`
404 * @param success the success indicating integer, GNUNET_OK on success
405 * @param emsg the error message (can be NULL)
408 create_finished (void *cls, int32_t success, const char *emsg)
410 struct RequestHandle *handle = cls;
411 struct MHD_Response *resp;
413 handle->add_qe = NULL;
414 if (GNUNET_YES != success)
418 handle->emsg = GNUNET_strdup (emsg);
419 GNUNET_SCHEDULER_add_now (&do_error, handle);
422 handle->emsg = GNUNET_strdup ("Error storing records");
423 GNUNET_SCHEDULER_add_now (&do_error, handle);
426 resp = GNUNET_REST_create_response (NULL);
427 handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT);
428 GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
433 * Delete finished callback
435 * @param cls the `struct RequestHandle`
436 * @param success the success indicating integer, GNUNET_OK on success
437 * @param emsg the error message (can be NULL)
440 del_finished (void *cls, int32_t success, const char *emsg)
442 struct RequestHandle *handle = cls;
444 handle->add_qe = NULL;
445 if (GNUNET_NO == success)
447 handle->response_code = MHD_HTTP_NOT_FOUND;
448 handle->emsg = GNUNET_strdup ("No record found");
449 GNUNET_SCHEDULER_add_now (&do_error, handle);
452 if (GNUNET_SYSERR == success)
456 handle->emsg = GNUNET_strdup (emsg);
457 GNUNET_SCHEDULER_add_now (&do_error, handle);
460 handle->emsg = GNUNET_strdup ("Deleting record failed");
461 GNUNET_SCHEDULER_add_now (&do_error, handle);
464 handle->proc (handle->proc_cls,
465 GNUNET_REST_create_response (NULL),
466 MHD_HTTP_NO_CONTENT);
467 GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
472 * Iteration over all results finished, build final
475 * @param cls the `struct RequestHandle`
478 namestore_list_finished (void *cls)
480 struct RequestHandle *handle = cls;
482 struct MHD_Response *resp;
484 handle->list_it = NULL;
486 if (NULL == handle->resp_object)
487 handle->resp_object = json_array ();
489 result_str = json_dumps (handle->resp_object, 0);
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
491 resp = GNUNET_REST_create_response (result_str);
492 MHD_add_response_header (resp, "Content-Type", "application/json");
493 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
494 GNUNET_free_non_null (result_str);
495 GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
500 * Create a response with requested records
502 * @param handle the RequestHandle
505 namestore_list_iteration (void *cls,
506 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
509 const struct GNUNET_GNSRECORD_Data *rd)
511 struct RequestHandle *handle = cls;
512 struct GNUNET_GNSRECORD_Data rd_filtered[rd_len];
517 if (NULL == handle->resp_object)
518 handle->resp_object = json_array ();
519 for (i = 0; i < rd_len; i++)
521 if ((GNUNET_GNSRECORD_TYPE_ANY != handle->record_type) &&
522 (rd[i].record_type != handle->record_type))
523 continue; /* Apply filter */
524 rd_filtered[j] = rd[i];
525 rd_filtered[j].data = rd[i].data;
528 /** Only add if not empty **/
531 record_obj = GNUNET_JSON_from_gnsrecord (rname,
534 json_array_append_new (handle->resp_object, record_obj);
536 GNUNET_NAMESTORE_zone_iterator_next (handle->list_it, 1);
541 * Handle namestore GET request
543 * @param con_handle the connection handle
545 * @param cls the RequestHandle
548 namestore_get (struct GNUNET_REST_RequestHandle *con_handle,
552 struct RequestHandle *handle = cls;
553 struct EgoEntry *ego_entry;
554 struct GNUNET_HashCode key;
561 // set zone to name if given
562 if (strlen (GNUNET_REST_API_NS_NAMESTORE) + 1 >= strlen (handle->url))
564 handle->response_code = MHD_HTTP_NOT_FOUND;
565 handle->emsg = GNUNET_strdup (GNUNET_REST_IDENTITY_NOT_FOUND);
566 GNUNET_SCHEDULER_add_now (&do_error, handle);
569 egoname = &handle->url[strlen (GNUNET_REST_API_NS_NAMESTORE) + 1];
570 ego_entry = get_egoentry_namestore (handle, egoname);
571 if (NULL == ego_entry)
573 handle->response_code = MHD_HTTP_NOT_FOUND;
574 handle->emsg = GNUNET_strdup (GNUNET_REST_IDENTITY_NOT_FOUND);
575 GNUNET_SCHEDULER_add_now (&do_error, handle);
578 handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego);
580 GNUNET_CRYPTO_hash ("record_type", strlen ("record_type"), &key);
582 GNUNET_CONTAINER_multihashmap_contains (con_handle->url_param_map, &key))
584 handle->record_type = GNUNET_GNSRECORD_TYPE_ANY;
588 typename = GNUNET_CONTAINER_multihashmap_get (con_handle->url_param_map,
590 handle->record_type = GNUNET_GNSRECORD_typename_to_number (typename);
594 GNUNET_NAMESTORE_zone_iteration_start (handle->ns_handle,
596 &namestore_iteration_error,
598 &namestore_list_iteration,
600 &namestore_list_finished,
602 if (NULL == handle->list_it)
604 handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_FAILED);
605 GNUNET_SCHEDULER_add_now (&do_error, handle);
612 ns_lookup_error_cb (void *cls)
614 struct RequestHandle *handle = cls;
616 handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_FAILED);
617 GNUNET_SCHEDULER_add_now (&do_error, handle);
622 ns_lookup_cb (void *cls,
623 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
625 unsigned int rd_count,
626 const struct GNUNET_GNSRECORD_Data *rd)
628 struct RequestHandle *handle = cls;
629 struct GNUNET_GNSRECORD_Data rd_new[rd_count + handle->rd_count];
633 if (UPDATE_STRATEGY_APPEND == handle->update_strategy)
635 for (i = 0; i < rd_count; i++)
638 for (j = 0; j < handle->rd_count; j++)
639 rd_new[i + j] = handle->rd[j];
640 handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle,
647 if (NULL == handle->add_qe)
649 handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_FAILED);
650 GNUNET_SCHEDULER_add_now (&do_error, handle);
657 * Handle namestore POST/PUT request
659 * @param con_handle the connection handle
661 * @param cls the RequestHandle
664 namestore_add_or_update (struct GNUNET_REST_RequestHandle *con_handle,
668 struct RequestHandle *handle = cls;
669 struct EgoEntry *ego_entry;
674 char term_data[handle->rest_handle->data_size + 1];
676 if (0 >= handle->rest_handle->data_size)
678 handle->response_code = MHD_HTTP_BAD_REQUEST;
679 handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_NO_DATA);
680 GNUNET_SCHEDULER_add_now (&do_error, handle);
683 term_data[handle->rest_handle->data_size] = '\0';
684 GNUNET_memcpy (term_data,
685 handle->rest_handle->data,
686 handle->rest_handle->data_size);
687 data_js = json_loads (term_data, JSON_DECODE_ANY, &err);
688 struct GNUNET_JSON_Specification gnsspec[] =
689 { GNUNET_JSON_spec_gnsrecord (&handle->rd, &handle->rd_count,
690 &handle->record_name),
691 GNUNET_JSON_spec_end () };
692 if (GNUNET_OK != GNUNET_JSON_parse (data_js, gnsspec, NULL, NULL))
694 handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_INVALID_DATA);
695 GNUNET_SCHEDULER_add_now (&do_error, handle);
696 json_decref (data_js);
699 GNUNET_JSON_parse_free (gnsspec);
700 if (0 >= strlen (handle->record_name))
702 handle->response_code = MHD_HTTP_BAD_REQUEST;
703 handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_INVALID_DATA);
704 GNUNET_SCHEDULER_add_now (&do_error, handle);
705 json_decref (data_js);
708 json_decref (data_js);
713 // set zone to name if given
714 if (strlen (GNUNET_REST_API_NS_NAMESTORE) + 1 >= strlen (handle->url))
716 handle->response_code = MHD_HTTP_NOT_FOUND;
717 handle->emsg = GNUNET_strdup (GNUNET_REST_IDENTITY_NOT_FOUND);
718 GNUNET_SCHEDULER_add_now (&do_error, handle);
721 egoname = &handle->url[strlen (GNUNET_REST_API_NS_NAMESTORE) + 1];
722 ego_entry = get_egoentry_namestore (handle, egoname);
724 if (NULL == ego_entry)
726 handle->response_code = MHD_HTTP_NOT_FOUND;
727 handle->emsg = GNUNET_strdup (GNUNET_REST_IDENTITY_NOT_FOUND);
728 GNUNET_SCHEDULER_add_now (&do_error, handle);
731 handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego);
732 handle->add_qe = GNUNET_NAMESTORE_records_lookup (handle->ns_handle,
739 if (NULL == handle->add_qe)
741 handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_FAILED);
742 GNUNET_SCHEDULER_add_now (&do_error, handle);
749 * Handle namestore PUT request
751 * @param con_handle the connection handle
753 * @param cls the RequestHandle
756 namestore_update (struct GNUNET_REST_RequestHandle *con_handle,
760 struct RequestHandle *handle = cls;
761 handle->update_strategy = UPDATE_STRATEGY_REPLACE;
762 namestore_add_or_update (con_handle, url, cls);
767 * Handle namestore POST request
769 * @param con_handle the connection handle
771 * @param cls the RequestHandle
774 namestore_add (struct GNUNET_REST_RequestHandle *con_handle,
778 struct RequestHandle *handle = cls;
779 handle->update_strategy = UPDATE_STRATEGY_APPEND;
780 namestore_add_or_update (con_handle, url, cls);
785 * Handle namestore DELETE request
787 * @param con_handle the connection handle
789 * @param cls the RequestHandle
792 namestore_delete (struct GNUNET_REST_RequestHandle *con_handle,
796 struct RequestHandle *handle = cls;
797 struct EgoEntry *ego_entry;
804 // set zone to name if given
805 if (strlen (GNUNET_REST_API_NS_NAMESTORE) + 1 >= strlen (handle->url))
807 handle->response_code = MHD_HTTP_NOT_FOUND;
808 handle->emsg = GNUNET_strdup (GNUNET_REST_IDENTITY_NOT_FOUND);
809 GNUNET_SCHEDULER_add_now (&do_error, handle);
812 egoname = &handle->url[strlen (GNUNET_REST_API_NS_NAMESTORE) + 1];
813 ego_entry = get_egoentry_namestore (handle, egoname);
814 if (NULL == ego_entry)
816 handle->response_code = MHD_HTTP_NOT_FOUND;
817 handle->emsg = GNUNET_strdup (GNUNET_REST_IDENTITY_NOT_FOUND);
818 GNUNET_SCHEDULER_add_now (&do_error, handle);
821 handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego);
822 labelname = &egoname[strlen (ego_entry->identifier)];
823 // set zone to name if given
824 if (1 >= strlen (labelname))
826 /* label is only "/" */
827 handle->response_code = MHD_HTTP_BAD_REQUEST;
828 handle->emsg = GNUNET_strdup ("Label missing");
829 GNUNET_SCHEDULER_add_now (&do_error, handle);
832 handle->record_name = GNUNET_strdup (labelname + 1);
833 handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle,
840 if (NULL == handle->add_qe)
842 handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_FAILED);
843 GNUNET_SCHEDULER_add_now (&do_error, handle);
850 * Respond to OPTIONS request
852 * @param con_handle the connection handle
854 * @param cls the RequestHandle
857 options_cont (struct GNUNET_REST_RequestHandle *con_handle,
861 struct MHD_Response *resp;
862 struct RequestHandle *handle = cls;
864 // independent of path return all options
865 resp = GNUNET_REST_create_response (NULL);
866 MHD_add_response_header (resp, "Access-Control-Allow-Methods", allow_methods);
867 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
868 GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
874 * Handle rest request
876 * @param handle the request handle
879 init_cont (struct RequestHandle *handle)
881 struct GNUNET_REST_RequestHandlerError err;
882 static const struct GNUNET_REST_RequestHandler handlers[] =
883 { { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_NAMESTORE, &namestore_get },
884 { MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_NAMESTORE, &namestore_add },
885 { MHD_HTTP_METHOD_PUT, GNUNET_REST_API_NS_NAMESTORE, &namestore_update },
886 { MHD_HTTP_METHOD_DELETE, GNUNET_REST_API_NS_NAMESTORE, &namestore_delete },
887 { MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_NAMESTORE, &options_cont },
888 GNUNET_REST_HANDLER_END };
891 GNUNET_REST_handle_request (handle->rest_handle, handlers, &err, handle))
893 handle->response_code = err.error_code;
894 GNUNET_SCHEDULER_add_now (&do_error, handle);
900 * This function is initially called for all egos and then again
901 * whenever a ego's identifier changes or if it is deleted. At the
902 * end of the initial pass over all egos, the function is once called
903 * with 'NULL' for 'ego'. That does NOT mean that the callback won't
904 * be invoked in the future or that there was an error.
906 * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get',
907 * this function is only called ONCE, and 'NULL' being passed in
908 * 'ego' does indicate an error (i.e. name is taken or no default
909 * value is known). If 'ego' is non-NULL and if '*ctx'
910 * is set in those callbacks, the value WILL be passed to a subsequent
911 * call to the identity callback of 'GNUNET_IDENTITY_connect' (if
912 * that one was not NULL).
914 * When an identity is renamed, this function is called with the
915 * (known) ego but the NEW identifier.
917 * When an identity is deleted, this function is called with the
918 * (known) ego and "NULL" for the 'identifier'. In this case,
919 * the 'ego' is henceforth invalid (and the 'ctx' should also be
923 * @param ego ego handle
924 * @param ctx context for application to store data for this ego
925 * (during the lifetime of this process, initially NULL)
926 * @param name identifier assigned by the user for this ego,
927 * NULL if the user just deleted the ego and it
928 * must thus no longer be used
931 id_connect_cb (void *cls,
932 struct GNUNET_IDENTITY_Ego *ego,
936 struct RequestHandle *handle = cls;
937 struct EgoEntry *ego_entry;
938 struct GNUNET_CRYPTO_EcdsaPublicKey pk;
940 if ((NULL == ego) && (ID_REST_STATE_INIT == handle->state))
942 handle->state = ID_REST_STATE_POST_INIT;
946 if (ID_REST_STATE_INIT == handle->state)
948 ego_entry = GNUNET_new (struct EgoEntry);
949 GNUNET_IDENTITY_ego_get_public_key (ego, &pk);
950 ego_entry->keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk);
951 ego_entry->ego = ego;
952 GNUNET_asprintf (&ego_entry->identifier, "%s", name);
953 GNUNET_CONTAINER_DLL_insert_tail (handle->ego_head,
961 * Function processing the REST call
963 * @param method HTTP method
964 * @param url URL of the HTTP request
965 * @param data body of the HTTP request (optional)
966 * @param data_size length of the body
967 * @param proc callback function for the result
968 * @param proc_cls closure for callback function
969 * @return GNUNET_OK if request accepted
972 rest_process_request (struct GNUNET_REST_RequestHandle *rest_handle,
973 GNUNET_REST_ResultProcessor proc,
976 struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
978 handle->response_code = 0;
979 handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
980 handle->proc_cls = proc_cls;
982 handle->rest_handle = rest_handle;
983 handle->zone_pkey = NULL;
985 handle->url = GNUNET_strdup (rest_handle->url);
986 if (handle->url[strlen (handle->url) - 1] == '/')
987 handle->url[strlen (handle->url) - 1] = '\0';
988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting...\n");
990 handle->ns_handle = GNUNET_NAMESTORE_connect (cfg);
991 handle->identity_handle =
992 GNUNET_IDENTITY_connect (cfg, &id_connect_cb, handle);
993 handle->timeout_task =
994 GNUNET_SCHEDULER_add_delayed (handle->timeout, &do_error, handle);
996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected\n");
1001 * Entry point for the plugin.
1003 * @param cls Config info
1004 * @return NULL on error, otherwise the plugin context
1007 libgnunet_plugin_rest_namestore_init (void *cls)
1009 static struct Plugin plugin;
1010 struct GNUNET_REST_Plugin *api;
1013 if (NULL != plugin.cfg)
1014 return NULL; /* can only initialize once! */
1015 memset (&plugin, 0, sizeof(struct Plugin));
1017 api = GNUNET_new (struct GNUNET_REST_Plugin);
1019 api->name = GNUNET_REST_API_NS_NAMESTORE;
1020 api->process_request = &rest_process_request;
1021 GNUNET_asprintf (&allow_methods,
1022 "%s, %s, %s, %s, %s",
1023 MHD_HTTP_METHOD_GET,
1024 MHD_HTTP_METHOD_POST,
1025 MHD_HTTP_METHOD_PUT,
1026 MHD_HTTP_METHOD_DELETE,
1027 MHD_HTTP_METHOD_OPTIONS);
1029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _ ("Namestore REST API initialized\n"));
1035 * Exit point from the plugin.
1037 * @param cls the plugin context (as returned by "init")
1038 * @return always NULL
1041 libgnunet_plugin_rest_namestore_done (void *cls)
1043 struct GNUNET_REST_Plugin *api = cls;
1044 struct Plugin *plugin = api->cls;
1048 GNUNET_free_non_null (allow_methods);
1050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Namestore REST plugin is finished\n");
1055 /* end of plugin_rest_namestore.c */