aboutsummaryrefslogtreecommitdiff
path: root/src/namestore/plugin_rest_namestore.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2016-07-09 14:29:34 +0000
committerChristian Grothoff <christian@grothoff.org>2016-07-09 14:29:34 +0000
commit03374cc39814fa4bfb8d96e1165c688c44d8342f (patch)
treeed772250c49b39d82b9708ec8df0ee3e07e366d1 /src/namestore/plugin_rest_namestore.c
parent0c13a1e6fb0baffd83d413f313742a5f2b420297 (diff)
downloadgnunet-03374cc39814fa4bfb8d96e1165c688c44d8342f.tar.gz
gnunet-03374cc39814fa4bfb8d96e1165c688c44d8342f.zip
make namestore API less brittle/sublte to use
Diffstat (limited to 'src/namestore/plugin_rest_namestore.c')
-rw-r--r--src/namestore/plugin_rest_namestore.c86
1 files changed, 60 insertions, 26 deletions
diff --git a/src/namestore/plugin_rest_namestore.c b/src/namestore/plugin_rest_namestore.c
index 35d3595ce..58ab46ca9 100644
--- a/src/namestore/plugin_rest_namestore.c
+++ b/src/namestore/plugin_rest_namestore.c
@@ -388,6 +388,38 @@ cleanup_handle_delayed (void *cls)
388 388
389 389
390/** 390/**
391 * Iteration over all results finished, build final
392 * response.
393 *
394 * @param cls the `struct RequestHandle`
395 */
396static void
397namestore_list_finished (void *cls)
398{
399 struct RequestHandle *handle = cls;
400 char *result;
401 struct MHD_Response *resp;
402
403 handle->list_it = NULL;
404 if (GNUNET_SYSERR ==
405 GNUNET_JSONAPI_document_serialize (handle->resp_object,
406 &result))
407 {
408 do_error (handle);
409 return;
410 }
411 resp = GNUNET_REST_create_response (result);
412 handle->proc (handle->proc_cls,
413 resp,
414 MHD_HTTP_OK);
415 GNUNET_free_non_null (result);
416 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed,
417 handle);
418}
419
420
421
422/**
391 * Create a response with requested records 423 * Create a response with requested records
392 * 424 *
393 * @param handle the RequestHandle 425 * @param handle the RequestHandle
@@ -401,31 +433,13 @@ namestore_list_response (void *cls,
401{ 433{
402 struct RequestHandle *handle = cls; 434 struct RequestHandle *handle = cls;
403 struct GNUNET_JSONAPI_Resource *json_resource; 435 struct GNUNET_JSONAPI_Resource *json_resource;
404 struct MHD_Response *resp;
405 json_t *result_array; 436 json_t *result_array;
406 json_t *record_obj; 437 json_t *record_obj;
407 int i; 438 int i;
408 char *result;
409 439
410 if (NULL == handle->resp_object) 440 if (NULL == handle->resp_object)
411 handle->resp_object = GNUNET_JSONAPI_document_new (); 441 handle->resp_object = GNUNET_JSONAPI_document_new ();
412 442
413 if (NULL == rname)
414 {
415 handle->list_it = NULL;
416 //Handle response
417 if (GNUNET_SYSERR == GNUNET_JSONAPI_document_serialize (handle->resp_object, &result))
418 {
419 GNUNET_SCHEDULER_add_now (&do_error, handle);
420 return;
421 }
422 resp = GNUNET_REST_create_response (result);
423 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
424 GNUNET_free_non_null (result);
425 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
426 return;
427 }
428
429 if ( (NULL != handle->name) && 443 if ( (NULL != handle->name) &&
430 (0 != strcmp (handle->name, rname)) ) 444 (0 != strcmp (handle->name, rname)) )
431 { 445 {
@@ -464,6 +478,7 @@ namestore_list_response (void *cls,
464 GNUNET_NAMESTORE_zone_iterator_next (handle->list_it); 478 GNUNET_NAMESTORE_zone_iterator_next (handle->list_it);
465} 479}
466 480
481
467static void 482static void
468create_finished (void *cls, int32_t success, const char *emsg) 483create_finished (void *cls, int32_t success, const char *emsg)
469{ 484{
@@ -506,11 +521,10 @@ create_new_record_cont (void *cls,
506 struct RequestHandle *handle = cls; 521 struct RequestHandle *handle = cls;
507 522
508 handle->add_qe = NULL; 523 handle->add_qe = NULL;
509 if ( (NULL != zone_key) && 524 if (0 != strcmp (rec_name, handle->name))
510 (0 != strcmp (rec_name, handle->name)) )
511 { 525 {
512 GNUNET_break (0); 526 GNUNET_break (0);
513 GNUNET_SCHEDULER_add_now (&do_error, handle); 527 do_error (handle);
514 return; 528 return;
515 } 529 }
516 530
@@ -533,6 +547,7 @@ create_new_record_cont (void *cls,
533 handle); 547 handle);
534} 548}
535 549
550
536static void 551static void
537del_finished (void *cls, 552del_finished (void *cls,
538 int32_t success, 553 int32_t success,
@@ -565,6 +580,7 @@ del_finished (void *cls,
565 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle); 580 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
566} 581}
567 582
583
568static void 584static void
569del_cont (void *cls, 585del_cont (void *cls,
570 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, 586 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
@@ -573,13 +589,14 @@ del_cont (void *cls,
573 const struct GNUNET_GNSRECORD_Data *rd) 589 const struct GNUNET_GNSRECORD_Data *rd)
574{ 590{
575 struct RequestHandle *handle = cls; 591 struct RequestHandle *handle = cls;
592
576 handle->add_qe = NULL; 593 handle->add_qe = NULL;
577 if (0 == rd_count) 594 if (0 == rd_count)
578 { 595 {
579 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 596 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
580 _("There are no records under label `%s' that could be deleted.\n"), 597 _("There are no records under label `%s' that could be deleted.\n"),
581 label); 598 label);
582 GNUNET_SCHEDULER_add_now (&do_error, handle); 599 do_error (handle);
583 return; 600 return;
584 } 601 }
585 602
@@ -591,6 +608,7 @@ del_cont (void *cls,
591 handle); 608 handle);
592} 609}
593 610
611
594static void 612static void
595namestore_delete_cont (struct GNUNET_REST_RequestHandle *con, 613namestore_delete_cont (struct GNUNET_REST_RequestHandle *con,
596 const char *url, 614 const char *url,
@@ -607,10 +625,13 @@ namestore_delete_cont (struct GNUNET_REST_RequestHandle *con,
607 handle->add_qe = GNUNET_NAMESTORE_records_lookup (handle->ns_handle, 625 handle->add_qe = GNUNET_NAMESTORE_records_lookup (handle->ns_handle,
608 &handle->zone_pkey, 626 &handle->zone_pkey,
609 handle->name, 627 handle->name,
628 &do_error,
629 handle,
610 &del_cont, 630 &del_cont,
611 handle); 631 handle);
612} 632}
613 633
634
614static int 635static int
615json_to_gnsrecord (const json_t *records_json, 636json_to_gnsrecord (const json_t *records_json,
616 struct GNUNET_GNSRECORD_Data **rd, 637 struct GNUNET_GNSRECORD_Data **rd,
@@ -713,6 +734,7 @@ json_to_gnsrecord (const json_t *records_json,
713 return GNUNET_OK; 734 return GNUNET_OK;
714} 735}
715 736
737
716static void 738static void
717namestore_create_cont (struct GNUNET_REST_RequestHandle *con, 739namestore_create_cont (struct GNUNET_REST_RequestHandle *con,
718 const char *url, 740 const char *url,
@@ -730,7 +752,7 @@ namestore_create_cont (struct GNUNET_REST_RequestHandle *con,
730 GNUNET_JSON_spec_jsonapi_document (&json_obj), 752 GNUNET_JSON_spec_jsonapi_document (&json_obj),
731 GNUNET_JSON_spec_end() 753 GNUNET_JSON_spec_end()
732 }; 754 };
733 755
734 if (strlen (GNUNET_REST_API_NS_NAMESTORE) != strlen (handle->url)) 756 if (strlen (GNUNET_REST_API_NS_NAMESTORE) != strlen (handle->url))
735 { 757 {
736 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 758 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@@ -805,9 +827,13 @@ namestore_create_cont (struct GNUNET_REST_RequestHandle *con,
805 handle->add_qe = GNUNET_NAMESTORE_records_lookup (handle->ns_handle, 827 handle->add_qe = GNUNET_NAMESTORE_records_lookup (handle->ns_handle,
806 &handle->zone_pkey, 828 &handle->zone_pkey,
807 handle->name, 829 handle->name,
808 &create_new_record_cont, handle ); 830 &do_error,
831 handle,
832 &create_new_record_cont,
833 handle);
809} 834}
810 835
836
811static void 837static void
812namestore_zkey_response (void *cls, 838namestore_zkey_response (void *cls,
813 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, 839 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
@@ -847,10 +873,9 @@ namestore_zkey_response (void *cls,
847 GNUNET_JSONAPI_document_delete (json_obj); 873 GNUNET_JSONAPI_document_delete (json_obj);
848 GNUNET_free (result); 874 GNUNET_free (result);
849 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle); 875 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
850 return;
851
852} 876}
853 877
878
854static void 879static void
855namestore_zkey_cont (struct GNUNET_REST_RequestHandle *con, 880namestore_zkey_cont (struct GNUNET_REST_RequestHandle *con,
856 const char *url, 881 const char *url,
@@ -887,22 +912,31 @@ namestore_zkey_cont (struct GNUNET_REST_RequestHandle *con,
887 handle->reverse_qe = GNUNET_NAMESTORE_zone_to_name (handle->ns_handle, 912 handle->reverse_qe = GNUNET_NAMESTORE_zone_to_name (handle->ns_handle,
888 &handle->zone_pkey, 913 &handle->zone_pkey,
889 &pubkey, 914 &pubkey,
915 &do_error,
916 handle,
890 &namestore_zkey_response, 917 &namestore_zkey_response,
891 handle); 918 handle);
892} 919}
893 920
921
894static void 922static void
895namestore_info_cont (struct GNUNET_REST_RequestHandle *con, 923namestore_info_cont (struct GNUNET_REST_RequestHandle *con,
896 const char *url, 924 const char *url,
897 void *cls) 925 void *cls)
898{ 926{
899 struct RequestHandle *handle = cls; 927 struct RequestHandle *handle = cls;
928
900 handle->list_it = GNUNET_NAMESTORE_zone_iteration_start (handle->ns_handle, 929 handle->list_it = GNUNET_NAMESTORE_zone_iteration_start (handle->ns_handle,
901 &handle->zone_pkey, 930 &handle->zone_pkey,
931 &do_error,
932 handle,
902 &namestore_list_response, 933 &namestore_list_response,
934 handle,
935 &namestore_list_finished,
903 handle); 936 handle);
904} 937}
905 938
939
906static char* 940static char*
907get_name_from_url (const char* url) 941get_name_from_url (const char* url)
908{ 942{