diff options
author | Martin Schanzenbach <mschanzenbach@posteo.de> | 2015-03-26 14:57:05 +0000 |
---|---|---|
committer | Martin Schanzenbach <mschanzenbach@posteo.de> | 2015-03-26 14:57:05 +0000 |
commit | b68a1cacd29fb91dee84f736085c48db4ea5acaf (patch) | |
tree | 16b343e23754b0eec27f927e877e0f457955c105 /src/identity | |
parent | 496e08d0b3e7719b4bf63eb9e0a3e0f8f19d6569 (diff) | |
download | gnunet-b68a1cacd29fb91dee84f736085c48db4ea5acaf.tar.gz gnunet-b68a1cacd29fb91dee84f736085c48db4ea5acaf.zip |
-more REST
Diffstat (limited to 'src/identity')
-rw-r--r-- | src/identity/plugin_rest_identity.c | 130 |
1 files changed, 56 insertions, 74 deletions
diff --git a/src/identity/plugin_rest_identity.c b/src/identity/plugin_rest_identity.c index 75c65466c..d62e56db2 100644 --- a/src/identity/plugin_rest_identity.c +++ b/src/identity/plugin_rest_identity.c | |||
@@ -45,6 +45,8 @@ | |||
45 | 45 | ||
46 | #define GNUNET_REST_JSONAPI_IDENTITY_KEY "key" | 46 | #define GNUNET_REST_JSONAPI_IDENTITY_KEY "key" |
47 | 47 | ||
48 | #define GNUNET_REST_JSONAPI_IDENTITY_SUBSYSTEM "subsystem" | ||
49 | |||
48 | /** | 50 | /** |
49 | * @brief struct returned by the initialization function of the plugin | 51 | * @brief struct returned by the initialization function of the plugin |
50 | */ | 52 | */ |
@@ -236,16 +238,16 @@ get_ego_for_subsys (void *cls, | |||
236 | const char *name) | 238 | const char *name) |
237 | { | 239 | { |
238 | struct RequestHandle *handle = cls; | 240 | struct RequestHandle *handle = cls; |
241 | struct JsonApiObject *json_object; | ||
242 | struct JsonApiResource *json_resource; | ||
239 | struct EgoEntry *ego_entry; | 243 | struct EgoEntry *ego_entry; |
240 | struct MHD_Response *resp; | 244 | struct MHD_Response *resp; |
245 | json_t *key_json; | ||
241 | char *result_str; | 246 | char *result_str; |
242 | char *keystring; | 247 | char *keystring; |
243 | json_t *ego_json; | ||
244 | json_t *root_json; | ||
245 | |||
246 | root_json = json_object (); | ||
247 | 248 | ||
248 | //Return all egos | 249 | json_object = GNUNET_REST_jsonapi_object_new (); |
250 | |||
249 | for (ego_entry = handle->ego_head; | 251 | for (ego_entry = handle->ego_head; |
250 | NULL != ego_entry; | 252 | NULL != ego_entry; |
251 | ego_entry = ego_entry->next) | 253 | ego_entry = ego_entry->next) |
@@ -254,26 +256,25 @@ get_ego_for_subsys (void *cls, | |||
254 | continue; | 256 | continue; |
255 | if (NULL == name) | 257 | if (NULL == name) |
256 | continue; | 258 | continue; |
257 | ego_json = json_object (); | 259 | json_resource = GNUNET_REST_jsonapi_resource_new (GNUNET_REST_JSONAPI_IDENTITY_EGO, ego_entry->identifier); |
258 | keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&ego_entry->pk); | 260 | keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&ego_entry->pk); |
259 | json_object_set_new (ego_json, "id", json_string (ego_entry->identifier)); | 261 | key_json = json_string (keystring); |
260 | json_object_set_new (ego_json, "type", json_string (GNUNET_REST_JSONAPI_IDENTITY_EGO)); | 262 | GNUNET_REST_jsonapi_resource_add_attr (json_resource, GNUNET_REST_JSONAPI_IDENTITY_KEY, key_json); |
261 | json_object_set_new (ego_json, "key", json_string (keystring)); | 263 | json_decref (key_json); |
262 | GNUNET_free (keystring); | 264 | GNUNET_free (keystring); |
265 | GNUNET_REST_jsonapi_object_resource_add (json_object, json_resource); | ||
263 | break; | 266 | break; |
264 | } | 267 | } |
265 | if (NULL == ego_json) | 268 | if (0 == GNUNET_REST_jsonapi_object_resource_count (json_object)) |
266 | { | 269 | { |
267 | json_decref (root_json); | 270 | GNUNET_REST_jsonapi_object_delete (json_object); |
268 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 271 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
269 | return; | 272 | return; |
270 | } | 273 | } |
271 | json_object_set (root_json, GNUNET_REST_JSONAPI_KEY_DATA, ego_json); | 274 | GNUNET_REST_jsonapi_data_serialize (json_object, &result_str); |
272 | result_str = json_dumps (root_json, JSON_COMPACT); | ||
273 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); | 275 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); |
274 | json_decref (ego_json); | ||
275 | json_decref (root_json); | ||
276 | resp = GNUNET_REST_create_json_response (result_str); | 276 | resp = GNUNET_REST_create_json_response (result_str); |
277 | GNUNET_REST_jsonapi_object_delete (json_object); | ||
277 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | 278 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); |
278 | GNUNET_free (result_str); | 279 | GNUNET_free (result_str); |
279 | cleanup_handle (handle); | 280 | cleanup_handle (handle); |
@@ -297,7 +298,7 @@ ego_info_response (struct RestConnectionDataHandle *con, | |||
297 | struct EgoEntry *ego_entry; | 298 | struct EgoEntry *ego_entry; |
298 | struct GNUNET_HashCode key; | 299 | struct GNUNET_HashCode key; |
299 | struct MHD_Response *resp; | 300 | struct MHD_Response *resp; |
300 | struct JsonApiResponse *json_response; | 301 | struct JsonApiObject *json_object; |
301 | struct JsonApiResource *json_resource; | 302 | struct JsonApiResource *json_resource; |
302 | json_t *key_str; | 303 | json_t *key_str; |
303 | 304 | ||
@@ -309,7 +310,6 @@ ego_info_response (struct RestConnectionDataHandle *con, | |||
309 | GNUNET_break (0); | 310 | GNUNET_break (0); |
310 | return; | 311 | return; |
311 | } | 312 | } |
312 | json_response = GNUNET_REST_jsonapi_response_new (); | ||
313 | if ( (strlen (EGO_NAMESPACE) == strlen (handle->url) )) { | 313 | if ( (strlen (EGO_NAMESPACE) == strlen (handle->url) )) { |
314 | GNUNET_CRYPTO_hash (URL_PARAM_SUBSYS, strlen (URL_PARAM_SUBSYS), &key); | 314 | GNUNET_CRYPTO_hash (URL_PARAM_SUBSYS, strlen (URL_PARAM_SUBSYS), &key); |
315 | if ( GNUNET_YES == | 315 | if ( GNUNET_YES == |
@@ -330,7 +330,7 @@ ego_info_response (struct RestConnectionDataHandle *con, | |||
330 | } | 330 | } |
331 | } | 331 | } |
332 | } | 332 | } |
333 | json_response = GNUNET_REST_jsonapi_response_new (); | 333 | json_object = GNUNET_REST_jsonapi_object_new (); |
334 | egoname = &handle->url[strlen (EGO_NAMESPACE)+1]; | 334 | egoname = &handle->url[strlen (EGO_NAMESPACE)+1]; |
335 | 335 | ||
336 | if (strlen (EGO_NAMESPACE) == strlen (handle->url)) | 336 | if (strlen (EGO_NAMESPACE) == strlen (handle->url)) |
@@ -353,13 +353,13 @@ ego_info_response (struct RestConnectionDataHandle *con, | |||
353 | key_str); | 353 | key_str); |
354 | json_decref (key_str); | 354 | json_decref (key_str); |
355 | GNUNET_free (keystring); | 355 | GNUNET_free (keystring); |
356 | GNUNET_REST_jsonapi_response_resource_add (json_response, json_resource); | 356 | GNUNET_REST_jsonapi_object_resource_add (json_object, json_resource); |
357 | } | 357 | } |
358 | 358 | ||
359 | GNUNET_REST_jsonapi_data_serialize (json_response, &result_str); | 359 | GNUNET_REST_jsonapi_data_serialize (json_object, &result_str); |
360 | |||
361 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); | 360 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); |
362 | resp = GNUNET_REST_create_json_response (result_str); | 361 | resp = GNUNET_REST_create_json_response (result_str); |
362 | GNUNET_REST_jsonapi_object_delete (json_object); | ||
363 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | 363 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); |
364 | GNUNET_free (result_str); | 364 | GNUNET_free (result_str); |
365 | cleanup_handle (handle); | 365 | cleanup_handle (handle); |
@@ -421,16 +421,14 @@ ego_create_cont (struct RestConnectionDataHandle *con, | |||
421 | const char *url, | 421 | const char *url, |
422 | void *cls) | 422 | void *cls) |
423 | { | 423 | { |
424 | const char* egoname; | ||
425 | struct RequestHandle *handle = cls; | 424 | struct RequestHandle *handle = cls; |
426 | char term_data[handle->data_size+1]; | ||
427 | struct EgoEntry *ego_entry; | 425 | struct EgoEntry *ego_entry; |
428 | struct MHD_Response *resp; | 426 | struct MHD_Response *resp; |
429 | json_t *root_json; | 427 | struct JsonApiObject *json_obj; |
430 | json_t *data_json; | 428 | struct JsonApiResource *json_res; |
431 | json_t *type_json; | ||
432 | json_t *egoname_json; | 429 | json_t *egoname_json; |
433 | json_error_t error; | 430 | char term_data[handle->data_size+1]; |
431 | const char* egoname; | ||
434 | 432 | ||
435 | if (strlen (API_NAMESPACE) != strlen (handle->url)) | 433 | if (strlen (API_NAMESPACE) != strlen (handle->url)) |
436 | { | 434 | { |
@@ -442,43 +440,39 @@ ego_create_cont (struct RestConnectionDataHandle *con, | |||
442 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 440 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
443 | return; | 441 | return; |
444 | } | 442 | } |
445 | |||
446 | term_data[handle->data_size] = '\0'; | 443 | term_data[handle->data_size] = '\0'; |
447 | memcpy (term_data, handle->data, handle->data_size); | 444 | memcpy (term_data, handle->data, handle->data_size); |
448 | root_json = json_loads (term_data, 0, &error); | 445 | json_obj = GNUNET_REST_jsonapi_object_parse (term_data); |
449 | 446 | ||
450 | if ((NULL == root_json) || !json_is_object (root_json)) | 447 | if (NULL == json_obj) |
451 | { | 448 | { |
452 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 449 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
453 | return; | 450 | return; |
454 | } | 451 | } |
455 | data_json = json_object_get (root_json, GNUNET_REST_JSONAPI_KEY_DATA); | 452 | if (1 != GNUNET_REST_jsonapi_object_resource_count (json_obj)) |
456 | if ((NULL == data_json) || !json_is_object (data_json)) | ||
457 | { | 453 | { |
454 | GNUNET_REST_jsonapi_object_delete (json_obj); | ||
458 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 455 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
459 | return; | 456 | return; |
460 | } | 457 | } |
461 | type_json = json_object_get (data_json, "type"); | 458 | |
462 | if (!json_is_string (type_json) || | 459 | json_res = GNUNET_REST_jsonapi_object_get_resource (json_obj, 0); |
463 | (0 != strcmp (GNUNET_REST_JSONAPI_IDENTITY_EGO, json_string_value (type_json)))) | 460 | if (GNUNET_NO == GNUNET_REST_jsonapi_resource_check_type (json_res, GNUNET_REST_JSONAPI_IDENTITY_EGO)) |
464 | { | 461 | { |
465 | json_decref (data_json); | 462 | GNUNET_REST_jsonapi_object_delete (json_obj); |
466 | json_decref (root_json); | ||
467 | resp = GNUNET_REST_create_json_response (NULL); | 463 | resp = GNUNET_REST_create_json_response (NULL); |
468 | handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); | 464 | handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); |
469 | cleanup_handle (handle); | 465 | cleanup_handle (handle); |
470 | return; | 466 | return; |
471 | } | 467 | } |
472 | json_decref (type_json); | 468 | |
473 | egoname_json = json_object_get (data_json, "id"); | 469 | egoname_json = GNUNET_REST_jsonapi_resource_read_attr (json_res, GNUNET_REST_JSONAPI_KEY_ID); |
474 | if (!json_is_string (egoname_json)) | 470 | if (!json_is_string (egoname_json)) |
475 | { | 471 | { |
476 | json_decref (data_json); | 472 | GNUNET_REST_jsonapi_object_delete (json_obj); |
477 | json_decref (root_json); | ||
478 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 473 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
479 | return; | 474 | return; |
480 | } | 475 | } |
481 | |||
482 | egoname = json_string_value (egoname_json); | 476 | egoname = json_string_value (egoname_json); |
483 | for (ego_entry = handle->ego_head; | 477 | for (ego_entry = handle->ego_head; |
484 | NULL != ego_entry; | 478 | NULL != ego_entry; |
@@ -487,8 +481,7 @@ ego_create_cont (struct RestConnectionDataHandle *con, | |||
487 | if (0 == strcasecmp (egoname, ego_entry->identifier)) | 481 | if (0 == strcasecmp (egoname, ego_entry->identifier)) |
488 | { | 482 | { |
489 | json_decref (egoname_json); | 483 | json_decref (egoname_json); |
490 | json_decref (data_json); | 484 | GNUNET_REST_jsonapi_object_delete (json_obj); |
491 | json_decref (root_json); | ||
492 | resp = GNUNET_REST_create_json_response (NULL); | 485 | resp = GNUNET_REST_create_json_response (NULL); |
493 | handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); | 486 | handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); |
494 | cleanup_handle (handle); | 487 | cleanup_handle (handle); |
@@ -497,8 +490,7 @@ ego_create_cont (struct RestConnectionDataHandle *con, | |||
497 | } | 490 | } |
498 | GNUNET_asprintf (&handle->name, "%s", egoname); | 491 | GNUNET_asprintf (&handle->name, "%s", egoname); |
499 | json_decref (egoname_json); | 492 | json_decref (egoname_json); |
500 | json_decref (data_json); | 493 | GNUNET_REST_jsonapi_object_delete (json_obj); |
501 | json_decref (root_json); | ||
502 | handle->op = GNUNET_IDENTITY_create (handle->identity_handle, | 494 | handle->op = GNUNET_IDENTITY_create (handle->identity_handle, |
503 | handle->name, | 495 | handle->name, |
504 | &create_finished, | 496 | &create_finished, |
@@ -510,6 +502,8 @@ subsys_set_cont (struct RestConnectionDataHandle *con, | |||
510 | const char *url, | 502 | const char *url, |
511 | void *cls) | 503 | void *cls) |
512 | { | 504 | { |
505 | struct JsonApiObject *json_obj; | ||
506 | struct JsonApiResource *json_res; | ||
513 | const char *egoname; | 507 | const char *egoname; |
514 | const char *subsys; | 508 | const char *subsys; |
515 | struct RequestHandle *handle = cls; | 509 | struct RequestHandle *handle = cls; |
@@ -517,13 +511,8 @@ subsys_set_cont (struct RestConnectionDataHandle *con, | |||
517 | struct EgoEntry *ego_entry; | 511 | struct EgoEntry *ego_entry; |
518 | struct MHD_Response *resp; | 512 | struct MHD_Response *resp; |
519 | int ego_exists = GNUNET_NO; | 513 | int ego_exists = GNUNET_NO; |
520 | json_t *root_json; | ||
521 | json_t *data_json; | ||
522 | json_t *type_json; | ||
523 | json_t *id_json; | ||
524 | json_t *subsys_json; | 514 | json_t *subsys_json; |
525 | json_error_t error; | 515 | |
526 | |||
527 | if (strlen (API_NAMESPACE) > strlen (handle->url)) | 516 | if (strlen (API_NAMESPACE) > strlen (handle->url)) |
528 | { | 517 | { |
529 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 518 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
@@ -557,54 +546,47 @@ subsys_set_cont (struct RestConnectionDataHandle *con, | |||
557 | 546 | ||
558 | term_data[handle->data_size] = '\0'; | 547 | term_data[handle->data_size] = '\0'; |
559 | memcpy (term_data, handle->data, handle->data_size); | 548 | memcpy (term_data, handle->data, handle->data_size); |
560 | root_json = json_loads (term_data, 0, &error); | 549 | json_obj = GNUNET_REST_jsonapi_object_parse (term_data); |
561 | 550 | ||
562 | if ((NULL == root_json) || !json_is_object (root_json)) | 551 | if (NULL == json_obj) |
563 | { | 552 | { |
564 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 553 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
565 | return; | 554 | return; |
566 | } | 555 | } |
567 | data_json = json_object_get (root_json, "data"); | 556 | if (1 != GNUNET_REST_jsonapi_object_resource_count (json_obj)) |
568 | if (!json_is_object (data_json)) | ||
569 | { | 557 | { |
570 | json_decref (root_json); | 558 | GNUNET_REST_jsonapi_object_delete (json_obj); |
571 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 559 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
572 | return; | 560 | return; |
573 | } | 561 | } |
574 | id_json = json_object_get (data_json, "id"); | 562 | |
575 | if (!json_is_string (id_json) || | 563 | json_res = GNUNET_REST_jsonapi_object_get_resource (json_obj, 0); |
576 | (0 != strcmp (egoname, json_string_value (id_json)))) | 564 | if (GNUNET_NO == GNUNET_REST_jsonapi_resource_check_id (json_res, egoname)) |
577 | { | 565 | { |
578 | json_decref (root_json); | 566 | GNUNET_REST_jsonapi_object_delete (json_obj); |
579 | json_decref (data_json); | ||
580 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 567 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
581 | return; | 568 | return; |
582 | } | 569 | } |
583 | json_decref (id_json); | 570 | |
584 | 571 | if (GNUNET_NO == GNUNET_REST_jsonapi_resource_check_id (json_res, GNUNET_REST_JSONAPI_IDENTITY_EGO)) | |
585 | type_json = json_object_get (data_json, "type"); | ||
586 | if (!json_is_string (type_json) || | ||
587 | (0 != strcmp (GNUNET_REST_JSONAPI_IDENTITY_EGO, json_string_value (type_json)))) | ||
588 | { | 572 | { |
589 | json_decref (root_json); | 573 | GNUNET_REST_jsonapi_object_delete (json_obj); |
590 | json_decref (data_json); | ||
591 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 574 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
592 | return; | 575 | return; |
593 | } | 576 | } |
594 | json_decref (type_json); | ||
595 | 577 | ||
596 | subsys_json = json_object_get (data_json, "subsystem"); | 578 | subsys_json = GNUNET_REST_jsonapi_resource_read_attr (json_res, GNUNET_REST_JSONAPI_IDENTITY_SUBSYSTEM); |
597 | if (!json_is_string (subsys_json)) | 579 | if (!json_is_string (subsys_json)) |
598 | { | 580 | { |
599 | json_decref (root_json); | 581 | json_decref (subsys_json); |
600 | json_decref (data_json); | 582 | GNUNET_REST_jsonapi_object_delete (json_obj); |
601 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 583 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
602 | return; | 584 | return; |
603 | } | 585 | } |
604 | subsys = json_string_value (subsys_json); | 586 | subsys = json_string_value (subsys_json); |
605 | GNUNET_asprintf (&handle->subsys, "%s", subsys); | 587 | GNUNET_asprintf (&handle->subsys, "%s", subsys); |
606 | json_decref (subsys_json); | 588 | json_decref (subsys_json); |
607 | json_decref (root_json); | 589 | GNUNET_REST_jsonapi_object_delete (json_obj); |
608 | handle->op = GNUNET_IDENTITY_set (handle->identity_handle, | 590 | handle->op = GNUNET_IDENTITY_set (handle->identity_handle, |
609 | handle->subsys, | 591 | handle->subsys, |
610 | ego_entry->ego, | 592 | ego_entry->ego, |