diff options
author | Martin Schanzenbach <mschanzenbach@posteo.de> | 2015-03-25 15:58:38 +0000 |
---|---|---|
committer | Martin Schanzenbach <mschanzenbach@posteo.de> | 2015-03-25 15:58:38 +0000 |
commit | 08cd39e74d35cec73f54de9826961a97d8a8ea0d (patch) | |
tree | f85511b479dc9480427dffaaaa5425ebc221c87c /src/identity/plugin_rest_identity.c | |
parent | 00f87691363acaf94f533793654e610e46bf4e1d (diff) | |
download | gnunet-08cd39e74d35cec73f54de9826961a97d8a8ea0d.tar.gz gnunet-08cd39e74d35cec73f54de9826961a97d8a8ea0d.zip |
-start a lib for REST and json:api
Diffstat (limited to 'src/identity/plugin_rest_identity.c')
-rw-r--r-- | src/identity/plugin_rest_identity.c | 123 |
1 files changed, 33 insertions, 90 deletions
diff --git a/src/identity/plugin_rest_identity.c b/src/identity/plugin_rest_identity.c index 1bc5f6fea..ea611128d 100644 --- a/src/identity/plugin_rest_identity.c +++ b/src/identity/plugin_rest_identity.c | |||
@@ -27,6 +27,7 @@ | |||
27 | #include "platform.h" | 27 | #include "platform.h" |
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 "microhttpd.h" | 31 | #include "microhttpd.h" |
31 | #include <jansson.h> | 32 | #include <jansson.h> |
32 | 33 | ||
@@ -40,9 +41,9 @@ | |||
40 | 41 | ||
41 | #define URL_PARAM_SUBSYS "service" | 42 | #define URL_PARAM_SUBSYS "service" |
42 | 43 | ||
43 | #define JSON_API_TYPE_EGO "ego" | 44 | #define GNUNET_REST_JSONAPI_IDENTITY_EGO "ego" |
44 | 45 | ||
45 | #define JSON_API_TYPE_DATA "data" | 46 | #define GNUNET_REST_JSONAPI_IDENTITY_KEY "key" |
46 | 47 | ||
47 | /** | 48 | /** |
48 | * @brief struct returned by the initialization function of the plugin | 49 | * @brief struct returned by the initialization function of the plugin |
@@ -168,27 +169,6 @@ struct RequestHandle | |||
168 | 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 | */ | ||
177 | struct MHD_Response* | ||
178 | create_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 | 172 | ||
193 | /** | 173 | /** |
194 | * Cleanup lookup handle | 174 | * Cleanup lookup handle |
@@ -236,7 +216,7 @@ do_error (void *cls, | |||
236 | const struct GNUNET_SCHEDULER_TaskContext *tc) | 216 | const struct GNUNET_SCHEDULER_TaskContext *tc) |
237 | { | 217 | { |
238 | struct RequestHandle *handle = cls; | 218 | struct RequestHandle *handle = cls; |
239 | struct MHD_Response *resp = create_json_response (NULL); | 219 | struct MHD_Response *resp = GNUNET_REST_create_json_response (NULL); |
240 | handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST); | 220 | handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST); |
241 | cleanup_handle (handle); | 221 | cleanup_handle (handle); |
242 | } | 222 | } |
@@ -277,7 +257,7 @@ get_ego_for_subsys (void *cls, | |||
277 | ego_json = json_object (); | 257 | ego_json = json_object (); |
278 | keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&ego_entry->pk); | 258 | keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&ego_entry->pk); |
279 | json_object_set_new (ego_json, "id", json_string (ego_entry->identifier)); | 259 | json_object_set_new (ego_json, "id", json_string (ego_entry->identifier)); |
280 | json_object_set_new (ego_json, "type", json_string (JSON_API_TYPE_EGO)); | 260 | json_object_set_new (ego_json, "type", json_string (GNUNET_REST_JSONAPI_IDENTITY_EGO)); |
281 | json_object_set_new (ego_json, "key", json_string (keystring)); | 261 | json_object_set_new (ego_json, "key", json_string (keystring)); |
282 | GNUNET_free (keystring); | 262 | GNUNET_free (keystring); |
283 | break; | 263 | break; |
@@ -288,43 +268,18 @@ get_ego_for_subsys (void *cls, | |||
288 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 268 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
289 | return; | 269 | return; |
290 | } | 270 | } |
291 | json_object_set (root_json, JSON_API_TYPE_DATA, ego_json); | 271 | json_object_set (root_json, GNUNET_REST_JSONAPI_KEY_DATA, ego_json); |
292 | result_str = json_dumps (root_json, JSON_COMPACT); | 272 | result_str = json_dumps (root_json, JSON_COMPACT); |
293 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); | 273 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); |
294 | json_decref (ego_json); | 274 | json_decref (ego_json); |
295 | json_decref (root_json); | 275 | json_decref (root_json); |
296 | resp = create_json_response (result_str); | 276 | resp = GNUNET_REST_create_json_response (result_str); |
297 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | 277 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); |
298 | GNUNET_free (result_str); | 278 | GNUNET_free (result_str); |
299 | cleanup_handle (handle); | 279 | cleanup_handle (handle); |
300 | } | 280 | } |
301 | 281 | ||
302 | /** | 282 | /** |
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 | */ | ||
309 | int | ||
310 | check_namespace (const char *url, const char *ns) | ||
311 | { | ||
312 | if (0 != strncmp (EGO_NAMESPACE, url, strlen (EGO_NAMESPACE))) | ||
313 | { | ||
314 | return GNUNET_NO; | ||
315 | } | ||
316 | |||
317 | if ((strlen (EGO_NAMESPACE) < strlen (url)) && | ||
318 | (url[strlen (EGO_NAMESPACE)] != '/')) | ||
319 | { | ||
320 | return GNUNET_NO; | ||
321 | } | ||
322 | return GNUNET_YES; | ||
323 | |||
324 | |||
325 | } | ||
326 | |||
327 | /** | ||
328 | * Create a response with requested ego(s) | 283 | * Create a response with requested ego(s) |
329 | * | 284 | * |
330 | * @param handle the RequestHandle | 285 | * @param handle the RequestHandle |
@@ -339,18 +294,19 @@ ego_info_response (struct RequestHandle *handle) | |||
339 | struct EgoEntry *ego_entry; | 294 | struct EgoEntry *ego_entry; |
340 | struct GNUNET_HashCode key; | 295 | struct GNUNET_HashCode key; |
341 | struct MHD_Response *resp; | 296 | struct MHD_Response *resp; |
342 | json_t *ego_arr; | 297 | struct JsonApiResponse *json_response; |
343 | json_t *ego_json; | 298 | struct JsonApiResource *json_resource; |
344 | json_t *root_json; | 299 | json_t *key_str; |
345 | 300 | ||
346 | if (GNUNET_NO == check_namespace (handle->url, EGO_NAMESPACE)) | 301 | if (GNUNET_NO == GNUNET_REST_namespace_match (handle->url, EGO_NAMESPACE)) |
347 | { | 302 | { |
348 | resp = create_json_response (NULL); | 303 | resp = GNUNET_REST_create_json_response (NULL); |
349 | handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST); | 304 | handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST); |
350 | cleanup_handle (handle); | 305 | cleanup_handle (handle); |
351 | GNUNET_break (0); | 306 | GNUNET_break (0); |
352 | return; | 307 | return; |
353 | } | 308 | } |
309 | json_response = GNUNET_REST_jsonapi_response_new (); | ||
354 | if ( (strlen (EGO_NAMESPACE) == strlen (handle->url) )) { | 310 | if ( (strlen (EGO_NAMESPACE) == strlen (handle->url) )) { |
355 | GNUNET_CRYPTO_hash (URL_PARAM_SUBSYS, strlen (URL_PARAM_SUBSYS), &key); | 311 | GNUNET_CRYPTO_hash (URL_PARAM_SUBSYS, strlen (URL_PARAM_SUBSYS), &key); |
356 | if ( GNUNET_YES == | 312 | if ( GNUNET_YES == |
@@ -371,8 +327,7 @@ ego_info_response (struct RequestHandle *handle) | |||
371 | } | 327 | } |
372 | } | 328 | } |
373 | } | 329 | } |
374 | ego_arr = json_array (); | 330 | json_response = GNUNET_REST_jsonapi_response_new (); |
375 | root_json = json_object (); | ||
376 | egoname = &handle->url[strlen (EGO_NAMESPACE)+1]; | 331 | egoname = &handle->url[strlen (EGO_NAMESPACE)+1]; |
377 | 332 | ||
378 | if (strlen (EGO_NAMESPACE) == strlen (handle->url)) | 333 | if (strlen (EGO_NAMESPACE) == strlen (handle->url)) |
@@ -387,36 +342,24 @@ ego_info_response (struct RequestHandle *handle) | |||
387 | { | 342 | { |
388 | if ( (NULL != egoname) && (0 != strcmp (egoname, ego_entry->identifier)) ) | 343 | if ( (NULL != egoname) && (0 != strcmp (egoname, ego_entry->identifier)) ) |
389 | continue; | 344 | continue; |
390 | ego_json = json_object (); | ||
391 | keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&ego_entry->pk); | 345 | keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&ego_entry->pk); |
392 | json_object_set_new (ego_json, "id", json_string (ego_entry->identifier)); | 346 | json_resource = GNUNET_REST_jsonapi_resource_new (GNUNET_REST_JSONAPI_IDENTITY_EGO, ego_entry->identifier); |
393 | json_object_set_new (ego_json, "key", json_string (keystring)); | 347 | key_str = json_string (keystring); |
394 | json_object_set_new (ego_json, "type", json_string (JSON_API_TYPE_EGO)); | 348 | GNUNET_REST_jsonapi_resource_add_attr (json_resource, |
349 | GNUNET_REST_JSONAPI_IDENTITY_KEY, | ||
350 | key_str); | ||
351 | json_decref (key_str); | ||
395 | GNUNET_free (keystring); | 352 | GNUNET_free (keystring); |
396 | if (NULL == egoname) | 353 | GNUNET_REST_jsonapi_response_resource_add (json_response, json_resource); |
397 | { | ||
398 | json_array_append (ego_arr, ego_json); | ||
399 | json_decref (ego_json); | ||
400 | } | ||
401 | else | ||
402 | break; | ||
403 | } | 354 | } |
404 | if (NULL == egoname) | ||
405 | json_object_set (root_json, "egos", ego_arr); | ||
406 | else | ||
407 | json_object_set (root_json, "ego", ego_json); | ||
408 | 355 | ||
409 | result_str = json_dumps (root_json, JSON_COMPACT); | 356 | GNUNET_REST_jsonapi_data_serialize (json_response, &result_str); |
410 | json_decref (ego_arr); | ||
411 | if (NULL != egoname) | ||
412 | json_decref (ego_json); | ||
413 | 357 | ||
414 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); | 358 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); |
415 | resp = create_json_response (result_str); | 359 | resp = GNUNET_REST_create_json_response (result_str); |
416 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | 360 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); |
417 | GNUNET_free (result_str); | 361 | GNUNET_free (result_str); |
418 | cleanup_handle (handle); | 362 | cleanup_handle (handle); |
419 | |||
420 | } | 363 | } |
421 | 364 | ||
422 | static void | 365 | static void |
@@ -431,7 +374,7 @@ do_finished (void *cls, const char *emsg) | |||
431 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 374 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
432 | return; | 375 | return; |
433 | } | 376 | } |
434 | resp = create_json_response (NULL); | 377 | resp = GNUNET_REST_create_json_response (NULL); |
435 | handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT); | 378 | handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT); |
436 | cleanup_handle (handle); | 379 | cleanup_handle (handle); |
437 | } | 380 | } |
@@ -448,7 +391,7 @@ set_finished (void *cls, const char *emsg) | |||
448 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 391 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
449 | return; | 392 | return; |
450 | } | 393 | } |
451 | resp = create_json_response (NULL); | 394 | resp = GNUNET_REST_create_json_response (NULL); |
452 | handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT); | 395 | handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT); |
453 | cleanup_handle (handle); | 396 | cleanup_handle (handle); |
454 | } | 397 | } |
@@ -465,7 +408,7 @@ create_finished (void *cls, const char *emsg) | |||
465 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 408 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
466 | return; | 409 | return; |
467 | } | 410 | } |
468 | resp = create_json_response (NULL); | 411 | resp = GNUNET_REST_create_json_response (NULL); |
469 | handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT); | 412 | handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT); |
470 | cleanup_handle (handle); | 413 | cleanup_handle (handle); |
471 | } | 414 | } |
@@ -503,7 +446,7 @@ ego_create_cont (struct RequestHandle *handle) | |||
503 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 446 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
504 | return; | 447 | return; |
505 | } | 448 | } |
506 | data_json = json_object_get (root_json, JSON_API_TYPE_DATA); | 449 | data_json = json_object_get (root_json, GNUNET_REST_JSONAPI_KEY_DATA); |
507 | if ((NULL == data_json) || !json_is_object (data_json)) | 450 | if ((NULL == data_json) || !json_is_object (data_json)) |
508 | { | 451 | { |
509 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 452 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
@@ -511,11 +454,11 @@ ego_create_cont (struct RequestHandle *handle) | |||
511 | } | 454 | } |
512 | type_json = json_object_get (data_json, "type"); | 455 | type_json = json_object_get (data_json, "type"); |
513 | if (!json_is_string (type_json) || | 456 | if (!json_is_string (type_json) || |
514 | (0 != strcmp (JSON_API_TYPE_EGO, json_string_value (type_json)))) | 457 | (0 != strcmp (GNUNET_REST_JSONAPI_IDENTITY_EGO, json_string_value (type_json)))) |
515 | { | 458 | { |
516 | json_decref (data_json); | 459 | json_decref (data_json); |
517 | json_decref (root_json); | 460 | json_decref (root_json); |
518 | resp = create_json_response (NULL); | 461 | resp = GNUNET_REST_create_json_response (NULL); |
519 | handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); | 462 | handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); |
520 | cleanup_handle (handle); | 463 | cleanup_handle (handle); |
521 | return; | 464 | return; |
@@ -540,7 +483,7 @@ ego_create_cont (struct RequestHandle *handle) | |||
540 | json_decref (egoname_json); | 483 | json_decref (egoname_json); |
541 | json_decref (data_json); | 484 | json_decref (data_json); |
542 | json_decref (root_json); | 485 | json_decref (root_json); |
543 | resp = create_json_response (NULL); | 486 | resp = GNUNET_REST_create_json_response (NULL); |
544 | handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); | 487 | handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT); |
545 | cleanup_handle (handle); | 488 | cleanup_handle (handle); |
546 | return; | 489 | return; |
@@ -591,7 +534,7 @@ subsys_set_cont (struct RequestHandle *handle) | |||
591 | } | 534 | } |
592 | if (GNUNET_NO == ego_exists) | 535 | if (GNUNET_NO == ego_exists) |
593 | { | 536 | { |
594 | resp = create_json_response (NULL); | 537 | resp = GNUNET_REST_create_json_response (NULL); |
595 | handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND); | 538 | handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND); |
596 | cleanup_handle (handle); | 539 | cleanup_handle (handle); |
597 | return; | 540 | return; |
@@ -632,7 +575,7 @@ subsys_set_cont (struct RequestHandle *handle) | |||
632 | 575 | ||
633 | type_json = json_object_get (data_json, "type"); | 576 | type_json = json_object_get (data_json, "type"); |
634 | if (!json_is_string (type_json) || | 577 | if (!json_is_string (type_json) || |
635 | (0 != strcmp (JSON_API_TYPE_EGO, json_string_value (type_json)))) | 578 | (0 != strcmp (GNUNET_REST_JSONAPI_IDENTITY_EGO, json_string_value (type_json)))) |
636 | { | 579 | { |
637 | json_decref (root_json); | 580 | json_decref (root_json); |
638 | json_decref (data_json); | 581 | json_decref (data_json); |
@@ -687,7 +630,7 @@ ego_delete_cont (struct RequestHandle *handle) | |||
687 | } | 630 | } |
688 | if (GNUNET_NO == ego_exists) | 631 | if (GNUNET_NO == ego_exists) |
689 | { | 632 | { |
690 | resp = create_json_response (NULL); | 633 | resp = GNUNET_REST_create_json_response (NULL); |
691 | handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND); | 634 | handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND); |
692 | cleanup_handle (handle); | 635 | cleanup_handle (handle); |
693 | return; | 636 | return; |