diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/gns/plugin_rest_gns.c | 188 | ||||
-rw-r--r-- | src/identity/plugin_rest_identity.c | 229 | ||||
-rwxr-xr-x | src/identity/test_plugin_identity_rest.sh | 252 |
3 files changed, 486 insertions, 183 deletions
diff --git a/src/gns/plugin_rest_gns.c b/src/gns/plugin_rest_gns.c index 2b729db54..82d62744c 100644 --- a/src/gns/plugin_rest_gns.c +++ b/src/gns/plugin_rest_gns.c | |||
@@ -17,6 +17,7 @@ | |||
17 | */ | 17 | */ |
18 | /** | 18 | /** |
19 | * @author Martin Schanzenbach | 19 | * @author Martin Schanzenbach |
20 | * @author Philippe Buschmann | ||
20 | * @file gns/plugin_rest_gns.c | 21 | * @file gns/plugin_rest_gns.c |
21 | * @brief GNUnet GNS REST plugin | 22 | * @brief GNUnet GNS REST plugin |
22 | * | 23 | * |
@@ -38,6 +39,8 @@ | |||
38 | 39 | ||
39 | #define GNUNET_REST_JSONAPI_GNS_RECORD_TYPE "record_type" | 40 | #define GNUNET_REST_JSONAPI_GNS_RECORD_TYPE "record_type" |
40 | 41 | ||
42 | #define GNUNET_REST_PARAMETER_GNS_NAME "name" | ||
43 | |||
41 | #define GNUNET_REST_JSONAPI_GNS_TYPEINFO "gns_name" | 44 | #define GNUNET_REST_JSONAPI_GNS_TYPEINFO "gns_name" |
42 | 45 | ||
43 | #define GNUNET_REST_JSONAPI_GNS_RECORD "records" | 46 | #define GNUNET_REST_JSONAPI_GNS_RECORD "records" |
@@ -162,6 +165,11 @@ struct LookupHandle | |||
162 | */ | 165 | */ |
163 | int response_code; | 166 | int response_code; |
164 | 167 | ||
168 | /** | ||
169 | * HTTP response code | ||
170 | */ | ||
171 | char* emsg; | ||
172 | |||
165 | }; | 173 | }; |
166 | 174 | ||
167 | 175 | ||
@@ -180,6 +188,8 @@ cleanup_handle (struct LookupHandle *handle) | |||
180 | 188 | ||
181 | if (NULL != handle->name) | 189 | if (NULL != handle->name) |
182 | GNUNET_free (handle->name); | 190 | GNUNET_free (handle->name); |
191 | if (NULL != handle->emsg) | ||
192 | GNUNET_free (handle->emsg); | ||
183 | if (NULL != handle->el) | 193 | if (NULL != handle->el) |
184 | { | 194 | { |
185 | GNUNET_IDENTITY_ego_lookup_cancel (handle->el); | 195 | GNUNET_IDENTITY_ego_lookup_cancel (handle->el); |
@@ -225,10 +235,18 @@ do_error (void *cls) | |||
225 | { | 235 | { |
226 | struct LookupHandle *handle = cls; | 236 | struct LookupHandle *handle = cls; |
227 | struct MHD_Response *resp; | 237 | struct MHD_Response *resp; |
238 | char *json_error; | ||
228 | 239 | ||
229 | resp = GNUNET_REST_create_response (NULL); | 240 | if (NULL == handle->emsg) |
241 | handle->emsg = GNUNET_strdup("Unknown Error"); | ||
242 | |||
243 | GNUNET_asprintf (&json_error, "{\"error\": \"%s\"}", handle->emsg); | ||
244 | handle->response_code = MHD_HTTP_OK; | ||
245 | |||
246 | resp = GNUNET_REST_create_response (json_error); | ||
230 | handle->proc (handle->proc_cls, resp, handle->response_code); | 247 | handle->proc (handle->proc_cls, resp, handle->response_code); |
231 | cleanup_handle (handle); | 248 | cleanup_handle (handle); |
249 | GNUNET_free(json_error); | ||
232 | } | 250 | } |
233 | 251 | ||
234 | 252 | ||
@@ -294,16 +312,12 @@ process_lookup_result (void *cls, uint32_t rd_count, | |||
294 | { | 312 | { |
295 | struct LookupHandle *handle = cls; | 313 | struct LookupHandle *handle = cls; |
296 | struct MHD_Response *resp; | 314 | struct MHD_Response *resp; |
297 | struct GNUNET_JSONAPI_Document *json_document; | ||
298 | struct GNUNET_JSONAPI_Resource *json_resource; | ||
299 | uint32_t i; | 315 | uint32_t i; |
300 | char *result; | 316 | char *result; |
301 | json_t *result_array; | 317 | json_t *result_array; |
302 | json_t *record_obj; | 318 | json_t *record_obj; |
303 | 319 | ||
304 | result_array = json_array(); | 320 | result_array = json_array(); |
305 | json_document = GNUNET_JSONAPI_document_new (); | ||
306 | json_resource = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_GNS_TYPEINFO, handle->name); | ||
307 | handle->lookup_request = NULL; | 321 | handle->lookup_request = NULL; |
308 | for (i=0; i<rd_count; i++) | 322 | for (i=0; i<rd_count; i++) |
309 | { | 323 | { |
@@ -314,17 +328,12 @@ process_lookup_result (void *cls, uint32_t rd_count, | |||
314 | json_array_append (result_array, record_obj); | 328 | json_array_append (result_array, record_obj); |
315 | json_decref (record_obj); | 329 | json_decref (record_obj); |
316 | } | 330 | } |
317 | GNUNET_JSONAPI_resource_add_attr (json_resource, | 331 | result = json_dumps(result_array, 0); |
318 | GNUNET_REST_JSONAPI_GNS_RECORD, | ||
319 | result_array); | ||
320 | GNUNET_JSONAPI_document_resource_add (json_document, json_resource); | ||
321 | GNUNET_JSONAPI_document_serialize (json_document, &result); | ||
322 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result); | 332 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result); |
323 | json_decref (result_array); | ||
324 | GNUNET_JSONAPI_document_delete (json_document); | ||
325 | resp = GNUNET_REST_create_response (result); | 333 | resp = GNUNET_REST_create_response (result); |
326 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | 334 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); |
327 | GNUNET_free (result); | 335 | GNUNET_free (result); |
336 | json_decref (result_array); | ||
328 | cleanup_handle (handle); | 337 | cleanup_handle (handle); |
329 | } | 338 | } |
330 | 339 | ||
@@ -356,6 +365,7 @@ lookup_with_public_key (struct LookupHandle *handle) | |||
356 | } | 365 | } |
357 | else | 366 | else |
358 | { | 367 | { |
368 | handle->emsg = GNUNET_strdup("Parameter name is missing"); | ||
359 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 369 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
360 | return; | 370 | return; |
361 | } | 371 | } |
@@ -380,6 +390,7 @@ identity_zone_cb (void *cls, | |||
380 | { | 390 | { |
381 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 391 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
382 | _("Ego for not found, cannot perform lookup.\n")); | 392 | _("Ego for not found, cannot perform lookup.\n")); |
393 | handle->emsg = GNUNET_strdup ("Ego for not found, cannot perform lookup."); | ||
383 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 394 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
384 | return; | 395 | return; |
385 | } | 396 | } |
@@ -418,6 +429,7 @@ identity_master_cb (void *cls, | |||
418 | { | 429 | { |
419 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 430 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
420 | _("Ego for `gns-master' not found, cannot perform lookup. Did you run gnunet-gns-import.sh?\n")); | 431 | _("Ego for `gns-master' not found, cannot perform lookup. Did you run gnunet-gns-import.sh?\n")); |
432 | handle->emsg = GNUNET_strdup("Ego for `gns-master' not found, cannot perform lookup. Did you run gnunet-gns-import.sh?"); | ||
421 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 433 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
422 | return; | 434 | return; |
423 | } | 435 | } |
@@ -436,35 +448,10 @@ identity_master_cb (void *cls, | |||
436 | } | 448 | } |
437 | 449 | ||
438 | /** | 450 | /** |
439 | * Parse REST uri for name and record type | 451 | * Handle get request |
440 | * | 452 | * |
441 | * @param url Url to parse | 453 | * @param handle the lookup handle |
442 | * @param handle lookup handle to populate | ||
443 | * @return GNUNET_SYSERR on error | ||
444 | */ | 454 | */ |
445 | static int | ||
446 | parse_url (const char *url, struct LookupHandle *handle) | ||
447 | { | ||
448 | char *name; | ||
449 | char tmp_url[strlen(url)+1]; | ||
450 | char *tok; | ||
451 | |||
452 | strcpy (tmp_url, url); | ||
453 | tok = strtok ((char*)tmp_url, "/"); | ||
454 | if (NULL == tok) | ||
455 | return GNUNET_SYSERR; | ||
456 | name = strtok (NULL, "/"); | ||
457 | if (NULL == name) | ||
458 | return GNUNET_SYSERR; | ||
459 | GNUNET_asprintf (&handle->name, | ||
460 | "%s", | ||
461 | name); | ||
462 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
463 | "Got name: %s\n", handle->name); | ||
464 | return GNUNET_OK; | ||
465 | } | ||
466 | |||
467 | |||
468 | static void | 455 | static void |
469 | get_gns_cont (struct GNUNET_REST_RequestHandle *conndata_handle, | 456 | get_gns_cont (struct GNUNET_REST_RequestHandle *conndata_handle, |
470 | const char* url, | 457 | const char* url, |
@@ -472,40 +459,48 @@ get_gns_cont (struct GNUNET_REST_RequestHandle *conndata_handle, | |||
472 | { | 459 | { |
473 | struct LookupHandle *handle = cls; | 460 | struct LookupHandle *handle = cls; |
474 | struct GNUNET_HashCode key; | 461 | struct GNUNET_HashCode key; |
462 | long int enum_test; | ||
463 | char *temp_val; | ||
475 | 464 | ||
476 | //parse name and type from url | 465 | //check for /gns otherwise 404 |
477 | if (GNUNET_OK != parse_url (url, handle)) | 466 | if (strlen (GNUNET_REST_API_NS_GNS) > strlen (url)) |
478 | { | 467 | { |
479 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error parsing url...\n"); | 468 | handle->emsg = GNUNET_strdup("Wrong URL"); |
480 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 469 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
481 | return; | 470 | return; |
482 | } | 471 | } |
483 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 472 | |
484 | "Connecting...\n"); | 473 | //connect to gns |
474 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connecting...\n"); | ||
485 | handle->gns = GNUNET_GNS_connect (cfg); | 475 | handle->gns = GNUNET_GNS_connect (cfg); |
486 | handle->identity = GNUNET_IDENTITY_connect (cfg, NULL, NULL); | 476 | handle->identity = GNUNET_IDENTITY_connect (cfg, NULL, NULL); |
487 | handle->timeout_task = GNUNET_SCHEDULER_add_delayed (handle->timeout, | 477 | handle->timeout_task = GNUNET_SCHEDULER_add_delayed (handle->timeout, |
488 | &do_error, handle); | 478 | &do_error, handle); |
489 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 479 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connected\n"); |
490 | "Connected\n"); | ||
491 | if (NULL == handle->gns) | 480 | if (NULL == handle->gns) |
492 | { | 481 | { |
493 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 482 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Connecting to GNS failed\n"); |
494 | "Connecting to GNS failed\n"); | 483 | handle->emsg = GNUNET_strdup("Connecting to GNS failed"); |
495 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 484 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
496 | return; | 485 | return; |
497 | } | 486 | } |
498 | GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_OPTIONS, | 487 | |
499 | strlen (GNUNET_REST_JSONAPI_GNS_OPTIONS), | 488 | //check parameter name -> BAD_REQUEST |
500 | &key); | 489 | GNUNET_CRYPTO_hash (GNUNET_REST_PARAMETER_GNS_NAME, |
501 | handle->options = GNUNET_GNS_LO_DEFAULT; | 490 | strlen (GNUNET_REST_PARAMETER_GNS_NAME), |
502 | if ( GNUNET_YES == | 491 | &key); |
503 | GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map, | 492 | if ( GNUNET_NO |
504 | &key) ) | 493 | == GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map, |
494 | &key)) | ||
505 | { | 495 | { |
506 | handle->options = GNUNET_GNS_LO_DEFAULT;//TODO(char*) GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map, | 496 | handle->emsg = GNUNET_strdup("Parameter name is missing"); |
507 | //&key); | 497 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
498 | return; | ||
508 | } | 499 | } |
500 | handle->name = GNUNET_strdup(GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map, | ||
501 | &key)); | ||
502 | |||
503 | //check parameter record_type, optional | ||
509 | GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_RECORD_TYPE, | 504 | GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_RECORD_TYPE, |
510 | strlen (GNUNET_REST_JSONAPI_GNS_RECORD_TYPE), | 505 | strlen (GNUNET_REST_JSONAPI_GNS_RECORD_TYPE), |
511 | &key); | 506 | &key); |
@@ -513,34 +508,66 @@ get_gns_cont (struct GNUNET_REST_RequestHandle *conndata_handle, | |||
513 | GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map, | 508 | GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map, |
514 | &key) ) | 509 | &key) ) |
515 | { | 510 | { |
516 | handle->type = GNUNET_GNSRECORD_typename_to_number | 511 | handle->type = GNUNET_GNSRECORD_typename_to_number( |
517 | (GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map, | 512 | GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map, |
518 | &key)); | 513 | &key)); |
519 | } | 514 | } |
520 | else | 515 | else |
516 | { | ||
521 | handle->type = GNUNET_GNSRECORD_TYPE_ANY; | 517 | handle->type = GNUNET_GNSRECORD_TYPE_ANY; |
518 | } | ||
522 | 519 | ||
520 | //check parameter options, optional | ||
521 | GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_OPTIONS, | ||
522 | strlen (GNUNET_REST_JSONAPI_GNS_OPTIONS), | ||
523 | &key); | ||
524 | handle->options = GNUNET_GNS_LO_DEFAULT; | ||
525 | if ( GNUNET_YES | ||
526 | == GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map, | ||
527 | &key)) | ||
528 | { | ||
529 | temp_val = GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map, &key); | ||
530 | if (1 < strlen(temp_val)) | ||
531 | { | ||
532 | handle->options = GNUNET_GNS_LO_DEFAULT; | ||
533 | } | ||
534 | else | ||
535 | { | ||
536 | //atoi because no valid conversion is default local option | ||
537 | enum_test = atoi(temp_val); | ||
538 | if (2 < enum_test) | ||
539 | handle->options = GNUNET_GNS_LO_DEFAULT; | ||
540 | else | ||
541 | handle->options = enum_test; | ||
542 | } | ||
543 | } | ||
544 | else | ||
545 | handle->options = GNUNET_GNS_LO_DEFAULT; | ||
546 | |||
547 | //check parameter pkey, shortcut to lookup | ||
523 | GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_PKEY, | 548 | GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_PKEY, |
524 | strlen (GNUNET_REST_JSONAPI_GNS_PKEY), | 549 | strlen (GNUNET_REST_JSONAPI_GNS_PKEY), |
525 | &key); | 550 | &key); |
526 | if ( GNUNET_YES == | 551 | if ( GNUNET_YES |
527 | GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map, | 552 | == GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map, |
528 | &key) ) | 553 | &key)) |
529 | { | 554 | { |
530 | handle->pkey_str = GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map, | 555 | handle->pkey_str = GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map, |
531 | &key); | 556 | &key); |
532 | GNUNET_assert (NULL != handle->pkey_str); | 557 | GNUNET_assert(NULL != handle->pkey_str); |
533 | if (GNUNET_OK != | 558 | if (GNUNET_OK |
534 | GNUNET_CRYPTO_ecdsa_public_key_from_string (handle->pkey_str, | 559 | != GNUNET_CRYPTO_ecdsa_public_key_from_string ( |
535 | strlen(handle->pkey_str), | 560 | handle->pkey_str, strlen (handle->pkey_str), &(handle->pkey))) |
536 | &(handle->pkey))) | ||
537 | { | 561 | { |
562 | handle->emsg = GNUNET_strdup("Parameter pkey has a wrong format"); | ||
538 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 563 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
539 | return; | 564 | return; |
540 | } | 565 | } |
541 | lookup_with_public_key (handle); | 566 | lookup_with_public_key (handle); |
542 | return; | 567 | return; |
543 | } | 568 | } |
569 | |||
570 | //check parameter ego, lookup public key of ego | ||
544 | GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_EGO, | 571 | GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_EGO, |
545 | strlen (GNUNET_REST_JSONAPI_GNS_EGO), | 572 | strlen (GNUNET_REST_JSONAPI_GNS_EGO), |
546 | &key); | 573 | &key); |
@@ -556,24 +583,23 @@ get_gns_cont (struct GNUNET_REST_RequestHandle *conndata_handle, | |||
556 | handle); | 583 | handle); |
557 | return; | 584 | return; |
558 | } | 585 | } |
586 | |||
587 | //if name ends with .zkey then get public key | ||
559 | if ( (NULL != handle->name) && | 588 | if ( (NULL != handle->name) && |
560 | (strlen (handle->name) > 4) && | 589 | (strlen (handle->name) > 4) && |
561 | (0 == strcmp (".zkey", | 590 | (0 == strcmp (".zkey", |
562 | &handle->name[strlen (handle->name) - 4])) ) | 591 | &handle->name[strlen (handle->name) - 4])) ) |
563 | { | 592 | { |
564 | GNUNET_CRYPTO_ecdsa_key_get_public | 593 | GNUNET_CRYPTO_ecdsa_key_get_public( GNUNET_CRYPTO_ecdsa_key_get_anonymous (), |
565 | (GNUNET_CRYPTO_ecdsa_key_get_anonymous (), | 594 | &(handle->pkey)); |
566 | &(handle->pkey)); | ||
567 | lookup_with_public_key (handle); | 595 | lookup_with_public_key (handle); |
568 | } | 596 | } |
569 | else | 597 | else //else use gns-master identity |
570 | { | 598 | { |
571 | GNUNET_break (NULL == handle->id_op); | ||
572 | handle->id_op = GNUNET_IDENTITY_get (handle->identity, | 599 | handle->id_op = GNUNET_IDENTITY_get (handle->identity, |
573 | "gns-master", | 600 | "gns-master", |
574 | &identity_master_cb, | 601 | &identity_master_cb, |
575 | handle); | 602 | handle); |
576 | GNUNET_assert (NULL != handle->id_op); | ||
577 | } | 603 | } |
578 | } | 604 | } |
579 | 605 | ||
@@ -631,10 +657,10 @@ rest_gns_process_request (struct GNUNET_REST_RequestHandle *conndata_handle, | |||
631 | handle->proc = proc; | 657 | handle->proc = proc; |
632 | handle->rest_handle = conndata_handle; | 658 | handle->rest_handle = conndata_handle; |
633 | 659 | ||
634 | if (GNUNET_NO == GNUNET_JSONAPI_handle_request (conndata_handle, | 660 | if (GNUNET_NO == GNUNET_REST_handle_request (conndata_handle, |
635 | handlers, | 661 | handlers, |
636 | &err, | 662 | &err, |
637 | handle)) | 663 | handle)) |
638 | { | 664 | { |
639 | handle->response_code = err.error_code; | 665 | handle->response_code = err.error_code; |
640 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 666 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
diff --git a/src/identity/plugin_rest_identity.c b/src/identity/plugin_rest_identity.c index 52685c52e..fd6562f1d 100644 --- a/src/identity/plugin_rest_identity.c +++ b/src/identity/plugin_rest_identity.c | |||
@@ -17,6 +17,7 @@ | |||
17 | */ | 17 | */ |
18 | /** | 18 | /** |
19 | * @author Martin Schanzenbach | 19 | * @author Martin Schanzenbach |
20 | * @author Philippe Buschmann | ||
20 | * @file identity/plugin_rest_identity.c | 21 | * @file identity/plugin_rest_identity.c |
21 | * @brief GNUnet Namestore REST plugin | 22 | * @brief GNUnet Namestore REST plugin |
22 | * | 23 | * |
@@ -26,8 +27,6 @@ | |||
26 | #include "gnunet_rest_plugin.h" | 27 | #include "gnunet_rest_plugin.h" |
27 | #include "gnunet_identity_service.h" | 28 | #include "gnunet_identity_service.h" |
28 | #include "gnunet_rest_lib.h" | 29 | #include "gnunet_rest_lib.h" |
29 | #include "gnunet_jsonapi_lib.h" | ||
30 | #include "gnunet_jsonapi_util.h" | ||
31 | #include "microhttpd.h" | 30 | #include "microhttpd.h" |
32 | #include <jansson.h> | 31 | #include <jansson.h> |
33 | #include "gnunet_signatures.h" | 32 | #include "gnunet_signatures.h" |
@@ -50,23 +49,23 @@ | |||
50 | /** | 49 | /** |
51 | * Resource type | 50 | * Resource type |
52 | */ | 51 | */ |
53 | #define GNUNET_REST_JSONAPI_IDENTITY_EGO "ego" | 52 | #define GNUNET_REST_JSON_IDENTITY_EGO "ego" |
54 | 53 | ||
55 | /** | 54 | /** |
56 | * Name attribute | 55 | * Name attribute |
57 | */ | 56 | */ |
58 | #define GNUNET_REST_JSONAPI_IDENTITY_NAME "name" | 57 | #define GNUNET_REST_JSON_IDENTITY_NAME "name" |
59 | 58 | ||
60 | /** | 59 | /** |
61 | * Attribute to rename "name" TODO we changed id to the pubkey | 60 | * Attribute to rename "name" TODO we changed id to the pubkey |
62 | * so this can be unified with "name" | 61 | * so this can be unified with "name" |
63 | */ | 62 | */ |
64 | #define GNUNET_REST_JSONAPI_IDENTITY_NEWNAME "newname" | 63 | #define GNUNET_REST_JSON_IDENTITY_NEWNAME "newname" |
65 | 64 | ||
66 | /** | 65 | /** |
67 | * URL parameter to change the subsytem for ego | 66 | * URL parameter to change the subsytem for ego |
68 | */ | 67 | */ |
69 | #define GNUNET_REST_JSONAPI_IDENTITY_SUBSYSTEM "subsystem" | 68 | #define GNUNET_REST_JSON_IDENTITY_SUBSYSTEM "subsystem" |
70 | 69 | ||
71 | 70 | ||
72 | /** | 71 | /** |
@@ -74,6 +73,7 @@ | |||
74 | */ | 73 | */ |
75 | #define GNUNET_REST_ERROR_RESOURCE_INVALID "Resource location invalid" | 74 | #define GNUNET_REST_ERROR_RESOURCE_INVALID "Resource location invalid" |
76 | #define GNUNET_REST_ERROR_NO_DATA "No data" | 75 | #define GNUNET_REST_ERROR_NO_DATA "No data" |
76 | #define GNUNET_REST_ERROR_DATA_INVALID "Data invalid" | ||
77 | 77 | ||
78 | /** | 78 | /** |
79 | * GNUid token lifetime | 79 | * GNUid token lifetime |
@@ -276,16 +276,16 @@ do_error (void *cls) | |||
276 | struct MHD_Response *resp; | 276 | struct MHD_Response *resp; |
277 | char *json_error; | 277 | char *json_error; |
278 | 278 | ||
279 | GNUNET_asprintf (&json_error, | 279 | if (NULL == handle->emsg) |
280 | "{Error while processing request: %s}", | 280 | handle->emsg = GNUNET_strdup("Unknown Error"); |
281 | &handle->emsg); | 281 | |
282 | GNUNET_asprintf (&json_error, "{\"error\": \"%s\"}", handle->emsg); | ||
283 | handle->response_code = MHD_HTTP_OK; | ||
282 | 284 | ||
283 | resp = GNUNET_REST_create_response (json_error); | 285 | resp = GNUNET_REST_create_response (json_error); |
284 | handle->proc (handle->proc_cls, | 286 | handle->proc (handle->proc_cls, resp, handle->response_code); |
285 | resp, | ||
286 | handle->response_code); | ||
287 | cleanup_handle (handle); | 287 | cleanup_handle (handle); |
288 | GNUNET_free (json_error); | 288 | GNUNET_free(json_error); |
289 | } | 289 | } |
290 | 290 | ||
291 | 291 | ||
@@ -304,14 +304,15 @@ get_ego_for_subsys (void *cls, | |||
304 | const char *name) | 304 | const char *name) |
305 | { | 305 | { |
306 | struct RequestHandle *handle = cls; | 306 | struct RequestHandle *handle = cls; |
307 | struct GNUNET_JSONAPI_Document *json_document; | ||
308 | struct GNUNET_JSONAPI_Resource *json_resource; | ||
309 | struct EgoEntry *ego_entry; | 307 | struct EgoEntry *ego_entry; |
310 | struct MHD_Response *resp; | 308 | struct MHD_Response *resp; |
309 | json_t *json_root; | ||
310 | json_t *json_ego; | ||
311 | json_t *name_json; | 311 | json_t *name_json; |
312 | char *result_str; | 312 | char *result_str; |
313 | size_t index; | ||
313 | 314 | ||
314 | json_document = GNUNET_JSONAPI_document_new (); | 315 | json_root = json_array(); |
315 | 316 | ||
316 | for (ego_entry = handle->ego_head; | 317 | for (ego_entry = handle->ego_head; |
317 | NULL != ego_entry; | 318 | NULL != ego_entry; |
@@ -321,32 +322,38 @@ get_ego_for_subsys (void *cls, | |||
321 | continue; | 322 | continue; |
322 | if (NULL == name) | 323 | if (NULL == name) |
323 | continue; | 324 | continue; |
324 | json_resource = GNUNET_JSONAPI_resource_new | 325 | |
325 | (GNUNET_REST_JSONAPI_IDENTITY_EGO, ego_entry->keystring); | 326 | json_ego = json_object(); |
326 | name_json = json_string (ego_entry->identifier); | 327 | name_json = json_string (ego_entry->identifier); |
327 | GNUNET_JSONAPI_resource_add_attr (json_resource, | 328 | json_object_set_new(json_ego, GNUNET_REST_JSON_IDENTITY_EGO, name_json); |
328 | GNUNET_REST_JSONAPI_IDENTITY_NAME, | 329 | json_array_append(json_root, json_ego); |
329 | name_json); | 330 | |
330 | json_decref (name_json); | ||
331 | GNUNET_JSONAPI_document_resource_add (json_document, json_resource); | ||
332 | break; | 331 | break; |
333 | } | 332 | } |
334 | if (0 == GNUNET_JSONAPI_document_resource_count (json_document)) | 333 | |
334 | if (0 == json_array_size(json_root)) | ||
335 | { | 335 | { |
336 | GNUNET_JSONAPI_document_delete (json_document); | 336 | json_decref(json_root); |
337 | handle->emsg = GNUNET_strdup("No identity matches results!"); | 337 | handle->emsg = GNUNET_strdup("No identity matches results!"); |
338 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 338 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
339 | return; | 339 | return; |
340 | } | 340 | } |
341 | GNUNET_JSONAPI_document_serialize (json_document, &result_str); | 341 | |
342 | result_str = json_dumps(json_root, 0); | ||
342 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); | 343 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); |
343 | resp = GNUNET_REST_create_response (result_str); | 344 | resp = GNUNET_REST_create_response (result_str); |
344 | GNUNET_JSONAPI_document_delete (json_document); | 345 | |
346 | json_array_foreach(json_root, index, json_ego ) | ||
347 | { | ||
348 | json_decref(json_ego); | ||
349 | } | ||
350 | json_decref (json_root); | ||
345 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | 351 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); |
346 | GNUNET_free (result_str); | 352 | GNUNET_free (result_str); |
347 | cleanup_handle (handle); | 353 | cleanup_handle (handle); |
348 | } | 354 | } |
349 | 355 | ||
356 | |||
350 | /** | 357 | /** |
351 | * Create a response with requested ego(s) | 358 | * Create a response with requested ego(s) |
352 | * | 359 | * |
@@ -367,9 +374,10 @@ ego_info_response (struct GNUNET_REST_RequestHandle *con, | |||
367 | struct EgoEntry *ego_entry; | 374 | struct EgoEntry *ego_entry; |
368 | struct GNUNET_HashCode key; | 375 | struct GNUNET_HashCode key; |
369 | struct MHD_Response *resp; | 376 | struct MHD_Response *resp; |
370 | struct GNUNET_JSONAPI_Document *json_document; | 377 | json_t *json_root; |
371 | struct GNUNET_JSONAPI_Resource *json_resource; | 378 | json_t *json_ego; |
372 | json_t *name_str; | 379 | json_t *name_str; |
380 | size_t index; | ||
373 | 381 | ||
374 | if (GNUNET_NO == GNUNET_REST_namespace_match (handle->url, GNUNET_REST_API_NS_IDENTITY)) | 382 | if (GNUNET_NO == GNUNET_REST_namespace_match (handle->url, GNUNET_REST_API_NS_IDENTITY)) |
375 | { | 383 | { |
@@ -395,8 +403,8 @@ ego_info_response (struct GNUNET_REST_RequestHandle *con, | |||
395 | } | 403 | } |
396 | 404 | ||
397 | if ( NULL == egoname ) { | 405 | if ( NULL == egoname ) { |
398 | GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_IDENTITY_SUBSYSTEM, | 406 | GNUNET_CRYPTO_hash (GNUNET_REST_JSON_IDENTITY_SUBSYSTEM, |
399 | strlen (GNUNET_REST_JSONAPI_IDENTITY_SUBSYSTEM), | 407 | strlen (GNUNET_REST_JSON_IDENTITY_SUBSYSTEM), |
400 | &key); | 408 | &key); |
401 | if ( GNUNET_YES == | 409 | if ( GNUNET_YES == |
402 | GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map, | 410 | GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map, |
@@ -417,7 +425,7 @@ ego_info_response (struct GNUNET_REST_RequestHandle *con, | |||
417 | } | 425 | } |
418 | } | 426 | } |
419 | 427 | ||
420 | json_document = GNUNET_JSONAPI_document_new (); | 428 | json_root = json_array(); |
421 | 429 | ||
422 | //Return all egos | 430 | //Return all egos |
423 | for (ego_entry = handle->ego_head; | 431 | for (ego_entry = handle->ego_head; |
@@ -426,27 +434,35 @@ ego_info_response (struct GNUNET_REST_RequestHandle *con, | |||
426 | { | 434 | { |
427 | if ( (NULL != egoname) && (0 != strcmp (egoname, ego_entry->identifier)) ) | 435 | if ( (NULL != egoname) && (0 != strcmp (egoname, ego_entry->identifier)) ) |
428 | continue; | 436 | continue; |
429 | json_resource = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_IDENTITY_EGO, | 437 | |
430 | ego_entry->keystring); | 438 | json_ego = json_object(); |
439 | |||
440 | json_object_set_new( json_ego, "id", json_string (ego_entry->keystring)); | ||
441 | json_object_set_new( json_ego, "type", json_string (GNUNET_REST_JSON_IDENTITY_EGO)); | ||
431 | name_str = json_string (ego_entry->identifier); | 442 | name_str = json_string (ego_entry->identifier); |
432 | GNUNET_JSONAPI_resource_add_attr ( | 443 | json_object_set_new( json_ego, "name", name_str); |
433 | json_resource, | 444 | |
434 | GNUNET_REST_JSONAPI_IDENTITY_NAME, | 445 | json_array_append( json_root, json_ego ); |
435 | name_str); | ||
436 | json_decref (name_str); | ||
437 | GNUNET_JSONAPI_document_resource_add (json_document, json_resource); | ||
438 | } | 446 | } |
439 | if (0 == GNUNET_JSONAPI_document_resource_count (json_document)) | 447 | |
448 | if ((size_t)0 == json_array_size(json_root)) | ||
440 | { | 449 | { |
441 | GNUNET_JSONAPI_document_delete (json_document); | 450 | json_decref (json_root); |
442 | handle->emsg = GNUNET_strdup ("No identities found!"); | 451 | handle->emsg = GNUNET_strdup ("No identities found!"); |
443 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 452 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
444 | return; | 453 | return; |
445 | } | 454 | } |
446 | GNUNET_JSONAPI_document_serialize (json_document, &result_str); | 455 | |
456 | result_str = json_dumps(json_root, 0); | ||
447 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); | 457 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); |
448 | resp = GNUNET_REST_create_response (result_str); | 458 | resp = GNUNET_REST_create_response (result_str); |
449 | GNUNET_JSONAPI_document_delete (json_document); | 459 | |
460 | //delete json_objects in json_array with macro | ||
461 | json_array_foreach(json_root, index, json_ego ) | ||
462 | { | ||
463 | json_decref(json_ego); | ||
464 | } | ||
465 | json_decref (json_root); | ||
450 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | 466 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); |
451 | GNUNET_free (result_str); | 467 | GNUNET_free (result_str); |
452 | cleanup_handle (handle); | 468 | cleanup_handle (handle); |
@@ -472,7 +488,7 @@ do_finished (void *cls, const char *emsg) | |||
472 | return; | 488 | return; |
473 | } | 489 | } |
474 | resp = GNUNET_REST_create_response (NULL); | 490 | resp = GNUNET_REST_create_response (NULL); |
475 | handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT); | 491 | handle->proc (handle->proc_cls, resp, handle->response_code); |
476 | cleanup_handle (handle); | 492 | cleanup_handle (handle); |
477 | } | 493 | } |
478 | 494 | ||
@@ -491,23 +507,19 @@ ego_create_cont (struct GNUNET_REST_RequestHandle *con, | |||
491 | struct RequestHandle *handle = cls; | 507 | struct RequestHandle *handle = cls; |
492 | struct EgoEntry *ego_entry; | 508 | struct EgoEntry *ego_entry; |
493 | struct MHD_Response *resp; | 509 | struct MHD_Response *resp; |
494 | struct GNUNET_JSONAPI_Document *json_obj; | ||
495 | struct GNUNET_JSONAPI_Resource *json_res; | ||
496 | json_t *egoname_json; | 510 | json_t *egoname_json; |
497 | json_t *data_js; | 511 | json_t *data_js; |
498 | json_error_t err; | 512 | json_error_t err; |
499 | const char* egoname; | 513 | const char* egoname; |
500 | char term_data[handle->data_size+1]; | 514 | char term_data[handle->data_size+1]; |
501 | struct GNUNET_JSON_Specification docspec[] = { | 515 | |
502 | GNUNET_JSON_spec_jsonapi_document (&json_obj), | ||
503 | GNUNET_JSON_spec_end() | ||
504 | }; | ||
505 | if (strlen (GNUNET_REST_API_NS_IDENTITY) != strlen (handle->url)) | 516 | if (strlen (GNUNET_REST_API_NS_IDENTITY) != strlen (handle->url)) |
506 | { | 517 | { |
507 | handle->emsg = GNUNET_strdup (GNUNET_REST_ERROR_RESOURCE_INVALID); | 518 | handle->emsg = GNUNET_strdup (GNUNET_REST_ERROR_RESOURCE_INVALID); |
508 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 519 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
509 | return; | 520 | return; |
510 | } | 521 | } |
522 | |||
511 | if (0 >= handle->data_size) | 523 | if (0 >= handle->data_size) |
512 | { | 524 | { |
513 | handle->emsg = GNUNET_strdup (GNUNET_REST_ERROR_NO_DATA); | 525 | handle->emsg = GNUNET_strdup (GNUNET_REST_ERROR_NO_DATA); |
@@ -519,50 +531,54 @@ ego_create_cont (struct GNUNET_REST_RequestHandle *con, | |||
519 | data_js = json_loads (term_data, | 531 | data_js = json_loads (term_data, |
520 | JSON_DECODE_ANY, | 532 | JSON_DECODE_ANY, |
521 | &err); | 533 | &err); |
522 | GNUNET_assert (NULL != data_js); | ||
523 | GNUNET_assert (GNUNET_OK == | ||
524 | GNUNET_JSON_parse (data_js, docspec, | ||
525 | NULL, NULL)); | ||
526 | 534 | ||
527 | json_decref (data_js); | ||
528 | 535 | ||
529 | if (NULL == json_obj) | 536 | if (NULL == data_js) |
530 | { | 537 | { |
538 | handle->emsg = GNUNET_strdup (GNUNET_REST_ERROR_DATA_INVALID); | ||
531 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 539 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
532 | return; | 540 | return; |
533 | } | 541 | } |
534 | if (1 != GNUNET_JSONAPI_document_resource_count (json_obj)) | 542 | //instead of parse |
543 | if (!json_is_object(data_js)) | ||
535 | { | 544 | { |
536 | GNUNET_JSONAPI_document_delete (json_obj); | 545 | json_decref(data_js); |
537 | handle->emsg = GNUNET_strdup ("Provided resource count invalid"); | 546 | handle->emsg = GNUNET_strdup (GNUNET_REST_ERROR_DATA_INVALID); |
538 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 547 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
539 | return; | 548 | return; |
540 | } | 549 | } |
541 | json_res = GNUNET_JSONAPI_document_get_resource (json_obj, 0); | 550 | |
542 | if (GNUNET_NO == GNUNET_JSONAPI_resource_check_type (json_res, GNUNET_REST_JSONAPI_IDENTITY_EGO)) | 551 | if (1 != json_object_size (data_js)) |
543 | { | 552 | { |
544 | GNUNET_JSONAPI_document_delete (json_obj); | 553 | json_decref (data_js); |
545 | resp = GNUNET_REST_create_response (NULL); | 554 | handle->emsg = GNUNET_strdup("Provided resource count invalid"); |
546 | handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); | 555 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
547 | cleanup_handle (handle); | ||
548 | return; | 556 | return; |
549 | } | 557 | } |
550 | egoname_json = GNUNET_JSONAPI_resource_read_attr (json_res, GNUNET_REST_JSONAPI_IDENTITY_NAME); | 558 | |
559 | egoname_json = json_object_get (data_js, GNUNET_REST_JSON_IDENTITY_NAME); | ||
551 | if (!json_is_string (egoname_json)) | 560 | if (!json_is_string (egoname_json)) |
552 | { | 561 | { |
553 | GNUNET_JSONAPI_document_delete (json_obj); | 562 | json_decref (data_js); |
554 | handle->emsg = GNUNET_strdup ("No name provided"); | 563 | handle->emsg = GNUNET_strdup ("No name provided"); |
555 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 564 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
556 | return; | 565 | return; |
557 | } | 566 | } |
558 | egoname = json_string_value (egoname_json); | 567 | egoname = json_string_value (egoname_json); |
568 | if(0 >= strlen(egoname)) | ||
569 | { | ||
570 | json_decref (data_js); | ||
571 | handle->emsg = GNUNET_strdup ("No name provided"); | ||
572 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
573 | return; | ||
574 | } | ||
559 | for (ego_entry = handle->ego_head; | 575 | for (ego_entry = handle->ego_head; |
560 | NULL != ego_entry; | 576 | NULL != ego_entry; |
561 | ego_entry = ego_entry->next) | 577 | ego_entry = ego_entry->next) |
562 | { | 578 | { |
563 | if (0 == strcasecmp (egoname, ego_entry->identifier)) | 579 | if (0 == strcasecmp (egoname, ego_entry->identifier)) |
564 | { | 580 | { |
565 | GNUNET_JSONAPI_document_delete (json_obj); | 581 | json_decref (data_js); |
566 | resp = GNUNET_REST_create_response (NULL); | 582 | resp = GNUNET_REST_create_response (NULL); |
567 | handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); | 583 | handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); |
568 | cleanup_handle (handle); | 584 | cleanup_handle (handle); |
@@ -570,7 +586,8 @@ ego_create_cont (struct GNUNET_REST_RequestHandle *con, | |||
570 | } | 586 | } |
571 | } | 587 | } |
572 | GNUNET_asprintf (&handle->name, "%s", egoname); | 588 | GNUNET_asprintf (&handle->name, "%s", egoname); |
573 | GNUNET_JSONAPI_document_delete (json_obj); | 589 | json_decref (data_js); |
590 | handle->response_code = MHD_HTTP_CREATED; | ||
574 | handle->op = GNUNET_IDENTITY_create (handle->identity_handle, | 591 | handle->op = GNUNET_IDENTITY_create (handle->identity_handle, |
575 | handle->name, | 592 | handle->name, |
576 | &do_finished, | 593 | &do_finished, |
@@ -590,8 +607,6 @@ ego_edit_cont (struct GNUNET_REST_RequestHandle *con, | |||
590 | const char *url, | 607 | const char *url, |
591 | void *cls) | 608 | void *cls) |
592 | { | 609 | { |
593 | struct GNUNET_JSONAPI_Document *json_obj; | ||
594 | struct GNUNET_JSONAPI_Resource *json_res; | ||
595 | struct RequestHandle *handle = cls; | 610 | struct RequestHandle *handle = cls; |
596 | struct EgoEntry *ego_entry; | 611 | struct EgoEntry *ego_entry; |
597 | struct EgoEntry *ego_entry_tmp; | 612 | struct EgoEntry *ego_entry_tmp; |
@@ -605,10 +620,6 @@ ego_edit_cont (struct GNUNET_REST_RequestHandle *con, | |||
605 | const char *newname; | 620 | const char *newname; |
606 | char term_data[handle->data_size+1]; | 621 | char term_data[handle->data_size+1]; |
607 | int ego_exists = GNUNET_NO; | 622 | int ego_exists = GNUNET_NO; |
608 | struct GNUNET_JSON_Specification docspec[] = { | ||
609 | GNUNET_JSON_spec_jsonapi_document (&json_obj), | ||
610 | GNUNET_JSON_spec_end() | ||
611 | }; | ||
612 | 623 | ||
613 | if (strlen (GNUNET_REST_API_NS_IDENTITY) > strlen (handle->url)) | 624 | if (strlen (GNUNET_REST_API_NS_IDENTITY) > strlen (handle->url)) |
614 | { | 625 | { |
@@ -649,43 +660,40 @@ ego_edit_cont (struct GNUNET_REST_RequestHandle *con, | |||
649 | data_js = json_loads (term_data, | 660 | data_js = json_loads (term_data, |
650 | JSON_DECODE_ANY, | 661 | JSON_DECODE_ANY, |
651 | &err); | 662 | &err); |
652 | GNUNET_assert (NULL != data_js); | 663 | if (NULL == data_js) |
653 | GNUNET_assert (GNUNET_OK == | ||
654 | GNUNET_JSON_parse (data_js, docspec, | ||
655 | NULL, NULL)); | ||
656 | |||
657 | json_decref (data_js); | ||
658 | |||
659 | if (NULL == json_obj) | ||
660 | { | 664 | { |
661 | handle->emsg = GNUNET_strdup ("Data invalid"); | 665 | handle->emsg = GNUNET_strdup (GNUNET_REST_ERROR_NO_DATA); |
662 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 666 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
663 | return; | 667 | return; |
664 | } | 668 | } |
665 | 669 | if (!json_is_object(data_js)) | |
666 | if (1 != GNUNET_JSONAPI_document_resource_count (json_obj)) | ||
667 | { | 670 | { |
668 | GNUNET_JSONAPI_document_delete (json_obj); | 671 | json_decref (data_js); |
669 | handle->emsg = GNUNET_strdup ("Resource amount invalid"); | 672 | handle->emsg = GNUNET_strdup (GNUNET_REST_ERROR_DATA_INVALID); |
670 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 673 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
671 | return; | 674 | return; |
672 | } | 675 | } |
673 | json_res = GNUNET_JSONAPI_document_get_resource (json_obj, 0); | ||
674 | 676 | ||
675 | if (GNUNET_NO == GNUNET_JSONAPI_resource_check_type (json_res, GNUNET_REST_JSONAPI_IDENTITY_EGO)) | 677 | if (1 != json_object_size(data_js)) |
676 | { | 678 | { |
677 | GNUNET_JSONAPI_document_delete (json_obj); | 679 | json_decref (data_js); |
678 | handle->emsg = GNUNET_strdup ("Resource type invalid"); | 680 | handle->emsg = GNUNET_strdup ("Resource amount invalid"); |
679 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 681 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
680 | return; | 682 | return; |
681 | } | 683 | } |
682 | 684 | ||
683 | //This is a rename | 685 | //This is a rename |
684 | name_json = GNUNET_JSONAPI_resource_read_attr (json_res, | 686 | name_json = json_object_get (data_js, GNUNET_REST_JSON_IDENTITY_NEWNAME); |
685 | GNUNET_REST_JSONAPI_IDENTITY_NEWNAME); | ||
686 | if ((NULL != name_json) && json_is_string (name_json)) | 687 | if ((NULL != name_json) && json_is_string (name_json)) |
687 | { | 688 | { |
688 | newname = json_string_value (name_json); | 689 | newname = json_string_value (name_json); |
690 | if(0 >= strlen(newname)) | ||
691 | { | ||
692 | json_decref (data_js); | ||
693 | handle->emsg = GNUNET_strdup ("No name provided"); | ||
694 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
695 | return; | ||
696 | } | ||
689 | for (ego_entry_tmp = handle->ego_head; | 697 | for (ego_entry_tmp = handle->ego_head; |
690 | NULL != ego_entry_tmp; | 698 | NULL != ego_entry_tmp; |
691 | ego_entry_tmp = ego_entry_tmp->next) | 699 | ego_entry_tmp = ego_entry_tmp->next) |
@@ -694,29 +702,38 @@ ego_edit_cont (struct GNUNET_REST_RequestHandle *con, | |||
694 | 0 != strcasecmp (keystring, ego_entry_tmp->keystring)) | 702 | 0 != strcasecmp (keystring, ego_entry_tmp->keystring)) |
695 | { | 703 | { |
696 | //Ego with same name not allowed | 704 | //Ego with same name not allowed |
697 | GNUNET_JSONAPI_document_delete (json_obj); | 705 | json_decref (data_js); |
698 | resp = GNUNET_REST_create_response (NULL); | 706 | resp = GNUNET_REST_create_response (NULL); |
699 | handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); | 707 | handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); |
700 | cleanup_handle (handle); | 708 | cleanup_handle (handle); |
701 | return; | 709 | return; |
702 | } | 710 | } |
703 | } | 711 | } |
712 | handle->response_code = MHD_HTTP_NO_CONTENT; | ||
704 | handle->op = GNUNET_IDENTITY_rename (handle->identity_handle, | 713 | handle->op = GNUNET_IDENTITY_rename (handle->identity_handle, |
705 | ego_entry->identifier, | 714 | ego_entry->identifier, |
706 | newname, | 715 | newname, |
707 | &do_finished, | 716 | &do_finished, |
708 | handle); | 717 | handle); |
709 | GNUNET_JSONAPI_document_delete (json_obj); | 718 | json_decref (data_js); |
710 | return; | 719 | return; |
711 | } | 720 | } |
712 | 721 | ||
713 | //Set subsystem | 722 | //Set subsystem |
714 | subsys_json = GNUNET_JSONAPI_resource_read_attr (json_res, GNUNET_REST_JSONAPI_IDENTITY_SUBSYSTEM); | 723 | subsys_json = json_object_get (data_js, GNUNET_REST_JSON_IDENTITY_SUBSYSTEM); |
715 | if ( (NULL != subsys_json) && json_is_string (subsys_json)) | 724 | if ( (NULL != subsys_json) && json_is_string (subsys_json)) |
716 | { | 725 | { |
717 | subsys = json_string_value (subsys_json); | 726 | subsys = json_string_value (subsys_json); |
727 | if(0 >= strlen(subsys)) | ||
728 | { | ||
729 | json_decref (data_js); | ||
730 | handle->emsg = GNUNET_strdup ("No name provided"); | ||
731 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
732 | return; | ||
733 | } | ||
718 | GNUNET_asprintf (&handle->subsys, "%s", subsys); | 734 | GNUNET_asprintf (&handle->subsys, "%s", subsys); |
719 | GNUNET_JSONAPI_document_delete (json_obj); | 735 | json_decref (data_js); |
736 | handle->response_code = MHD_HTTP_NO_CONTENT; | ||
720 | handle->op = GNUNET_IDENTITY_set (handle->identity_handle, | 737 | handle->op = GNUNET_IDENTITY_set (handle->identity_handle, |
721 | handle->subsys, | 738 | handle->subsys, |
722 | ego_entry->ego, | 739 | ego_entry->ego, |
@@ -724,11 +741,18 @@ ego_edit_cont (struct GNUNET_REST_RequestHandle *con, | |||
724 | handle); | 741 | handle); |
725 | return; | 742 | return; |
726 | } | 743 | } |
727 | GNUNET_JSONAPI_document_delete (json_obj); | 744 | json_decref (data_js); |
728 | handle->emsg = GNUNET_strdup ("Subsystem not provided"); | 745 | handle->emsg = GNUNET_strdup ("Subsystem not provided"); |
729 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 746 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
730 | } | 747 | } |
731 | 748 | ||
749 | /** | ||
750 | * Handle ego delete request | ||
751 | * | ||
752 | * @param con_handle the connection handle | ||
753 | * @param url the url | ||
754 | * @param cls the RequestHandle | ||
755 | */ | ||
732 | void | 756 | void |
733 | ego_delete_cont (struct GNUNET_REST_RequestHandle *con_handle, | 757 | ego_delete_cont (struct GNUNET_REST_RequestHandle *con_handle, |
734 | const char* url, | 758 | const char* url, |
@@ -764,6 +788,7 @@ ego_delete_cont (struct GNUNET_REST_RequestHandle *con_handle, | |||
764 | cleanup_handle (handle); | 788 | cleanup_handle (handle); |
765 | return; | 789 | return; |
766 | } | 790 | } |
791 | handle->response_code = MHD_HTTP_NO_CONTENT; | ||
767 | handle->op = GNUNET_IDENTITY_delete (handle->identity_handle, | 792 | handle->op = GNUNET_IDENTITY_delete (handle->identity_handle, |
768 | ego_entry->identifier, | 793 | ego_entry->identifier, |
769 | &do_finished, | 794 | &do_finished, |
@@ -815,7 +840,7 @@ init_cont (struct RequestHandle *handle) | |||
815 | GNUNET_REST_HANDLER_END | 840 | GNUNET_REST_HANDLER_END |
816 | }; | 841 | }; |
817 | 842 | ||
818 | if (GNUNET_NO == GNUNET_JSONAPI_handle_request (handle->conndata_handle, | 843 | if (GNUNET_NO == GNUNET_REST_handle_request (handle->conndata_handle, |
819 | handlers, | 844 | handlers, |
820 | &err, | 845 | &err, |
821 | handle)) | 846 | handle)) |
@@ -907,7 +932,7 @@ rest_identity_process_request(struct GNUNET_REST_RequestHandle *conndata_handle, | |||
907 | 932 | ||
908 | 933 | ||
909 | handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL; | 934 | handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL; |
910 | 935 | handle->response_code = MHD_HTTP_OK; | |
911 | handle->proc_cls = proc_cls; | 936 | handle->proc_cls = proc_cls; |
912 | handle->proc = proc; | 937 | handle->proc = proc; |
913 | handle->state = ID_REST_STATE_INIT; | 938 | handle->state = ID_REST_STATE_INIT; |
diff --git a/src/identity/test_plugin_identity_rest.sh b/src/identity/test_plugin_identity_rest.sh new file mode 100755 index 000000000..d43f4206b --- /dev/null +++ b/src/identity/test_plugin_identity_rest.sh | |||
@@ -0,0 +1,252 @@ | |||
1 | #!/usr/bin/bash | ||
2 | |||
3 | #First, start gnunet-arm and the rest-service. Make sure, no identity exists | ||
4 | |||
5 | link_to_api="http://localhost:7776/identity" | ||
6 | wrong_link="http://localhost:7776/idenmmmy" | ||
7 | |||
8 | #Test GET (multiple identities) for error when no identity exists | ||
9 | |||
10 | echo "No test for subsystem available" | ||
11 | echo "The next test case can be ignored if you have already added identities" | ||
12 | cache="$(curl --silent "$link_to_api" | grep "error")" | ||
13 | if [ "" == "$cache" ] | ||
14 | then | ||
15 | echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for GET request when missing identity\n" | ||
16 | else | ||
17 | echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for GET request when missing identity\n" | ||
18 | fi | ||
19 | |||
20 | #Test POST success code, error response code and error json | ||
21 | echo "The next test case can be ignored if you have already added an identity with the name Test" | ||
22 | cache="$(curl -v -X "POST" "$link_to_api" --data "{\"name\":\"Test\"}" 2>&1 | grep "HTTP/1.1 201")" | ||
23 | if [ "" == "$cache" ] | ||
24 | then | ||
25 | echo -n -e "[\033[0;31m FAILURE\033[0m ] Error for good POST request\n" | ||
26 | else | ||
27 | echo -n -e "[\033[0;32m SUCCESS\033[0m ] Success for good POST request\n" | ||
28 | fi | ||
29 | |||
30 | cache="$(curl -v -X "POST" "$link_to_api" --data "{\"name\":\"Test\"}" 2>&1 | grep "HTTP/1.1 409")" | ||
31 | if [ "" == "$cache" ] | ||
32 | then | ||
33 | echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for duplicate name POST request\n" | ||
34 | else | ||
35 | echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for duplicate name POST request\n" | ||
36 | fi | ||
37 | |||
38 | cache="$(curl -v -X "POST" "$link_to_api" 2>&1 | grep "error")" | ||
39 | if [ "" == "$cache" ] | ||
40 | then | ||
41 | echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for no data POST request\n" | ||
42 | else | ||
43 | echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for no data POST request\n" | ||
44 | fi | ||
45 | |||
46 | cache="$(curl -v -X "POST" "$link_to_api" --data "wrong" 2>&1 | grep "error")" | ||
47 | if [ "" == "$cache" ] | ||
48 | then | ||
49 | echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for wrong data POST request\n" | ||
50 | else | ||
51 | echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for wrong data POST request\n" | ||
52 | fi | ||
53 | |||
54 | cache="$(curl -v -X "POST" "$link_to_api" --data "[{}]" 2>&1 | grep "error")" | ||
55 | if [ "" == "$cache" ] | ||
56 | then | ||
57 | echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for json array input POST request\n" | ||
58 | else | ||
59 | echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for json array input POST request\n" | ||
60 | fi | ||
61 | |||
62 | cache="$(curl -v -X "POST" "$link_to_api" --data "{\"name\":\"Test\",\"other\":\"Test\"}" 2>&1 | grep "error")" | ||
63 | if [ "" == "$cache" ] | ||
64 | then | ||
65 | echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for multi element json POST request\n" | ||
66 | else | ||
67 | echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for multi element json POST request\n" | ||
68 | fi | ||
69 | |||
70 | cache="$(curl -v -X "POST" "$link_to_api" --data "{\"nam\":\"Test\"}" 2>&1 | grep "error")" | ||
71 | if [ "" == "$cache" ] | ||
72 | then | ||
73 | echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for wrong json POST request\n" | ||
74 | else | ||
75 | echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for wrong json POST request\n" | ||
76 | fi | ||
77 | |||
78 | cache="$(curl -v -X "POST" "$link_to_api" --data "{\"name\":123}" 2>&1 | grep "error")" | ||
79 | if [ "" == "$cache" ] | ||
80 | then | ||
81 | echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for wrong json type POST request\n" | ||
82 | else | ||
83 | echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for wrong json type POST request\n" | ||
84 | fi | ||
85 | |||
86 | cache="$(curl -v -X "POST" "$link_to_api" --data "{\"name\":""}" 2>&1 | grep "error")" | ||
87 | if [ "" == "$cache" ] | ||
88 | then | ||
89 | echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for no name POST request\n" | ||
90 | else | ||
91 | echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for no name POST request\n" | ||
92 | fi | ||
93 | |||
94 | |||
95 | #Test GET (multiple identities) for success and error json | ||
96 | cache="$(curl --silent "$link_to_api" | grep "error")" | ||
97 | if [ "" == "$cache" ] | ||
98 | then | ||
99 | echo -n -e "[\033[0;32m SUCCESS\033[0m ] Success for good GET request (multiple identities)\n" | ||
100 | else | ||
101 | echo -n -e "[\033[0;31m FAILURE\033[0m ] Error for good GET request (multiple identities)\n" | ||
102 | fi | ||
103 | |||
104 | |||
105 | id="$(gnunet-identity -d | grep "Test - " | sed "s/Test - //g")" | ||
106 | #Test GET (one identity) for success and error json | ||
107 | cache="$(curl --silent "${link_to_api}/$id" | grep "error")" | ||
108 | if [ "" == "$cache" ] | ||
109 | then | ||
110 | echo -n -e "[\033[0;32m SUCCESS\033[0m ] Success for good GET request (one identity)\n" | ||
111 | else | ||
112 | echo -n -e "[\033[0;31m FAILURE\033[0m ] Error for good GET request (one identity)\n" | ||
113 | fi | ||
114 | |||
115 | |||
116 | #Test DELETE success code, error response code and error json | ||
117 | echo "Next tests for DELETE will probably fail when POST fails" | ||
118 | cache="$(curl -v -X "DELETE" "${link_to_api}/$id" 2>&1 | grep "HTTP/1.1 404")" | ||
119 | if [ "" == "$cache" ] | ||
120 | then | ||
121 | echo -n -e "[\033[0;32m SUCCESS\033[0m ] Success for good DELETE request\n" | ||
122 | else | ||
123 | echo -n -e "[\033[0;31m FAILURE\033[0m ] Error for good DELETE request\n" | ||
124 | fi | ||
125 | |||
126 | curl --silent -X "POST" "$link_to_api" --data "{\"name\":\"Test\"}" | ||
127 | id="$(gnunet-identity -d | grep "Test - " | sed "s/Test - //g")" | ||
128 | |||
129 | cache="$(curl -v -X "DELETE" "${link_to_api}/df1" 2>&1 | grep "HTTP/1.1 404")" | ||
130 | if [ "" == "$cache" ] | ||
131 | then | ||
132 | echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for wrong DELETE request\n" | ||
133 | else | ||
134 | echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for wrong DELETE request\n" | ||
135 | fi | ||
136 | |||
137 | #Test PUT success code, error response codes and error json | ||
138 | cache="$(curl -v -X "PUT" "${link_to_api}/$id" --data "{\"newname\":\"NewTest\"}" 2>&1 | grep "HTTP/1.1 204")" | ||
139 | if [ "" == "$cache" ] | ||
140 | then | ||
141 | echo -n -e "[\033[0;31m FAILURE\033[0m ] Error for good PUT request\n" | ||
142 | else | ||
143 | echo -n -e "[\033[0;32m SUCCESS\033[0m ] Success for good PUT request\n" | ||
144 | fi | ||
145 | |||
146 | cache="$(curl -v -X "PUT" "${link_to_api}/${id}1" --data "{\"newname\":\"NewNewTest\"}" 2>&1 | grep "HTTP/1.1 404")" | ||
147 | if [ "" == "$cache" ] | ||
148 | then | ||
149 | echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for wrong identity PUT request\n" | ||
150 | else | ||
151 | echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for wrong identity PUT request\n" | ||
152 | fi | ||
153 | |||
154 | cache="$(curl -v -X "PUT" "$link_to_api/$id" --data "{\"newname\":\"NewTest\"}" 2>&1 | grep "error")" | ||
155 | if [ "" == "$cache" ] | ||
156 | then | ||
157 | echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for duplicate name PUT request\n" | ||
158 | else | ||
159 | echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for duplicate name PUT request\n" | ||
160 | fi | ||
161 | |||
162 | cache="$(curl -v -X "PUT" "$link_to_api/$id" 2>&1 | grep "error")" | ||
163 | if [ "" == "$cache" ] | ||
164 | then | ||
165 | echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for no data PUT request\n" | ||
166 | else | ||
167 | echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for no data PUT request\n" | ||
168 | fi | ||
169 | |||
170 | cache="$(curl -v -X "PUT" "$link_to_api/$id" --data "wrong" 2>&1 | grep "error")" | ||
171 | if [ "" == "$cache" ] | ||
172 | then | ||
173 | echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for wrong data PUT request\n" | ||
174 | else | ||
175 | echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for wrong data PUT request\n" | ||
176 | fi | ||
177 | |||
178 | cache="$(curl -v -X "PUT" "$link_to_api/$id" --data "[{}]" 2>&1 | grep "error")" | ||
179 | if [ "" == "$cache" ] | ||
180 | then | ||
181 | echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for json array input PUT request\n" | ||
182 | else | ||
183 | echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for json array input PUT request\n" | ||
184 | fi | ||
185 | |||
186 | cache="$(curl -v -X "PUT" "$link_to_api/$id" --data "{\"newname\":\"Test\",\"other\":\"Test\"}" 2>&1 | grep "error")" | ||
187 | if [ "" == "$cache" ] | ||
188 | then | ||
189 | echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for multi element json PUT request\n" | ||
190 | else | ||
191 | echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for multi element json PUT request\n" | ||
192 | fi | ||
193 | |||
194 | cache="$(curl -v -X "PUT" "$link_to_api/$id" --data "{\"newnam\":\"Test\"}" 2>&1 | grep "error")" | ||
195 | if [ "" == "$cache" ] | ||
196 | then | ||
197 | echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for wrong json PUT request\n" | ||
198 | else | ||
199 | echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for wrong json PUT request\n" | ||
200 | fi | ||
201 | |||
202 | cache="$(curl -v -X "PUT" "$link_to_api/$id" --data "{\"newname\":123}" 2>&1 | grep "error")" | ||
203 | if [ "" == "$cache" ] | ||
204 | then | ||
205 | echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for wrong json type PUT request\n" | ||
206 | else | ||
207 | echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for wrong json type PUT request\n" | ||
208 | fi | ||
209 | |||
210 | cache="$(curl -v -X "PUT" "$link_to_api/$id" --data "{\"newname\":""}" 2>&1 | grep "error")" | ||
211 | if [ "" == "$cache" ] | ||
212 | then | ||
213 | echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for no name PUT request\n" | ||
214 | else | ||
215 | echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for no name PUT request\n" | ||
216 | fi | ||
217 | #TODO Missing subsystem test | ||
218 | |||
219 | #Missing OPTIONS success - nothing can really go wrong here | ||
220 | |||
221 | #Test wrong url | ||
222 | cache="$(curl -v "$wrong_link" 2>&1 | grep "HTTP/1.1 404")" | ||
223 | if [ "" == "$cache" ] | ||
224 | then | ||
225 | echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for wrong url GET request \n" | ||
226 | else | ||
227 | echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for wrong url GET request \n" | ||
228 | fi | ||
229 | |||
230 | cache="$(curl -X "PUT" -v "$wrong_link/$id" --data "{\"newname\":\"Testing\"}" 2>&1 | grep "HTTP/1.1 404")" | ||
231 | if [ "" == "$cache" ] | ||
232 | then | ||
233 | echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for wrong url GET request \n" | ||
234 | else | ||
235 | echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for wrong url GET request \n" | ||
236 | fi | ||
237 | |||
238 | cache="$(curl -X "POST" -v "$wrong_link/$id" --data "{\"name\":\"Test\"}" 2>&1 | grep "HTTP/1.1 404")" | ||
239 | if [ "" == "$cache" ] | ||
240 | then | ||
241 | echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for wrong url POST request \n" | ||
242 | else | ||
243 | echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for wrong url POST request \n" | ||
244 | fi | ||
245 | |||
246 | cache="$(curl -X "DELETE" -v "${wrong_link}/$id" 2>&1 | grep "HTTP/1.1 404")" | ||
247 | if [ "" == "$cache" ] | ||
248 | then | ||
249 | echo -n -e "[\033[0;31m FAILURE\033[0m ] Success for wrong url DELETE request \n" | ||
250 | else | ||
251 | echo -n -e "[\033[0;32m SUCCESS\033[0m ] Error for wrong url DELETE request \n" | ||
252 | fi | ||