aboutsummaryrefslogtreecommitdiff
path: root/src/identity/plugin_rest_identity.c
diff options
context:
space:
mode:
authorMartin Schanzenbach <mschanzenbach@posteo.de>2016-05-02 16:32:36 +0000
committerMartin Schanzenbach <mschanzenbach@posteo.de>2016-05-02 16:32:36 +0000
commitbee208bcd6803829aa26c55a4c8e176a5f2c815f (patch)
tree6974bc9e94a1ec938b5376d2631233d2e75baf7d /src/identity/plugin_rest_identity.c
parentfc9b25be2dbe5392501dbc0b6659c1aa50809bc2 (diff)
downloadgnunet-bee208bcd6803829aa26c55a4c8e176a5f2c815f.tar.gz
gnunet-bee208bcd6803829aa26c55a4c8e176a5f2c815f.zip
- Refactor jsonapi into separate module
Diffstat (limited to 'src/identity/plugin_rest_identity.c')
-rw-r--r--src/identity/plugin_rest_identity.c93
1 files changed, 47 insertions, 46 deletions
diff --git a/src/identity/plugin_rest_identity.c b/src/identity/plugin_rest_identity.c
index 5bed8af55..b827bfb2b 100644
--- a/src/identity/plugin_rest_identity.c
+++ b/src/identity/plugin_rest_identity.c
@@ -28,6 +28,7 @@
28#include "gnunet_rest_plugin.h" 28#include "gnunet_rest_plugin.h"
29#include "gnunet_identity_service.h" 29#include "gnunet_identity_service.h"
30#include "gnunet_rest_lib.h" 30#include "gnunet_rest_lib.h"
31#include "gnunet_jsonapi_lib.h"
31#include "microhttpd.h" 32#include "microhttpd.h"
32#include <jansson.h> 33#include <jansson.h>
33#include "gnunet_signatures.h" 34#include "gnunet_signatures.h"
@@ -296,14 +297,14 @@ get_ego_for_subsys (void *cls,
296 const char *name) 297 const char *name)
297{ 298{
298 struct RequestHandle *handle = cls; 299 struct RequestHandle *handle = cls;
299 struct JsonApiObject *json_object; 300 struct GNUNET_JSONAPI_Object *json_object;
300 struct JsonApiResource *json_resource; 301 struct GNUNET_JSONAPI_Resource *json_resource;
301 struct EgoEntry *ego_entry; 302 struct EgoEntry *ego_entry;
302 struct MHD_Response *resp; 303 struct MHD_Response *resp;
303 json_t *name_json; 304 json_t *name_json;
304 char *result_str; 305 char *result_str;
305 306
306 json_object = GNUNET_REST_jsonapi_object_new (); 307 json_object = GNUNET_JSONAPI_object_new ();
307 308
308 for (ego_entry = handle->ego_head; 309 for (ego_entry = handle->ego_head;
309 NULL != ego_entry; 310 NULL != ego_entry;
@@ -313,27 +314,27 @@ get_ego_for_subsys (void *cls,
313 continue; 314 continue;
314 if (NULL == name) 315 if (NULL == name)
315 continue; 316 continue;
316 json_resource = GNUNET_REST_jsonapi_resource_new 317 json_resource = GNUNET_JSONAPI_resource_new
317 (GNUNET_REST_JSONAPI_IDENTITY_EGO, ego_entry->keystring); 318 (GNUNET_REST_JSONAPI_IDENTITY_EGO, ego_entry->keystring);
318 name_json = json_string (ego_entry->identifier); 319 name_json = json_string (ego_entry->identifier);
319 GNUNET_REST_jsonapi_resource_add_attr (json_resource, 320 GNUNET_JSONAPI_resource_add_attr (json_resource,
320 GNUNET_REST_JSONAPI_IDENTITY_NAME, 321 GNUNET_REST_JSONAPI_IDENTITY_NAME,
321 name_json); 322 name_json);
322 json_decref (name_json); 323 json_decref (name_json);
323 GNUNET_REST_jsonapi_object_resource_add (json_object, json_resource); 324 GNUNET_JSONAPI_object_resource_add (json_object, json_resource);
324 break; 325 break;
325 } 326 }
326 if (0 == GNUNET_REST_jsonapi_object_resource_count (json_object)) 327 if (0 == GNUNET_JSONAPI_object_resource_count (json_object))
327 { 328 {
328 GNUNET_REST_jsonapi_object_delete (json_object); 329 GNUNET_JSONAPI_object_delete (json_object);
329 handle->emsg = GNUNET_strdup("No identity matches results!"); 330 handle->emsg = GNUNET_strdup("No identity matches results!");
330 GNUNET_SCHEDULER_add_now (&do_error, handle); 331 GNUNET_SCHEDULER_add_now (&do_error, handle);
331 return; 332 return;
332 } 333 }
333 GNUNET_REST_jsonapi_data_serialize (json_object, &result_str); 334 GNUNET_JSONAPI_data_serialize (json_object, &result_str);
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); 335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
335 resp = GNUNET_REST_create_json_response (result_str); 336 resp = GNUNET_REST_create_json_response (result_str);
336 GNUNET_REST_jsonapi_object_delete (json_object); 337 GNUNET_JSONAPI_object_delete (json_object);
337 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); 338 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
338 GNUNET_free (result_str); 339 GNUNET_free (result_str);
339 cleanup_handle (handle); 340 cleanup_handle (handle);
@@ -359,8 +360,8 @@ ego_info_response (struct RestConnectionDataHandle *con,
359 struct EgoEntry *ego_entry; 360 struct EgoEntry *ego_entry;
360 struct GNUNET_HashCode key; 361 struct GNUNET_HashCode key;
361 struct MHD_Response *resp; 362 struct MHD_Response *resp;
362 struct JsonApiObject *json_object; 363 struct GNUNET_JSONAPI_Object *json_object;
363 struct JsonApiResource *json_resource; 364 struct GNUNET_JSONAPI_Resource *json_resource;
364 json_t *name_str; 365 json_t *name_str;
365 366
366 if (GNUNET_NO == GNUNET_REST_namespace_match (handle->url, GNUNET_REST_API_NS_IDENTITY)) 367 if (GNUNET_NO == GNUNET_REST_namespace_match (handle->url, GNUNET_REST_API_NS_IDENTITY))
@@ -409,7 +410,7 @@ ego_info_response (struct RestConnectionDataHandle *con,
409 } 410 }
410 } 411 }
411 412
412 json_object = GNUNET_REST_jsonapi_object_new (); 413 json_object = GNUNET_JSONAPI_object_new ();
413 414
414 //Return all egos 415 //Return all egos
415 for (ego_entry = handle->ego_head; 416 for (ego_entry = handle->ego_head;
@@ -418,27 +419,27 @@ ego_info_response (struct RestConnectionDataHandle *con,
418 { 419 {
419 if ( (NULL != egoname) && (0 != strcmp (egoname, ego_entry->identifier)) ) 420 if ( (NULL != egoname) && (0 != strcmp (egoname, ego_entry->identifier)) )
420 continue; 421 continue;
421 json_resource = GNUNET_REST_jsonapi_resource_new (GNUNET_REST_JSONAPI_IDENTITY_EGO, 422 json_resource = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_IDENTITY_EGO,
422 ego_entry->keystring); 423 ego_entry->keystring);
423 name_str = json_string (ego_entry->identifier); 424 name_str = json_string (ego_entry->identifier);
424 GNUNET_REST_jsonapi_resource_add_attr ( 425 GNUNET_JSONAPI_resource_add_attr (
425 json_resource, 426 json_resource,
426 GNUNET_REST_JSONAPI_IDENTITY_NAME, 427 GNUNET_REST_JSONAPI_IDENTITY_NAME,
427 name_str); 428 name_str);
428 json_decref (name_str); 429 json_decref (name_str);
429 GNUNET_REST_jsonapi_object_resource_add (json_object, json_resource); 430 GNUNET_JSONAPI_object_resource_add (json_object, json_resource);
430 } 431 }
431 if (0 == GNUNET_REST_jsonapi_object_resource_count (json_object)) 432 if (0 == GNUNET_JSONAPI_object_resource_count (json_object))
432 { 433 {
433 GNUNET_REST_jsonapi_object_delete (json_object); 434 GNUNET_JSONAPI_object_delete (json_object);
434 handle->emsg = GNUNET_strdup ("No identities found!"); 435 handle->emsg = GNUNET_strdup ("No identities found!");
435 GNUNET_SCHEDULER_add_now (&do_error, handle); 436 GNUNET_SCHEDULER_add_now (&do_error, handle);
436 return; 437 return;
437 } 438 }
438 GNUNET_REST_jsonapi_data_serialize (json_object, &result_str); 439 GNUNET_JSONAPI_data_serialize (json_object, &result_str);
439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); 440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
440 resp = GNUNET_REST_create_json_response (result_str); 441 resp = GNUNET_REST_create_json_response (result_str);
441 GNUNET_REST_jsonapi_object_delete (json_object); 442 GNUNET_JSONAPI_object_delete (json_object);
442 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); 443 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
443 GNUNET_free (result_str); 444 GNUNET_free (result_str);
444 cleanup_handle (handle); 445 cleanup_handle (handle);
@@ -483,8 +484,8 @@ ego_create_cont (struct RestConnectionDataHandle *con,
483 struct RequestHandle *handle = cls; 484 struct RequestHandle *handle = cls;
484 struct EgoEntry *ego_entry; 485 struct EgoEntry *ego_entry;
485 struct MHD_Response *resp; 486 struct MHD_Response *resp;
486 struct JsonApiObject *json_obj; 487 struct GNUNET_JSONAPI_Object *json_obj;
487 struct JsonApiResource *json_res; 488 struct GNUNET_JSONAPI_Resource *json_res;
488 json_t *egoname_json; 489 json_t *egoname_json;
489 const char* egoname; 490 const char* egoname;
490 char term_data[handle->data_size+1]; 491 char term_data[handle->data_size+1];
@@ -503,32 +504,32 @@ ego_create_cont (struct RestConnectionDataHandle *con,
503 } 504 }
504 term_data[handle->data_size] = '\0'; 505 term_data[handle->data_size] = '\0';
505 memcpy (term_data, handle->data, handle->data_size); 506 memcpy (term_data, handle->data, handle->data_size);
506 json_obj = GNUNET_REST_jsonapi_object_parse (term_data); 507 json_obj = GNUNET_JSONAPI_object_parse (term_data);
507 if (NULL == json_obj) 508 if (NULL == json_obj)
508 { 509 {
509 GNUNET_SCHEDULER_add_now (&do_error, handle); 510 GNUNET_SCHEDULER_add_now (&do_error, handle);
510 return; 511 return;
511 } 512 }
512 if (1 != GNUNET_REST_jsonapi_object_resource_count (json_obj)) 513 if (1 != GNUNET_JSONAPI_object_resource_count (json_obj))
513 { 514 {
514 GNUNET_REST_jsonapi_object_delete (json_obj); 515 GNUNET_JSONAPI_object_delete (json_obj);
515 handle->emsg = GNUNET_strdup ("Provided resource count invalid"); 516 handle->emsg = GNUNET_strdup ("Provided resource count invalid");
516 GNUNET_SCHEDULER_add_now (&do_error, handle); 517 GNUNET_SCHEDULER_add_now (&do_error, handle);
517 return; 518 return;
518 } 519 }
519 json_res = GNUNET_REST_jsonapi_object_get_resource (json_obj, 0); 520 json_res = GNUNET_JSONAPI_object_get_resource (json_obj, 0);
520 if (GNUNET_NO == GNUNET_REST_jsonapi_resource_check_type (json_res, GNUNET_REST_JSONAPI_IDENTITY_EGO)) 521 if (GNUNET_NO == GNUNET_JSONAPI_resource_check_type (json_res, GNUNET_REST_JSONAPI_IDENTITY_EGO))
521 { 522 {
522 GNUNET_REST_jsonapi_object_delete (json_obj); 523 GNUNET_JSONAPI_object_delete (json_obj);
523 resp = GNUNET_REST_create_json_response (NULL); 524 resp = GNUNET_REST_create_json_response (NULL);
524 handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); 525 handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT);
525 cleanup_handle (handle); 526 cleanup_handle (handle);
526 return; 527 return;
527 } 528 }
528 egoname_json = GNUNET_REST_jsonapi_resource_read_attr (json_res, GNUNET_REST_JSONAPI_IDENTITY_NAME); 529 egoname_json = GNUNET_JSONAPI_resource_read_attr (json_res, GNUNET_REST_JSONAPI_IDENTITY_NAME);
529 if (!json_is_string (egoname_json)) 530 if (!json_is_string (egoname_json))
530 { 531 {
531 GNUNET_REST_jsonapi_object_delete (json_obj); 532 GNUNET_JSONAPI_object_delete (json_obj);
532 handle->emsg = GNUNET_strdup ("No name provided"); 533 handle->emsg = GNUNET_strdup ("No name provided");
533 GNUNET_SCHEDULER_add_now (&do_error, handle); 534 GNUNET_SCHEDULER_add_now (&do_error, handle);
534 return; 535 return;
@@ -540,7 +541,7 @@ ego_create_cont (struct RestConnectionDataHandle *con,
540 { 541 {
541 if (0 == strcasecmp (egoname, ego_entry->identifier)) 542 if (0 == strcasecmp (egoname, ego_entry->identifier))
542 { 543 {
543 GNUNET_REST_jsonapi_object_delete (json_obj); 544 GNUNET_JSONAPI_object_delete (json_obj);
544 resp = GNUNET_REST_create_json_response (NULL); 545 resp = GNUNET_REST_create_json_response (NULL);
545 handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); 546 handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT);
546 cleanup_handle (handle); 547 cleanup_handle (handle);
@@ -548,7 +549,7 @@ ego_create_cont (struct RestConnectionDataHandle *con,
548 } 549 }
549 } 550 }
550 GNUNET_asprintf (&handle->name, "%s", egoname); 551 GNUNET_asprintf (&handle->name, "%s", egoname);
551 GNUNET_REST_jsonapi_object_delete (json_obj); 552 GNUNET_JSONAPI_object_delete (json_obj);
552 handle->op = GNUNET_IDENTITY_create (handle->identity_handle, 553 handle->op = GNUNET_IDENTITY_create (handle->identity_handle,
553 handle->name, 554 handle->name,
554 &do_finished, 555 &do_finished,
@@ -568,8 +569,8 @@ ego_edit_cont (struct RestConnectionDataHandle *con,
568 const char *url, 569 const char *url,
569 void *cls) 570 void *cls)
570{ 571{
571 struct JsonApiObject *json_obj; 572 struct GNUNET_JSONAPI_Object *json_obj;
572 struct JsonApiResource *json_res; 573 struct GNUNET_JSONAPI_Resource *json_res;
573 struct RequestHandle *handle = cls; 574 struct RequestHandle *handle = cls;
574 struct EgoEntry *ego_entry; 575 struct EgoEntry *ego_entry;
575 struct MHD_Response *resp; 576 struct MHD_Response *resp;
@@ -617,7 +618,7 @@ ego_edit_cont (struct RestConnectionDataHandle *con,
617 618
618 term_data[handle->data_size] = '\0'; 619 term_data[handle->data_size] = '\0';
619 memcpy (term_data, handle->data, handle->data_size); 620 memcpy (term_data, handle->data, handle->data_size);
620 json_obj = GNUNET_REST_jsonapi_object_parse (term_data); 621 json_obj = GNUNET_JSONAPI_object_parse (term_data);
621 622
622 if (NULL == json_obj) 623 if (NULL == json_obj)
623 { 624 {
@@ -626,25 +627,25 @@ ego_edit_cont (struct RestConnectionDataHandle *con,
626 return; 627 return;
627 } 628 }
628 629
629 if (1 != GNUNET_REST_jsonapi_object_resource_count (json_obj)) 630 if (1 != GNUNET_JSONAPI_object_resource_count (json_obj))
630 { 631 {
631 GNUNET_REST_jsonapi_object_delete (json_obj); 632 GNUNET_JSONAPI_object_delete (json_obj);
632 handle->emsg = GNUNET_strdup ("Resource amount invalid"); 633 handle->emsg = GNUNET_strdup ("Resource amount invalid");
633 GNUNET_SCHEDULER_add_now (&do_error, handle); 634 GNUNET_SCHEDULER_add_now (&do_error, handle);
634 return; 635 return;
635 } 636 }
636 json_res = GNUNET_REST_jsonapi_object_get_resource (json_obj, 0); 637 json_res = GNUNET_JSONAPI_object_get_resource (json_obj, 0);
637 638
638 if (GNUNET_NO == GNUNET_REST_jsonapi_resource_check_type (json_res, GNUNET_REST_JSONAPI_IDENTITY_EGO)) 639 if (GNUNET_NO == GNUNET_JSONAPI_resource_check_type (json_res, GNUNET_REST_JSONAPI_IDENTITY_EGO))
639 { 640 {
640 GNUNET_REST_jsonapi_object_delete (json_obj); 641 GNUNET_JSONAPI_object_delete (json_obj);
641 handle->emsg = GNUNET_strdup ("Resource type invalid"); 642 handle->emsg = GNUNET_strdup ("Resource type invalid");
642 GNUNET_SCHEDULER_add_now (&do_error, handle); 643 GNUNET_SCHEDULER_add_now (&do_error, handle);
643 return; 644 return;
644 } 645 }
645 646
646 //This is a rename 647 //This is a rename
647 name_json = GNUNET_REST_jsonapi_resource_read_attr (json_res, 648 name_json = GNUNET_JSONAPI_resource_read_attr (json_res,
648 GNUNET_REST_JSONAPI_IDENTITY_NEWNAME); 649 GNUNET_REST_JSONAPI_IDENTITY_NEWNAME);
649 if ((NULL != name_json) && json_is_string (name_json)) 650 if ((NULL != name_json) && json_is_string (name_json))
650 { 651 {
@@ -657,7 +658,7 @@ ego_edit_cont (struct RestConnectionDataHandle *con,
657 0 != strcasecmp (keystring, ego_entry->keystring)) 658 0 != strcasecmp (keystring, ego_entry->keystring))
658 { 659 {
659 //Ego with same name not allowed 660 //Ego with same name not allowed
660 GNUNET_REST_jsonapi_object_delete (json_obj); 661 GNUNET_JSONAPI_object_delete (json_obj);
661 resp = GNUNET_REST_create_json_response (NULL); 662 resp = GNUNET_REST_create_json_response (NULL);
662 handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); 663 handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT);
663 cleanup_handle (handle); 664 cleanup_handle (handle);
@@ -669,17 +670,17 @@ ego_edit_cont (struct RestConnectionDataHandle *con,
669 newname, 670 newname,
670 &do_finished, 671 &do_finished,
671 handle); 672 handle);
672 GNUNET_REST_jsonapi_object_delete (json_obj); 673 GNUNET_JSONAPI_object_delete (json_obj);
673 return; 674 return;
674 } 675 }
675 676
676 //Set subsystem 677 //Set subsystem
677 subsys_json = GNUNET_REST_jsonapi_resource_read_attr (json_res, GNUNET_REST_JSONAPI_IDENTITY_SUBSYSTEM); 678 subsys_json = GNUNET_JSONAPI_resource_read_attr (json_res, GNUNET_REST_JSONAPI_IDENTITY_SUBSYSTEM);
678 if ( (NULL != subsys_json) && json_is_string (subsys_json)) 679 if ( (NULL != subsys_json) && json_is_string (subsys_json))
679 { 680 {
680 subsys = json_string_value (subsys_json); 681 subsys = json_string_value (subsys_json);
681 GNUNET_asprintf (&handle->subsys, "%s", subsys); 682 GNUNET_asprintf (&handle->subsys, "%s", subsys);
682 GNUNET_REST_jsonapi_object_delete (json_obj); 683 GNUNET_JSONAPI_object_delete (json_obj);
683 handle->op = GNUNET_IDENTITY_set (handle->identity_handle, 684 handle->op = GNUNET_IDENTITY_set (handle->identity_handle,
684 handle->subsys, 685 handle->subsys,
685 ego_entry->ego, 686 ego_entry->ego,
@@ -687,7 +688,7 @@ ego_edit_cont (struct RestConnectionDataHandle *con,
687 handle); 688 handle);
688 return; 689 return;
689 } 690 }
690 GNUNET_REST_jsonapi_object_delete (json_obj); 691 GNUNET_JSONAPI_object_delete (json_obj);
691 handle->emsg = GNUNET_strdup ("Subsystem not provided"); 692 handle->emsg = GNUNET_strdup ("Subsystem not provided");
692 GNUNET_SCHEDULER_add_now (&do_error, handle); 693 GNUNET_SCHEDULER_add_now (&do_error, handle);
693} 694}