diff options
-rw-r--r-- | src/namestore/plugin_rest_namestore.c | 143 |
1 files changed, 101 insertions, 42 deletions
diff --git a/src/namestore/plugin_rest_namestore.c b/src/namestore/plugin_rest_namestore.c index efd309d6c..a9b49e19a 100644 --- a/src/namestore/plugin_rest_namestore.c +++ b/src/namestore/plugin_rest_namestore.c | |||
@@ -206,6 +206,11 @@ struct RequestHandle | |||
206 | struct GNUNET_NAMESTORE_QueueEntry *ns_qe; | 206 | struct GNUNET_NAMESTORE_QueueEntry *ns_qe; |
207 | 207 | ||
208 | /** | 208 | /** |
209 | * For bulk import, we need a dedicated Namestore handle | ||
210 | */ | ||
211 | struct GNUNET_NAMESTORE_Handle *nc; | ||
212 | |||
213 | /** | ||
209 | * Response object | 214 | * Response object |
210 | */ | 215 | */ |
211 | json_t *resp_object; | 216 | json_t *resp_object; |
@@ -319,7 +324,8 @@ cleanup_handle (void *cls) | |||
319 | GNUNET_NAMESTORE_zone_iteration_stop (handle->list_it); | 324 | GNUNET_NAMESTORE_zone_iteration_stop (handle->list_it); |
320 | if (NULL != handle->ns_qe) | 325 | if (NULL != handle->ns_qe) |
321 | GNUNET_NAMESTORE_cancel (handle->ns_qe); | 326 | GNUNET_NAMESTORE_cancel (handle->ns_qe); |
322 | 327 | if (NULL != handle->nc) | |
328 | GNUNET_NAMESTORE_disconnect (handle->nc); | ||
323 | if (NULL != handle->resp_object) | 329 | if (NULL != handle->resp_object) |
324 | { | 330 | { |
325 | json_decref (handle->resp_object); | 331 | json_decref (handle->resp_object); |
@@ -726,20 +732,16 @@ ns_lookup_cb (void *cls, | |||
726 | } | 732 | } |
727 | } | 733 | } |
728 | 734 | ||
729 | /** | 735 | |
730 | * Import callback | ||
731 | * | ||
732 | * @param cls the `struct RequestHandle` | ||
733 | * @param success the success indicating integer, GNUNET_OK on success | ||
734 | * @param emsg the error message (can be NULL) | ||
735 | */ | ||
736 | static void | 736 | static void |
737 | import_finished_cb (void *cls, int32_t success, const char *emsg) | 737 | bulk_tx_commit_cb (void *cls, int32_t success, const char *emsg) |
738 | { | 738 | { |
739 | struct RequestHandle *handle = cls; | 739 | struct RequestHandle *handle = cls; |
740 | struct MHD_Response *resp; | 740 | struct MHD_Response *resp; |
741 | 741 | ||
742 | handle->ns_qe = NULL; | 742 | handle->ns_qe = NULL; |
743 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
744 | "Commit finished, %d\n", success); | ||
743 | if (GNUNET_YES != success) | 745 | if (GNUNET_YES != success) |
744 | { | 746 | { |
745 | if (NULL != emsg) | 747 | if (NULL != emsg) |
@@ -748,7 +750,7 @@ import_finished_cb (void *cls, int32_t success, const char *emsg) | |||
748 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 750 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
749 | return; | 751 | return; |
750 | } | 752 | } |
751 | handle->emsg = GNUNET_strdup ("Error importing records"); | 753 | handle->emsg = GNUNET_strdup ("Error importing records on commit"); |
752 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 754 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
753 | return; | 755 | return; |
754 | } | 756 | } |
@@ -759,45 +761,46 @@ import_finished_cb (void *cls, int32_t success, const char *emsg) | |||
759 | 761 | ||
760 | 762 | ||
761 | /** | 763 | /** |
762 | * Handle namestore POST import | 764 | * Import callback |
763 | * | 765 | * |
764 | * @param con_handle the connection handle | 766 | * @param cls the `struct RequestHandle` |
765 | * @param url the url | 767 | * @param success the success indicating integer, GNUNET_OK on success |
766 | * @param cls the RequestHandle | 768 | * @param emsg the error message (can be NULL) |
767 | */ | 769 | */ |
768 | void | 770 | static void |
769 | namestore_import (struct GNUNET_REST_RequestHandle *con_handle, | 771 | import_finished_cb (void *cls, int32_t success, const char *emsg) |
770 | const char *url, | ||
771 | void *cls) | ||
772 | { | 772 | { |
773 | struct RequestHandle *handle = cls; | 773 | struct RequestHandle *handle = cls; |
774 | struct EgoEntry *ego_entry; | ||
775 | char *egoname; | ||
776 | json_t *data_js; | ||
777 | json_error_t err; | ||
778 | 774 | ||
779 | char term_data[handle->rest_handle->data_size + 1]; | 775 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
780 | // set zone to name if given | 776 | "Import finished, %d\n", success); |
781 | if (strlen (GNUNET_REST_API_NS_NAMESTORE_IMPORT) + 1 >= strlen (handle->url)) | 777 | handle->ns_qe = NULL; |
778 | if (GNUNET_YES != success) | ||
782 | { | 779 | { |
783 | handle->response_code = MHD_HTTP_NOT_FOUND; | 780 | if (NULL != emsg) |
784 | handle->emsg = GNUNET_strdup (GNUNET_REST_IDENTITY_NOT_FOUND); | 781 | { |
782 | handle->emsg = GNUNET_strdup (emsg); | ||
783 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
784 | return; | ||
785 | } | ||
786 | handle->emsg = GNUNET_strdup ("Error importing records"); | ||
785 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 787 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
786 | return; | 788 | return; |
787 | } | 789 | } |
788 | ego_entry = NULL; | 790 | handle->ns_qe = GNUNET_NAMESTORE_transaction_commit (handle->nc, |
789 | 791 | &bulk_tx_commit_cb, | |
790 | egoname = &handle->url[strlen (GNUNET_REST_API_NS_NAMESTORE_IMPORT) + 1]; | 792 | handle); |
791 | ego_entry = get_egoentry_namestore (handle, egoname); | 793 | } |
792 | 794 | ||
793 | if (NULL == ego_entry) | 795 | static void |
794 | { | 796 | bulk_tx_start (void *cls, int32_t success, const char *emsg) |
795 | handle->response_code = MHD_HTTP_NOT_FOUND; | 797 | { |
796 | handle->emsg = GNUNET_strdup (GNUNET_REST_IDENTITY_NOT_FOUND); | 798 | struct RequestHandle *handle = cls; |
797 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 799 | json_t *data_js; |
798 | return; | 800 | json_error_t err; |
799 | } | ||
800 | 801 | ||
802 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
803 | "Transaction started...\n"); | ||
801 | if (0 >= handle->rest_handle->data_size) | 804 | if (0 >= handle->rest_handle->data_size) |
802 | { | 805 | { |
803 | handle->response_code = MHD_HTTP_BAD_REQUEST; | 806 | handle->response_code = MHD_HTTP_BAD_REQUEST; |
@@ -805,6 +808,7 @@ namestore_import (struct GNUNET_REST_RequestHandle *con_handle, | |||
805 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 808 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
806 | return; | 809 | return; |
807 | } | 810 | } |
811 | char term_data[handle->rest_handle->data_size + 1]; | ||
808 | term_data[handle->rest_handle->data_size] = '\0'; | 812 | term_data[handle->rest_handle->data_size] = '\0'; |
809 | GNUNET_memcpy (term_data, | 813 | GNUNET_memcpy (term_data, |
810 | handle->rest_handle->data, | 814 | handle->rest_handle->data, |
@@ -851,10 +855,9 @@ namestore_import (struct GNUNET_REST_RequestHandle *con_handle, | |||
851 | "Parsed record set for name %s\n", ri[index].a_label); | 855 | "Parsed record set for name %s\n", ri[index].a_label); |
852 | } | 856 | } |
853 | } | 857 | } |
854 | //json_decref (data_js); | 858 | // json_decref (data_js); |
855 | 859 | ||
856 | handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego); | 860 | handle->ns_qe = GNUNET_NAMESTORE_records_store2 (handle->nc, |
857 | handle->ns_qe = GNUNET_NAMESTORE_records_store2 (ns_handle, | ||
858 | handle->zone_pkey, | 861 | handle->zone_pkey, |
859 | rd_set_count, | 862 | rd_set_count, |
860 | ri, | 863 | ri, |
@@ -868,6 +871,61 @@ namestore_import (struct GNUNET_REST_RequestHandle *con_handle, | |||
868 | } | 871 | } |
869 | } | 872 | } |
870 | 873 | ||
874 | |||
875 | |||
876 | /** | ||
877 | * Handle namestore POST import | ||
878 | * | ||
879 | * @param con_handle the connection handle | ||
880 | * @param url the url | ||
881 | * @param cls the RequestHandle | ||
882 | */ | ||
883 | void | ||
884 | namestore_import (struct GNUNET_REST_RequestHandle *con_handle, | ||
885 | const char *url, | ||
886 | void *cls) | ||
887 | { | ||
888 | struct RequestHandle *handle = cls; | ||
889 | struct EgoEntry *ego_entry; | ||
890 | char *egoname; | ||
891 | |||
892 | // set zone to name if given | ||
893 | if (strlen (GNUNET_REST_API_NS_NAMESTORE_IMPORT) + 1 >= strlen ( | ||
894 | handle->url)) | ||
895 | { | ||
896 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
897 | handle->emsg = GNUNET_strdup (GNUNET_REST_IDENTITY_NOT_FOUND); | ||
898 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
899 | return; | ||
900 | } | ||
901 | ego_entry = NULL; | ||
902 | |||
903 | egoname = &handle->url[strlen (GNUNET_REST_API_NS_NAMESTORE_IMPORT) + 1]; | ||
904 | ego_entry = get_egoentry_namestore (handle, egoname); | ||
905 | |||
906 | if (NULL == ego_entry) | ||
907 | { | ||
908 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
909 | handle->emsg = GNUNET_strdup (GNUNET_REST_IDENTITY_NOT_FOUND); | ||
910 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
911 | return; | ||
912 | } | ||
913 | handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego); | ||
914 | |||
915 | // We need a per-client connection for a transactional bulk import | ||
916 | handle->nc = GNUNET_NAMESTORE_connect (cfg); | ||
917 | if (NULL == handle->nc) | ||
918 | { | ||
919 | handle->response_code = MHD_HTTP_BAD_REQUEST; | ||
920 | handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_NO_DATA); | ||
921 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
922 | return; | ||
923 | } | ||
924 | handle->ns_qe = GNUNET_NAMESTORE_transaction_begin (handle->nc, | ||
925 | &bulk_tx_start, | ||
926 | handle); | ||
927 | } | ||
928 | |||
871 | /** | 929 | /** |
872 | * Handle namestore POST/PUT request | 930 | * Handle namestore POST/PUT request |
873 | * | 931 | * |
@@ -1188,7 +1246,8 @@ rest_process_request (struct GNUNET_REST_RequestHandle *rest_handle, | |||
1188 | struct GNUNET_REST_RequestHandlerError err; | 1246 | struct GNUNET_REST_RequestHandlerError err; |
1189 | static const struct GNUNET_REST_RequestHandler handlers[] = | 1247 | static const struct GNUNET_REST_RequestHandler handlers[] = |
1190 | { { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_NAMESTORE, &namestore_get }, | 1248 | { { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_NAMESTORE, &namestore_get }, |
1191 | { MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_NAMESTORE_IMPORT, &namestore_import }, | 1249 | { MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_NAMESTORE_IMPORT, |
1250 | &namestore_import }, | ||
1192 | { MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_NAMESTORE, &namestore_add }, | 1251 | { MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_NAMESTORE, &namestore_add }, |
1193 | { MHD_HTTP_METHOD_PUT, GNUNET_REST_API_NS_NAMESTORE, &namestore_update }, | 1252 | { MHD_HTTP_METHOD_PUT, GNUNET_REST_API_NS_NAMESTORE, &namestore_update }, |
1194 | { MHD_HTTP_METHOD_DELETE, GNUNET_REST_API_NS_NAMESTORE, | 1253 | { MHD_HTTP_METHOD_DELETE, GNUNET_REST_API_NS_NAMESTORE, |