diff options
author | Martin Schanzenbach <mschanzenbach@posteo.de> | 2016-05-02 16:32:36 +0000 |
---|---|---|
committer | Martin Schanzenbach <mschanzenbach@posteo.de> | 2016-05-02 16:32:36 +0000 |
commit | bee208bcd6803829aa26c55a4c8e176a5f2c815f (patch) | |
tree | 6974bc9e94a1ec938b5376d2631233d2e75baf7d /src/namestore | |
parent | fc9b25be2dbe5392501dbc0b6659c1aa50809bc2 (diff) | |
download | gnunet-bee208bcd6803829aa26c55a4c8e176a5f2c815f.tar.gz gnunet-bee208bcd6803829aa26c55a4c8e176a5f2c815f.zip |
- Refactor jsonapi into separate module
Diffstat (limited to 'src/namestore')
-rw-r--r-- | src/namestore/Makefile.am | 1 | ||||
-rw-r--r-- | src/namestore/plugin_rest_namestore.c | 65 |
2 files changed, 34 insertions, 32 deletions
diff --git a/src/namestore/Makefile.am b/src/namestore/Makefile.am index 887dea4ed..cf47e8507 100644 --- a/src/namestore/Makefile.am +++ b/src/namestore/Makefile.am | |||
@@ -192,6 +192,7 @@ libgnunet_plugin_rest_namestore_la_LIBADD = \ | |||
192 | libgnunetnamestore.la \ | 192 | libgnunetnamestore.la \ |
193 | $(top_builddir)/src/rest/libgnunetrest.la \ | 193 | $(top_builddir)/src/rest/libgnunetrest.la \ |
194 | $(top_builddir)/src/identity/libgnunetidentity.la \ | 194 | $(top_builddir)/src/identity/libgnunetidentity.la \ |
195 | $(top_builddir)/src/jsonapi/libgnunetjsonapi.la \ | ||
195 | $(top_builddir)/src/util/libgnunetutil.la $(XLIBS) \ | 196 | $(top_builddir)/src/util/libgnunetutil.la $(XLIBS) \ |
196 | $(LTLIBINTL) -ljansson -lmicrohttpd | 197 | $(LTLIBINTL) -ljansson -lmicrohttpd |
197 | libgnunet_plugin_rest_namestore_la_LDFLAGS = \ | 198 | libgnunet_plugin_rest_namestore_la_LDFLAGS = \ |
diff --git a/src/namestore/plugin_rest_namestore.c b/src/namestore/plugin_rest_namestore.c index 4612528c6..d33bf483b 100644 --- a/src/namestore/plugin_rest_namestore.c +++ b/src/namestore/plugin_rest_namestore.c | |||
@@ -29,6 +29,7 @@ | |||
29 | #include "gnunet_namestore_service.h" | 29 | #include "gnunet_namestore_service.h" |
30 | #include "gnunet_identity_service.h" | 30 | #include "gnunet_identity_service.h" |
31 | #include "gnunet_rest_lib.h" | 31 | #include "gnunet_rest_lib.h" |
32 | #include "gnunet_jsonapi_lib.h" | ||
32 | #include "microhttpd.h" | 33 | #include "microhttpd.h" |
33 | #include <jansson.h> | 34 | #include <jansson.h> |
34 | 35 | ||
@@ -105,7 +106,7 @@ struct RequestHandle | |||
105 | /** | 106 | /** |
106 | * JSON response object | 107 | * JSON response object |
107 | */ | 108 | */ |
108 | struct JsonApiObject *resp_object; | 109 | struct GNUNET_JSONAPI_Object *resp_object; |
109 | 110 | ||
110 | /** | 111 | /** |
111 | * Rest connection | 112 | * Rest connection |
@@ -255,7 +256,7 @@ cleanup_handle (struct RequestHandle *handle) | |||
255 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 256 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
256 | "Cleaning up\n"); | 257 | "Cleaning up\n"); |
257 | if (NULL != handle->resp_object) | 258 | if (NULL != handle->resp_object) |
258 | GNUNET_REST_jsonapi_object_delete (handle->resp_object); | 259 | GNUNET_JSONAPI_object_delete (handle->resp_object); |
259 | if (NULL != handle->name) | 260 | if (NULL != handle->name) |
260 | GNUNET_free (handle->name); | 261 | GNUNET_free (handle->name); |
261 | if (NULL != handle->timeout_task) | 262 | if (NULL != handle->timeout_task) |
@@ -403,7 +404,7 @@ namestore_list_response (void *cls, | |||
403 | const struct GNUNET_GNSRECORD_Data *rd) | 404 | const struct GNUNET_GNSRECORD_Data *rd) |
404 | { | 405 | { |
405 | struct RequestHandle *handle = cls; | 406 | struct RequestHandle *handle = cls; |
406 | struct JsonApiResource *json_resource; | 407 | struct GNUNET_JSONAPI_Resource *json_resource; |
407 | struct MHD_Response *resp; | 408 | struct MHD_Response *resp; |
408 | json_t *result_array; | 409 | json_t *result_array; |
409 | json_t *record_obj; | 410 | json_t *record_obj; |
@@ -411,13 +412,13 @@ namestore_list_response (void *cls, | |||
411 | char *result; | 412 | char *result; |
412 | 413 | ||
413 | if (NULL == handle->resp_object) | 414 | if (NULL == handle->resp_object) |
414 | handle->resp_object = GNUNET_REST_jsonapi_object_new (); | 415 | handle->resp_object = GNUNET_JSONAPI_object_new (); |
415 | 416 | ||
416 | if (NULL == rname) | 417 | if (NULL == rname) |
417 | { | 418 | { |
418 | handle->list_it = NULL; | 419 | handle->list_it = NULL; |
419 | //Handle response | 420 | //Handle response |
420 | if (GNUNET_SYSERR == GNUNET_REST_jsonapi_data_serialize (handle->resp_object, &result)) | 421 | if (GNUNET_SYSERR == GNUNET_JSONAPI_data_serialize (handle->resp_object, &result)) |
421 | { | 422 | { |
422 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 423 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
423 | return; | 424 | return; |
@@ -455,12 +456,12 @@ namestore_list_response (void *cls, | |||
455 | 456 | ||
456 | if (0 < json_array_size(result_array)) | 457 | if (0 < json_array_size(result_array)) |
457 | { | 458 | { |
458 | json_resource = GNUNET_REST_jsonapi_resource_new (GNUNET_REST_JSONAPI_NAMESTORE_TYPEINFO, | 459 | json_resource = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_NAMESTORE_TYPEINFO, |
459 | rname); | 460 | rname); |
460 | GNUNET_REST_jsonapi_resource_add_attr (json_resource, | 461 | GNUNET_JSONAPI_resource_add_attr (json_resource, |
461 | GNUNET_REST_JSONAPI_NAMESTORE_RECORD, | 462 | GNUNET_REST_JSONAPI_NAMESTORE_RECORD, |
462 | result_array); | 463 | result_array); |
463 | GNUNET_REST_jsonapi_object_resource_add (handle->resp_object, json_resource); | 464 | GNUNET_JSONAPI_object_resource_add (handle->resp_object, json_resource); |
464 | } | 465 | } |
465 | 466 | ||
466 | json_decref (result_array); | 467 | json_decref (result_array); |
@@ -723,8 +724,8 @@ namestore_create_cont (struct RestConnectionDataHandle *con, | |||
723 | { | 724 | { |
724 | struct RequestHandle *handle = cls; | 725 | struct RequestHandle *handle = cls; |
725 | struct MHD_Response *resp; | 726 | struct MHD_Response *resp; |
726 | struct JsonApiObject *json_obj; | 727 | struct GNUNET_JSONAPI_Object *json_obj; |
727 | struct JsonApiResource *json_res; | 728 | struct GNUNET_JSONAPI_Resource *json_res; |
728 | json_t *name_json; | 729 | json_t *name_json; |
729 | json_t *records_json; | 730 | json_t *records_json; |
730 | char term_data[handle->data_size+1]; | 731 | char term_data[handle->data_size+1]; |
@@ -743,7 +744,7 @@ namestore_create_cont (struct RestConnectionDataHandle *con, | |||
743 | } | 744 | } |
744 | term_data[handle->data_size] = '\0'; | 745 | term_data[handle->data_size] = '\0'; |
745 | memcpy (term_data, handle->data, handle->data_size); | 746 | memcpy (term_data, handle->data, handle->data_size); |
746 | json_obj = GNUNET_REST_jsonapi_object_parse (term_data); | 747 | json_obj = GNUNET_JSONAPI_object_parse (term_data); |
747 | if (NULL == json_obj) | 748 | if (NULL == json_obj) |
748 | { | 749 | { |
749 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 750 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
@@ -752,54 +753,54 @@ namestore_create_cont (struct RestConnectionDataHandle *con, | |||
752 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 753 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
753 | return; | 754 | return; |
754 | } | 755 | } |
755 | if (1 != GNUNET_REST_jsonapi_object_resource_count (json_obj)) | 756 | if (1 != GNUNET_JSONAPI_object_resource_count (json_obj)) |
756 | { | 757 | { |
757 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 758 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
758 | "Cannot create more than 1 resource! (Got %d)\n", | 759 | "Cannot create more than 1 resource! (Got %d)\n", |
759 | GNUNET_REST_jsonapi_object_resource_count (json_obj)); | 760 | GNUNET_JSONAPI_object_resource_count (json_obj)); |
760 | GNUNET_REST_jsonapi_object_delete (json_obj); | 761 | GNUNET_JSONAPI_object_delete (json_obj); |
761 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 762 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
762 | return; | 763 | return; |
763 | } | 764 | } |
764 | json_res = GNUNET_REST_jsonapi_object_get_resource (json_obj, 0); | 765 | json_res = GNUNET_JSONAPI_object_get_resource (json_obj, 0); |
765 | if (GNUNET_NO == GNUNET_REST_jsonapi_resource_check_type (json_res, | 766 | if (GNUNET_NO == GNUNET_JSONAPI_resource_check_type (json_res, |
766 | GNUNET_REST_JSONAPI_NAMESTORE_RECORD)) | 767 | GNUNET_REST_JSONAPI_NAMESTORE_RECORD)) |
767 | { | 768 | { |
768 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 769 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
769 | "Unsupported JSON data type\n"); | 770 | "Unsupported JSON data type\n"); |
770 | GNUNET_REST_jsonapi_object_delete (json_obj); | 771 | GNUNET_JSONAPI_object_delete (json_obj); |
771 | resp = GNUNET_REST_create_json_response (NULL); | 772 | resp = GNUNET_REST_create_json_response (NULL); |
772 | handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); | 773 | handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); |
773 | cleanup_handle (handle); | 774 | cleanup_handle (handle); |
774 | return; | 775 | return; |
775 | } | 776 | } |
776 | name_json = GNUNET_REST_jsonapi_resource_read_attr (json_res, GNUNET_REST_JSONAPI_KEY_ID); | 777 | name_json = GNUNET_JSONAPI_resource_get_id (json_res); |
777 | if (!json_is_string (name_json)) | 778 | if (!json_is_string (name_json)) |
778 | { | 779 | { |
779 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 780 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
780 | "Name property is no string\n"); | 781 | "Name property is no string\n"); |
781 | GNUNET_REST_jsonapi_object_delete (json_obj); | 782 | GNUNET_JSONAPI_object_delete (json_obj); |
782 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 783 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
783 | return; | 784 | return; |
784 | } | 785 | } |
785 | GNUNET_asprintf (&handle->name, "%s", json_string_value (name_json)); | 786 | GNUNET_asprintf (&handle->name, "%s", json_string_value (name_json)); |
786 | records_json = GNUNET_REST_jsonapi_resource_read_attr (json_res, | 787 | records_json = GNUNET_JSONAPI_resource_read_attr (json_res, |
787 | GNUNET_REST_JSONAPI_NAMESTORE_RECORD); | 788 | GNUNET_REST_JSONAPI_NAMESTORE_RECORD); |
788 | if (NULL == records_json) | 789 | if (NULL == records_json) |
789 | { | 790 | { |
790 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 791 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
791 | "No records given\n"); | 792 | "No records given\n"); |
792 | GNUNET_REST_jsonapi_object_delete (json_obj); | 793 | GNUNET_JSONAPI_object_delete (json_obj); |
793 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 794 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
794 | return; | 795 | return; |
795 | } | 796 | } |
796 | if (GNUNET_SYSERR == json_to_gnsrecord (records_json, &handle->rd, &handle->rd_count)) | 797 | if (GNUNET_SYSERR == json_to_gnsrecord (records_json, &handle->rd, &handle->rd_count)) |
797 | { | 798 | { |
798 | GNUNET_REST_jsonapi_object_delete (json_obj); | 799 | GNUNET_JSONAPI_object_delete (json_obj); |
799 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 800 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
800 | return; | 801 | return; |
801 | } | 802 | } |
802 | GNUNET_REST_jsonapi_object_delete (json_obj); | 803 | GNUNET_JSONAPI_object_delete (json_obj); |
803 | 804 | ||
804 | handle->add_qe = GNUNET_NAMESTORE_records_lookup (handle->ns_handle, | 805 | handle->add_qe = GNUNET_NAMESTORE_records_lookup (handle->ns_handle, |
805 | &handle->zone_pkey, | 806 | &handle->zone_pkey, |
@@ -816,34 +817,34 @@ namestore_zkey_response (void *cls, | |||
816 | { | 817 | { |
817 | struct RequestHandle *handle = cls; | 818 | struct RequestHandle *handle = cls; |
818 | struct MHD_Response *resp; | 819 | struct MHD_Response *resp; |
819 | struct JsonApiObject *json_obj; | 820 | struct GNUNET_JSONAPI_Object *json_obj; |
820 | struct JsonApiResource *json_res; | 821 | struct GNUNET_JSONAPI_Resource *json_res; |
821 | json_t *name_json; | 822 | json_t *name_json; |
822 | char* result; | 823 | char* result; |
823 | 824 | ||
824 | handle->reverse_qe = NULL; | 825 | handle->reverse_qe = NULL; |
825 | json_obj = GNUNET_REST_jsonapi_object_new (); | 826 | json_obj = GNUNET_JSONAPI_object_new (); |
826 | if (NULL != label) | 827 | if (NULL != label) |
827 | { | 828 | { |
828 | name_json = json_string (label); | 829 | name_json = json_string (label); |
829 | json_res = GNUNET_REST_jsonapi_resource_new (GNUNET_REST_JSONAPI_NAMESTORE_REVINFO, | 830 | json_res = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_NAMESTORE_REVINFO, |
830 | handle->zkey_str); | 831 | handle->zkey_str); |
831 | GNUNET_REST_jsonapi_resource_add_attr (json_res, | 832 | GNUNET_JSONAPI_resource_add_attr (json_res, |
832 | GNUNET_REST_JSONAPI_NAMESTORE_NAME, | 833 | GNUNET_REST_JSONAPI_NAMESTORE_NAME, |
833 | name_json); | 834 | name_json); |
834 | GNUNET_REST_jsonapi_object_resource_add (json_obj, json_res); | 835 | GNUNET_JSONAPI_object_resource_add (json_obj, json_res); |
835 | json_decref (name_json); | 836 | json_decref (name_json); |
836 | } | 837 | } |
837 | //Handle response | 838 | //Handle response |
838 | if (GNUNET_SYSERR == GNUNET_REST_jsonapi_data_serialize (json_obj, &result)) | 839 | if (GNUNET_SYSERR == GNUNET_JSONAPI_data_serialize (json_obj, &result)) |
839 | { | 840 | { |
840 | GNUNET_REST_jsonapi_object_delete (json_obj); | 841 | GNUNET_JSONAPI_object_delete (json_obj); |
841 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 842 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
842 | return; | 843 | return; |
843 | } | 844 | } |
844 | resp = GNUNET_REST_create_json_response (result); | 845 | resp = GNUNET_REST_create_json_response (result); |
845 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | 846 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); |
846 | GNUNET_REST_jsonapi_object_delete (json_obj); | 847 | GNUNET_JSONAPI_object_delete (json_obj); |
847 | GNUNET_free (result); | 848 | GNUNET_free (result); |
848 | GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle); | 849 | GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle); |
849 | return; | 850 | return; |