diff options
Diffstat (limited to 'src/namestore/plugin_rest_namestore.c')
-rw-r--r-- | src/namestore/plugin_rest_namestore.c | 144 |
1 files changed, 106 insertions, 38 deletions
diff --git a/src/namestore/plugin_rest_namestore.c b/src/namestore/plugin_rest_namestore.c index 4184d93a1..95b9b428f 100644 --- a/src/namestore/plugin_rest_namestore.c +++ b/src/namestore/plugin_rest_namestore.c | |||
@@ -163,7 +163,7 @@ struct RequestHandle | |||
163 | /** | 163 | /** |
164 | * NAMESTORE Operation | 164 | * NAMESTORE Operation |
165 | */ | 165 | */ |
166 | struct GNUNET_NAMESTORE_QueueEntry *add_qe; | 166 | struct GNUNET_NAMESTORE_QueueEntry *ns_qe; |
167 | 167 | ||
168 | /** | 168 | /** |
169 | * Response object | 169 | * Response object |
@@ -292,8 +292,8 @@ cleanup_handle (void *cls) | |||
292 | GNUNET_SCHEDULER_cancel (handle->timeout_task); | 292 | GNUNET_SCHEDULER_cancel (handle->timeout_task); |
293 | if (NULL != handle->list_it) | 293 | if (NULL != handle->list_it) |
294 | GNUNET_NAMESTORE_zone_iteration_stop (handle->list_it); | 294 | GNUNET_NAMESTORE_zone_iteration_stop (handle->list_it); |
295 | if (NULL != handle->add_qe) | 295 | if (NULL != handle->ns_qe) |
296 | GNUNET_NAMESTORE_cancel (handle->add_qe); | 296 | GNUNET_NAMESTORE_cancel (handle->ns_qe); |
297 | if (NULL != handle->identity_handle) | 297 | if (NULL != handle->identity_handle) |
298 | GNUNET_IDENTITY_disconnect (handle->identity_handle); | 298 | GNUNET_IDENTITY_disconnect (handle->identity_handle); |
299 | if (NULL != handle->ns_handle) | 299 | if (NULL != handle->ns_handle) |
@@ -410,7 +410,7 @@ create_finished (void *cls, int32_t success, const char *emsg) | |||
410 | struct RequestHandle *handle = cls; | 410 | struct RequestHandle *handle = cls; |
411 | struct MHD_Response *resp; | 411 | struct MHD_Response *resp; |
412 | 412 | ||
413 | handle->add_qe = NULL; | 413 | handle->ns_qe = NULL; |
414 | if (GNUNET_YES != success) | 414 | if (GNUNET_YES != success) |
415 | { | 415 | { |
416 | if (NULL != emsg) | 416 | if (NULL != emsg) |
@@ -441,7 +441,7 @@ del_finished (void *cls, int32_t success, const char *emsg) | |||
441 | { | 441 | { |
442 | struct RequestHandle *handle = cls; | 442 | struct RequestHandle *handle = cls; |
443 | 443 | ||
444 | handle->add_qe = NULL; | 444 | handle->ns_qe = NULL; |
445 | if (GNUNET_NO == success) | 445 | if (GNUNET_NO == success) |
446 | { | 446 | { |
447 | handle->response_code = MHD_HTTP_NOT_FOUND; | 447 | handle->response_code = MHD_HTTP_NOT_FOUND; |
@@ -525,13 +525,70 @@ namestore_list_iteration (void *cls, | |||
525 | rd_filtered[j].data = rd[i].data; | 525 | rd_filtered[j].data = rd[i].data; |
526 | j++; | 526 | j++; |
527 | } | 527 | } |
528 | record_obj = GNUNET_JSON_from_gnsrecord (rname, | 528 | /** Only add if not empty **/ |
529 | rd_filtered, | 529 | if (j > 0) |
530 | j); | 530 | { |
531 | json_array_append_new (handle->resp_object, record_obj); | 531 | record_obj = GNUNET_JSON_from_gnsrecord (rname, |
532 | rd_filtered, | ||
533 | j); | ||
534 | json_array_append_new (handle->resp_object, record_obj); | ||
535 | } | ||
532 | GNUNET_NAMESTORE_zone_iterator_next (handle->list_it, 1); | 536 | GNUNET_NAMESTORE_zone_iterator_next (handle->list_it, 1); |
533 | } | 537 | } |
534 | 538 | ||
539 | /** | ||
540 | * Handle lookup error | ||
541 | * | ||
542 | * @param cls the request handle | ||
543 | */ | ||
544 | static void | ||
545 | ns_lookup_error_cb (void *cls) | ||
546 | { | ||
547 | struct RequestHandle *handle = cls; | ||
548 | |||
549 | handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_FAILED); | ||
550 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
551 | } | ||
552 | |||
553 | |||
554 | static void | ||
555 | ns_get_lookup_cb (void *cls, | ||
556 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, | ||
557 | const char *label, | ||
558 | unsigned int rd_len, | ||
559 | const struct GNUNET_GNSRECORD_Data *rd) | ||
560 | { | ||
561 | struct RequestHandle *handle = cls; | ||
562 | struct GNUNET_GNSRECORD_Data rd_filtered[rd_len]; | ||
563 | json_t *record_obj; | ||
564 | int i = 0; | ||
565 | int j = 0; | ||
566 | |||
567 | handle->ns_qe = NULL; | ||
568 | if (NULL == handle->resp_object) | ||
569 | handle->resp_object = json_array (); | ||
570 | for (i = 0; i < rd_len; i++) | ||
571 | { | ||
572 | if ((GNUNET_GNSRECORD_TYPE_ANY != handle->record_type) && | ||
573 | (rd[i].record_type != handle->record_type)) | ||
574 | continue; /* Apply filter */ | ||
575 | rd_filtered[j] = rd[i]; | ||
576 | rd_filtered[j].data = rd[i].data; | ||
577 | j++; | ||
578 | } | ||
579 | /** Only add if not empty **/ | ||
580 | if (j > 0) | ||
581 | { | ||
582 | record_obj = GNUNET_JSON_from_gnsrecord (label, | ||
583 | rd_filtered, | ||
584 | j); | ||
585 | json_array_append_new (handle->resp_object, record_obj); | ||
586 | } | ||
587 | GNUNET_SCHEDULER_add_now (&namestore_list_finished, handle); | ||
588 | } | ||
589 | |||
590 | |||
591 | |||
535 | 592 | ||
536 | /** | 593 | /** |
537 | * Handle namestore GET request | 594 | * Handle namestore GET request |
@@ -549,6 +606,7 @@ namestore_get (struct GNUNET_REST_RequestHandle *con_handle, | |||
549 | struct EgoEntry *ego_entry; | 606 | struct EgoEntry *ego_entry; |
550 | struct GNUNET_HashCode key; | 607 | struct GNUNET_HashCode key; |
551 | char *egoname; | 608 | char *egoname; |
609 | char *labelname; | ||
552 | char *typename; | 610 | char *typename; |
553 | 611 | ||
554 | egoname = NULL; | 612 | egoname = NULL; |
@@ -585,17 +643,36 @@ namestore_get (struct GNUNET_REST_RequestHandle *con_handle, | |||
585 | &key); | 643 | &key); |
586 | handle->record_type = GNUNET_GNSRECORD_typename_to_number (typename); | 644 | handle->record_type = GNUNET_GNSRECORD_typename_to_number (typename); |
587 | } | 645 | } |
588 | 646 | labelname = &egoname[strlen (ego_entry->identifier)]; | |
589 | handle->list_it = | 647 | // set zone to name if given |
590 | GNUNET_NAMESTORE_zone_iteration_start (handle->ns_handle, | 648 | if (1 >= strlen (labelname)) |
591 | handle->zone_pkey, | 649 | { |
592 | &namestore_iteration_error, | 650 | handle->list_it = |
593 | handle, | 651 | GNUNET_NAMESTORE_zone_iteration_start (handle->ns_handle, |
594 | &namestore_list_iteration, | 652 | handle->zone_pkey, |
595 | handle, | 653 | &namestore_iteration_error, |
596 | &namestore_list_finished, | 654 | handle, |
597 | handle); | 655 | &namestore_list_iteration, |
598 | if (NULL == handle->list_it) | 656 | handle, |
657 | &namestore_list_finished, | ||
658 | handle); | ||
659 | if (NULL == handle->list_it) | ||
660 | { | ||
661 | handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_FAILED); | ||
662 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
663 | return; | ||
664 | } | ||
665 | return; | ||
666 | } | ||
667 | handle->record_name = GNUNET_strdup (labelname + 1); | ||
668 | handle->ns_qe = GNUNET_NAMESTORE_records_lookup (handle->ns_handle, | ||
669 | handle->zone_pkey, | ||
670 | handle->record_name, | ||
671 | &ns_lookup_error_cb, | ||
672 | handle, | ||
673 | &ns_get_lookup_cb, | ||
674 | handle); | ||
675 | if (NULL == handle->ns_qe) | ||
599 | { | 676 | { |
600 | handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_FAILED); | 677 | handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_FAILED); |
601 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 678 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
@@ -604,15 +681,6 @@ namestore_get (struct GNUNET_REST_RequestHandle *con_handle, | |||
604 | } | 681 | } |
605 | 682 | ||
606 | 683 | ||
607 | static void | ||
608 | ns_lookup_error_cb (void *cls) | ||
609 | { | ||
610 | struct RequestHandle *handle = cls; | ||
611 | |||
612 | handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_FAILED); | ||
613 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
614 | } | ||
615 | |||
616 | 684 | ||
617 | static void | 685 | static void |
618 | ns_lookup_cb (void *cls, | 686 | ns_lookup_cb (void *cls, |
@@ -633,14 +701,14 @@ ns_lookup_cb (void *cls, | |||
633 | } | 701 | } |
634 | for (j = 0; j < handle->rd_count; j++) | 702 | for (j = 0; j < handle->rd_count; j++) |
635 | rd_new[i + j] = handle->rd[j]; | 703 | rd_new[i + j] = handle->rd[j]; |
636 | handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle, | 704 | handle->ns_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle, |
637 | handle->zone_pkey, | 705 | handle->zone_pkey, |
638 | handle->record_name, | 706 | handle->record_name, |
639 | i + j, | 707 | i + j, |
640 | rd_new, | 708 | rd_new, |
641 | &create_finished, | 709 | &create_finished, |
642 | handle); | 710 | handle); |
643 | if (NULL == handle->add_qe) | 711 | if (NULL == handle->ns_qe) |
644 | { | 712 | { |
645 | handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_FAILED); | 713 | handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_FAILED); |
646 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 714 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
@@ -725,14 +793,14 @@ namestore_add_or_update (struct GNUNET_REST_RequestHandle *con_handle, | |||
725 | return; | 793 | return; |
726 | } | 794 | } |
727 | handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego); | 795 | handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego); |
728 | handle->add_qe = GNUNET_NAMESTORE_records_lookup (handle->ns_handle, | 796 | handle->ns_qe = GNUNET_NAMESTORE_records_lookup (handle->ns_handle, |
729 | handle->zone_pkey, | 797 | handle->zone_pkey, |
730 | handle->record_name, | 798 | handle->record_name, |
731 | &ns_lookup_error_cb, | 799 | &ns_lookup_error_cb, |
732 | handle, | 800 | handle, |
733 | &ns_lookup_cb, | 801 | &ns_lookup_cb, |
734 | handle); | 802 | handle); |
735 | if (NULL == handle->add_qe) | 803 | if (NULL == handle->ns_qe) |
736 | { | 804 | { |
737 | handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_FAILED); | 805 | handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_FAILED); |
738 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 806 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
@@ -826,14 +894,14 @@ namestore_delete (struct GNUNET_REST_RequestHandle *con_handle, | |||
826 | } | 894 | } |
827 | 895 | ||
828 | handle->record_name = GNUNET_strdup (labelname + 1); | 896 | handle->record_name = GNUNET_strdup (labelname + 1); |
829 | handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle, | 897 | handle->ns_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle, |
830 | handle->zone_pkey, | 898 | handle->zone_pkey, |
831 | handle->record_name, | 899 | handle->record_name, |
832 | 0, | 900 | 0, |
833 | NULL, | 901 | NULL, |
834 | &del_finished, | 902 | &del_finished, |
835 | handle); | 903 | handle); |
836 | if (NULL == handle->add_qe) | 904 | if (NULL == handle->ns_qe) |
837 | { | 905 | { |
838 | handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_FAILED); | 906 | handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_FAILED); |
839 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 907 | GNUNET_SCHEDULER_add_now (&do_error, handle); |