summaryrefslogtreecommitdiff
path: root/src/identity/plugin_rest_identity.c
diff options
context:
space:
mode:
authorMartin Schanzenbach <mschanzenbach@posteo.de>2015-03-13 15:33:47 +0000
committerMartin Schanzenbach <mschanzenbach@posteo.de>2015-03-13 15:33:47 +0000
commit7ae2282ee296b24e0132d155ece49ae87e21215b (patch)
tree1dd3d8f99e0b23fcb703c9cc50f05932bf682e18 /src/identity/plugin_rest_identity.c
parente2963510a62c68113535f885365137083d9f474c (diff)
downloadgnunet-7ae2282ee296b24e0132d155ece49ae87e21215b.tar.gz
gnunet-7ae2282ee296b24e0132d155ece49ae87e21215b.zip
-change API
Diffstat (limited to 'src/identity/plugin_rest_identity.c')
-rw-r--r--src/identity/plugin_rest_identity.c158
1 files changed, 110 insertions, 48 deletions
diff --git a/src/identity/plugin_rest_identity.c b/src/identity/plugin_rest_identity.c
index 3257b38d5..60d6547d5 100644
--- a/src/identity/plugin_rest_identity.c
+++ b/src/identity/plugin_rest_identity.c
@@ -169,6 +169,28 @@ struct RequestHandle
169}; 169};
170 170
171/** 171/**
172 * Create s JSON Response for MHD
173 *
174 * @param data the JSON to return (can be NULL)
175 * @return a MHD_Response handle
176 */
177struct MHD_Response*
178create_json_response (const char *data)
179{
180 size_t len;
181 if (NULL == data)
182 len = 0;
183 else
184 len = strlen (data);
185 struct MHD_Response *resp = MHD_create_response_from_buffer (len,
186 (void*)data,
187 MHD_RESPMEM_MUST_COPY);
188 MHD_add_response_header (resp,MHD_HTTP_HEADER_CONTENT_TYPE,"application/json");
189 return resp;
190}
191
192
193/**
172 * Cleanup lookup handle 194 * Cleanup lookup handle
173 * @praram handle Handle to clean up 195 * @praram handle Handle to clean up
174 */ 196 */
@@ -214,10 +236,19 @@ do_error (void *cls,
214 const struct GNUNET_SCHEDULER_TaskContext *tc) 236 const struct GNUNET_SCHEDULER_TaskContext *tc)
215{ 237{
216 struct RequestHandle *handle = cls; 238 struct RequestHandle *handle = cls;
217 handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR); 239 struct MHD_Response *resp = create_json_response (NULL);
240 handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST);
218 cleanup_handle (handle); 241 cleanup_handle (handle);
219} 242}
220 243
244/**
245 * Callback for IDENTITY_get()
246 *
247 * @param cls the RequestHandle
248 * @param ego the Ego found
249 * @param ctx the context
250 * @param name the id of the ego
251 */
221void 252void
222get_ego_for_subsys (void *cls, 253get_ego_for_subsys (void *cls,
223 struct GNUNET_IDENTITY_Ego *ego, 254 struct GNUNET_IDENTITY_Ego *ego,
@@ -226,6 +257,7 @@ get_ego_for_subsys (void *cls,
226{ 257{
227 struct RequestHandle *handle = cls; 258 struct RequestHandle *handle = cls;
228 struct EgoEntry *ego_entry; 259 struct EgoEntry *ego_entry;
260 struct MHD_Response *resp;
229 char *result_str; 261 char *result_str;
230 char *keystring; 262 char *keystring;
231 json_t *ego_json; 263 json_t *ego_json;
@@ -261,11 +293,19 @@ get_ego_for_subsys (void *cls,
261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); 293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
262 json_decref (ego_json); 294 json_decref (ego_json);
263 json_decref (root_json); 295 json_decref (root_json);
264 handle->proc (handle->proc_cls, result_str, strlen (result_str), GNUNET_OK); 296 resp = create_json_response (result_str);
297 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
265 GNUNET_free (result_str); 298 GNUNET_free (result_str);
266 cleanup_handle (handle); 299 cleanup_handle (handle);
267} 300}
268 301
302/**
303 * Validate the namespace of the requested url
304 * TODO move this to a lib
305 *
306 * @param url the url
307 * @param ns the namespace
308 */
269int 309int
270check_namespace (const char *url, const char *ns) 310check_namespace (const char *url, const char *ns)
271{ 311{
@@ -284,6 +324,11 @@ check_namespace (const char *url, const char *ns)
284 324
285} 325}
286 326
327/**
328 * Create a response with requested ego(s)
329 *
330 * @param handle the RequestHandle
331 */
287void 332void
288ego_info_response (struct RequestHandle *handle) 333ego_info_response (struct RequestHandle *handle)
289{ 334{
@@ -293,13 +338,15 @@ ego_info_response (struct RequestHandle *handle)
293 char *subsys_val; 338 char *subsys_val;
294 struct EgoEntry *ego_entry; 339 struct EgoEntry *ego_entry;
295 struct GNUNET_HashCode key; 340 struct GNUNET_HashCode key;
341 struct MHD_Response *resp;
296 json_t *ego_arr; 342 json_t *ego_arr;
297 json_t *ego_json; 343 json_t *ego_json;
298 json_t *root_json; 344 json_t *root_json;
299 345
300 if (GNUNET_NO == check_namespace (handle->url, EGO_NAMESPACE)) 346 if (GNUNET_NO == check_namespace (handle->url, EGO_NAMESPACE))
301 { 347 {
302 handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR); 348 resp = create_json_response (NULL);
349 handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST);
303 cleanup_handle (handle); 350 cleanup_handle (handle);
304 GNUNET_break (0); 351 GNUNET_break (0);
305 return; 352 return;
@@ -355,7 +402,6 @@ ego_info_response (struct RequestHandle *handle)
355 else 402 else
356 break; 403 break;
357 } 404 }
358 GNUNET_break (0);
359 if (NULL == egoname) 405 if (NULL == egoname)
360 json_object_set (root_json, JSON_API_TYPE_DATA, ego_arr); 406 json_object_set (root_json, JSON_API_TYPE_DATA, ego_arr);
361 else 407 else
@@ -367,7 +413,8 @@ ego_info_response (struct RequestHandle *handle)
367 json_decref (ego_json); 413 json_decref (ego_json);
368 414
369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); 415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
370 handle->proc (handle->proc_cls, result_str, strlen (result_str), MHD_HTTP_OK); 416 resp = create_json_response (result_str);
417 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
371 GNUNET_free (result_str); 418 GNUNET_free (result_str);
372 cleanup_handle (handle); 419 cleanup_handle (handle);
373 420
@@ -377,6 +424,7 @@ static void
377do_finished (void *cls, const char *emsg) 424do_finished (void *cls, const char *emsg)
378{ 425{
379 struct RequestHandle *handle = cls; 426 struct RequestHandle *handle = cls;
427 struct MHD_Response *resp;
380 428
381 handle->op = NULL; 429 handle->op = NULL;
382 if (NULL != emsg) 430 if (NULL != emsg)
@@ -384,7 +432,8 @@ do_finished (void *cls, const char *emsg)
384 GNUNET_SCHEDULER_add_now (&do_error, handle); 432 GNUNET_SCHEDULER_add_now (&do_error, handle);
385 return; 433 return;
386 } 434 }
387 handle->proc (handle->proc_cls, NULL, 0, GNUNET_OK); 435 resp = create_json_response (NULL);
436 handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT);
388 cleanup_handle (handle); 437 cleanup_handle (handle);
389} 438}
390 439
@@ -392,6 +441,7 @@ static void
392set_finished (void *cls, const char *emsg) 441set_finished (void *cls, const char *emsg)
393{ 442{
394 struct RequestHandle *handle = cls; 443 struct RequestHandle *handle = cls;
444 struct MHD_Response *resp;
395 445
396 handle->op = NULL; 446 handle->op = NULL;
397 if (NULL != emsg) 447 if (NULL != emsg)
@@ -399,7 +449,8 @@ set_finished (void *cls, const char *emsg)
399 GNUNET_SCHEDULER_add_now (&do_error, handle); 449 GNUNET_SCHEDULER_add_now (&do_error, handle);
400 return; 450 return;
401 } 451 }
402 handle->proc (handle->proc_cls, NULL, 0, MHD_HTTP_NO_CONTENT); 452 resp = create_json_response (NULL);
453 handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT);
403 cleanup_handle (handle); 454 cleanup_handle (handle);
404} 455}
405 456
@@ -407,6 +458,7 @@ static void
407create_finished (void *cls, const char *emsg) 458create_finished (void *cls, const char *emsg)
408{ 459{
409 struct RequestHandle *handle = cls; 460 struct RequestHandle *handle = cls;
461 struct MHD_Response *resp;
410 462
411 handle->op = NULL; 463 handle->op = NULL;
412 if (NULL != emsg) 464 if (NULL != emsg)
@@ -414,7 +466,8 @@ create_finished (void *cls, const char *emsg)
414 GNUNET_SCHEDULER_add_now (&do_error, handle); 466 GNUNET_SCHEDULER_add_now (&do_error, handle);
415 return; 467 return;
416 } 468 }
417 handle->proc (handle->proc_cls, NULL, 0, MHD_HTTP_NO_CONTENT); 469 resp = create_json_response (NULL);
470 handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT);
418 cleanup_handle (handle); 471 cleanup_handle (handle);
419} 472}
420 473
@@ -423,23 +476,22 @@ ego_create_cont (struct RequestHandle *handle)
423{ 476{
424 const char* egoname; 477 const char* egoname;
425 char term_data[handle->data_size+1]; 478 char term_data[handle->data_size+1];
426 json_t *egoname_json; 479 struct EgoEntry *ego_entry;
480 struct MHD_Response *resp;
427 json_t *root_json; 481 json_t *root_json;
482 json_t *data_json;
483 json_t *type_json;
484 json_t *egoname_json;
428 json_error_t error; 485 json_error_t error;
429 struct EgoEntry *ego_entry;
430 486
431 if (strlen (API_NAMESPACE) != strlen (handle->url)) 487 if (strlen (API_NAMESPACE) != strlen (handle->url))
432 { 488 {
433 GNUNET_break(0); 489 GNUNET_SCHEDULER_add_now (&do_error, handle);
434 handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
435 cleanup_handle (handle);
436 return; 490 return;
437 } 491 }
438 if (0 >= handle->data_size) 492 if (0 >= handle->data_size)
439 { 493 {
440 GNUNET_break(0); 494 GNUNET_SCHEDULER_add_now (&do_error, handle);
441 handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
442 cleanup_handle (handle);
443 return; 495 return;
444 } 496 }
445 497
@@ -449,19 +501,36 @@ ego_create_cont (struct RequestHandle *handle)
449 501
450 if ((NULL == root_json) || !json_is_object (root_json)) 502 if ((NULL == root_json) || !json_is_object (root_json))
451 { 503 {
452 GNUNET_break(0); 504 GNUNET_SCHEDULER_add_now (&do_error, handle);
453 handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR); 505 return;
506 }
507 data_json = json_object_get (root_json, JSON_API_TYPE_DATA);
508 if ((NULL == data_json) || !json_is_object (data_json))
509 {
510 GNUNET_SCHEDULER_add_now (&do_error, handle);
511 return;
512 }
513 type_json = json_object_get (data_json, "type");
514 if (!json_is_string (type_json) ||
515 (0 != strcmp (JSON_API_TYPE_EGO, json_string_value (type_json))))
516 {
517 json_decref (data_json);
518 json_decref (root_json);
519 resp = create_json_response (NULL);
520 handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT);
454 cleanup_handle (handle); 521 cleanup_handle (handle);
455 return; 522 return;
456 } 523 }
457 egoname_json = json_object_get (root_json, "ego"); 524 json_decref (type_json);
525 egoname_json = json_object_get (data_json, "id");
458 if (!json_is_string (egoname_json)) 526 if (!json_is_string (egoname_json))
459 { 527 {
460 GNUNET_break(0); 528 json_decref (data_json);
461 handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR); 529 json_decref (root_json);
462 cleanup_handle (handle); 530 GNUNET_SCHEDULER_add_now (&do_error, handle);
463 return; 531 return;
464 } 532 }
533
465 egoname = json_string_value (egoname_json); 534 egoname = json_string_value (egoname_json);
466 for (ego_entry = handle->ego_head; 535 for (ego_entry = handle->ego_head;
467 NULL != ego_entry; 536 NULL != ego_entry;
@@ -470,14 +539,17 @@ ego_create_cont (struct RequestHandle *handle)
470 if (0 == strcasecmp (egoname, ego_entry->identifier)) 539 if (0 == strcasecmp (egoname, ego_entry->identifier))
471 { 540 {
472 json_decref (egoname_json); 541 json_decref (egoname_json);
542 json_decref (data_json);
473 json_decref (root_json); 543 json_decref (root_json);
474 handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR); 544 resp = create_json_response (NULL);
545 handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT);
475 cleanup_handle (handle); 546 cleanup_handle (handle);
476 return; 547 return;
477 } 548 }
478 } 549 }
479 GNUNET_asprintf (&handle->name, "%s", egoname); 550 GNUNET_asprintf (&handle->name, "%s", egoname);
480 json_decref (egoname_json); 551 json_decref (egoname_json);
552 json_decref (data_json);
481 json_decref (root_json); 553 json_decref (root_json);
482 handle->op = GNUNET_IDENTITY_create (handle->identity_handle, 554 handle->op = GNUNET_IDENTITY_create (handle->identity_handle,
483 handle->name, 555 handle->name,
@@ -492,6 +564,7 @@ subsys_set_cont (struct RequestHandle *handle)
492 const char *subsys; 564 const char *subsys;
493 char term_data[handle->data_size+1]; 565 char term_data[handle->data_size+1];
494 struct EgoEntry *ego_entry; 566 struct EgoEntry *ego_entry;
567 struct MHD_Response *resp;
495 int ego_exists = GNUNET_NO; 568 int ego_exists = GNUNET_NO;
496 json_t *root_json; 569 json_t *root_json;
497 json_t *data_json; 570 json_t *data_json;
@@ -502,9 +575,7 @@ subsys_set_cont (struct RequestHandle *handle)
502 575
503 if (strlen (API_NAMESPACE) > strlen (handle->url)) 576 if (strlen (API_NAMESPACE) > strlen (handle->url))
504 { 577 {
505 GNUNET_break(0); 578 GNUNET_SCHEDULER_add_now (&do_error, handle);
506 handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
507 cleanup_handle (handle);
508 return; 579 return;
509 } 580 }
510 581
@@ -521,17 +592,15 @@ subsys_set_cont (struct RequestHandle *handle)
521 } 592 }
522 if (GNUNET_NO == ego_exists) 593 if (GNUNET_NO == ego_exists)
523 { 594 {
524 GNUNET_break(0); 595 resp = create_json_response (NULL);
525 handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR); 596 handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND);
526 cleanup_handle (handle); 597 cleanup_handle (handle);
527 return; 598 return;
528 } 599 }
529 600
530 if (0 >= handle->data_size) 601 if (0 >= handle->data_size)
531 { 602 {
532 GNUNET_break(0); 603 GNUNET_SCHEDULER_add_now (&do_error, handle);
533 handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
534 cleanup_handle (handle);
535 return; 604 return;
536 } 605 }
537 606
@@ -541,16 +610,14 @@ subsys_set_cont (struct RequestHandle *handle)
541 610
542 if ((NULL == root_json) || !json_is_object (root_json)) 611 if ((NULL == root_json) || !json_is_object (root_json))
543 { 612 {
544 handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR); 613 GNUNET_SCHEDULER_add_now (&do_error, handle);
545 cleanup_handle (handle);
546 return; 614 return;
547 } 615 }
548 data_json = json_object_get (root_json, "data"); 616 data_json = json_object_get (root_json, "data");
549 if (!json_is_object (data_json)) 617 if (!json_is_object (data_json))
550 { 618 {
551 json_decref (root_json); 619 json_decref (root_json);
552 handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR); 620 GNUNET_SCHEDULER_add_now (&do_error, handle);
553 cleanup_handle (handle);
554 return; 621 return;
555 } 622 }
556 id_json = json_object_get (data_json, "id"); 623 id_json = json_object_get (data_json, "id");
@@ -559,21 +626,18 @@ subsys_set_cont (struct RequestHandle *handle)
559 { 626 {
560 json_decref (root_json); 627 json_decref (root_json);
561 json_decref (data_json); 628 json_decref (data_json);
562 handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR); 629 GNUNET_SCHEDULER_add_now (&do_error, handle);
563 cleanup_handle (handle);
564 return; 630 return;
565 } 631 }
566 json_decref (id_json); 632 json_decref (id_json);
567 633
568 type_json = json_object_get (data_json, "type"); 634 type_json = json_object_get (data_json, "type");
569 if (!json_is_string (type_json) || 635 if (!json_is_string (type_json) ||
570 (0 != strcmp (JSON_API_TYPE_EGO, json_string_value (type_json)))) 636 (0 != strcmp (JSON_API_TYPE_EGO, json_string_value (type_json))))
571 { 637 {
572 json_decref (root_json); 638 json_decref (root_json);
573 json_decref (data_json); 639 json_decref (data_json);
574 handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR); 640 GNUNET_SCHEDULER_add_now (&do_error, handle);
575 cleanup_handle (handle);
576 GNUNET_break (0);
577 return; 641 return;
578 } 642 }
579 json_decref (type_json); 643 json_decref (type_json);
@@ -583,8 +647,7 @@ subsys_set_cont (struct RequestHandle *handle)
583 { 647 {
584 json_decref (root_json); 648 json_decref (root_json);
585 json_decref (data_json); 649 json_decref (data_json);
586 handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR); 650 GNUNET_SCHEDULER_add_now (&do_error, handle);
587 cleanup_handle (handle);
588 return; 651 return;
589 } 652 }
590 subsys = json_string_value (subsys_json); 653 subsys = json_string_value (subsys_json);
@@ -603,13 +666,12 @@ ego_delete_cont (struct RequestHandle *handle)
603{ 666{
604 const char *egoname; 667 const char *egoname;
605 struct EgoEntry *ego_entry; 668 struct EgoEntry *ego_entry;
669 struct MHD_Response *resp;
606 int ego_exists = GNUNET_NO; 670 int ego_exists = GNUNET_NO;
607 671
608 if (strlen (API_NAMESPACE) >= strlen (handle->url)) 672 if (strlen (API_NAMESPACE) >= strlen (handle->url))
609 { 673 {
610 GNUNET_break(0); 674 GNUNET_SCHEDULER_add_now (&do_error, handle);
611 handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
612 cleanup_handle (handle);
613 return; 675 return;
614 } 676 }
615 677
@@ -626,8 +688,8 @@ ego_delete_cont (struct RequestHandle *handle)
626 } 688 }
627 if (GNUNET_NO == ego_exists) 689 if (GNUNET_NO == ego_exists)
628 { 690 {
629 GNUNET_break(0); 691 resp = create_json_response (NULL);
630 handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR); 692 handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND);
631 cleanup_handle (handle); 693 cleanup_handle (handle);
632 return; 694 return;
633 } 695 }