diff options
author | Martin Schanzenbach <mschanzenbach@posteo.de> | 2015-03-13 13:44:37 +0000 |
---|---|---|
committer | Martin Schanzenbach <mschanzenbach@posteo.de> | 2015-03-13 13:44:37 +0000 |
commit | eed977f3ddf01bb72b2b365badd2836fb7ae940c (patch) | |
tree | 7c5917ab5275bf87c2801dcf94aca9196bd18697 /src/identity/plugin_rest_identity.c | |
parent | 8aa21ee7de6c4a6cb9c1e132ab4218d3d10e0e30 (diff) | |
download | gnunet-eed977f3ddf01bb72b2b365badd2836fb7ae940c.tar.gz gnunet-eed977f3ddf01bb72b2b365badd2836fb7ae940c.zip |
-towards JSON API compat
Diffstat (limited to 'src/identity/plugin_rest_identity.c')
-rw-r--r-- | src/identity/plugin_rest_identity.c | 109 |
1 files changed, 80 insertions, 29 deletions
diff --git a/src/identity/plugin_rest_identity.c b/src/identity/plugin_rest_identity.c index 3c5c6946e..0d55a4cc7 100644 --- a/src/identity/plugin_rest_identity.c +++ b/src/identity/plugin_rest_identity.c | |||
@@ -32,7 +32,7 @@ | |||
32 | 32 | ||
33 | #define API_NAMESPACE "/identity" | 33 | #define API_NAMESPACE "/identity" |
34 | 34 | ||
35 | #define EGO_NAMESPACE "/identity" | 35 | #define EGO_NAMESPACE "/identity/egos" |
36 | 36 | ||
37 | #define ID_REST_STATE_INIT 0 | 37 | #define ID_REST_STATE_INIT 0 |
38 | 38 | ||
@@ -40,6 +40,10 @@ | |||
40 | 40 | ||
41 | #define URL_PARAM_SUBSYS "service" | 41 | #define URL_PARAM_SUBSYS "service" |
42 | 42 | ||
43 | #define JSON_API_TYPE_EGO "ego" | ||
44 | |||
45 | #define JSON_API_TYPE_DATA "data" | ||
46 | |||
43 | /** | 47 | /** |
44 | * @brief struct returned by the initialization function of the plugin | 48 | * @brief struct returned by the initialization function of the plugin |
45 | */ | 49 | */ |
@@ -145,7 +149,7 @@ struct RequestHandle | |||
145 | /** | 149 | /** |
146 | * The url | 150 | * The url |
147 | */ | 151 | */ |
148 | const char *url; | 152 | char *url; |
149 | 153 | ||
150 | /** | 154 | /** |
151 | * The data from the REST request | 155 | * The data from the REST request |
@@ -185,6 +189,8 @@ cleanup_handle (struct RequestHandle *handle) | |||
185 | GNUNET_IDENTITY_disconnect (handle->identity_handle); | 189 | GNUNET_IDENTITY_disconnect (handle->identity_handle); |
186 | if (NULL != handle->subsys) | 190 | if (NULL != handle->subsys) |
187 | GNUNET_free (handle->subsys); | 191 | GNUNET_free (handle->subsys); |
192 | if (NULL != handle->url) | ||
193 | GNUNET_free (handle->url); | ||
188 | for (ego_entry = handle->ego_head; | 194 | for (ego_entry = handle->ego_head; |
189 | NULL != ego_entry;) | 195 | NULL != ego_entry;) |
190 | { | 196 | { |
@@ -223,12 +229,12 @@ get_ego_for_subsys (void *cls, | |||
223 | char *result_str; | 229 | char *result_str; |
224 | char *keystring; | 230 | char *keystring; |
225 | json_t *ego_json; | 231 | json_t *ego_json; |
226 | json_t *ego_arr; | 232 | json_t *root_json; |
227 | 233 | ||
228 | ego_arr = json_array (); | 234 | root_json = json_object (); |
229 | 235 | ||
230 | //Return all egos | 236 | //Return all egos |
231 | for (ego_entry = handle->ego_head; | 237 | for (ego_entry = handle->ego_head; |
232 | NULL != ego_entry; | 238 | NULL != ego_entry; |
233 | ego_entry = ego_entry->next) | 239 | ego_entry = ego_entry->next) |
234 | { | 240 | { |
@@ -238,20 +244,46 @@ get_ego_for_subsys (void *cls, | |||
238 | continue; | 244 | continue; |
239 | ego_json = json_object (); | 245 | ego_json = json_object (); |
240 | keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&ego_entry->pk); | 246 | keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&ego_entry->pk); |
241 | json_object_set_new (ego_json, "identity", json_string (ego_entry->identifier)); | 247 | json_object_set_new (ego_json, "id", json_string (ego_entry->identifier)); |
248 | json_object_set_new (ego_json, "type", json_string (JSON_API_TYPE_EGO)); | ||
242 | json_object_set_new (ego_json, "key", json_string (keystring)); | 249 | json_object_set_new (ego_json, "key", json_string (keystring)); |
243 | json_array_append (ego_arr, ego_json); | ||
244 | json_decref (ego_json); | ||
245 | GNUNET_free (keystring); | 250 | GNUNET_free (keystring); |
251 | break; | ||
252 | } | ||
253 | if (NULL == ego_json) | ||
254 | { | ||
255 | json_decref (root_json); | ||
256 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
257 | return; | ||
246 | } | 258 | } |
247 | result_str = json_dumps (ego_arr, JSON_COMPACT); | 259 | json_object_set (root_json, JSON_API_TYPE_DATA, ego_json); |
260 | result_str = json_dumps (root_json, JSON_COMPACT); | ||
248 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); | 261 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); |
249 | json_decref (ego_arr); | 262 | json_decref (ego_json); |
263 | json_decref (root_json); | ||
250 | handle->proc (handle->proc_cls, result_str, strlen (result_str), GNUNET_OK); | 264 | handle->proc (handle->proc_cls, result_str, strlen (result_str), GNUNET_OK); |
251 | GNUNET_free (result_str); | 265 | GNUNET_free (result_str); |
252 | cleanup_handle (handle); | 266 | cleanup_handle (handle); |
253 | } | 267 | } |
254 | 268 | ||
269 | int | ||
270 | check_namespace (const char *url, const char *ns) | ||
271 | { | ||
272 | if (0 != strncmp (EGO_NAMESPACE, url, strlen (EGO_NAMESPACE))) | ||
273 | { | ||
274 | return GNUNET_NO; | ||
275 | } | ||
276 | |||
277 | if ((strlen (EGO_NAMESPACE) < strlen (url)) && | ||
278 | (url[strlen (EGO_NAMESPACE)] != '/')) | ||
279 | { | ||
280 | return GNUNET_NO; | ||
281 | } | ||
282 | return GNUNET_YES; | ||
283 | |||
284 | |||
285 | } | ||
286 | |||
255 | void | 287 | void |
256 | ego_info_response (struct RequestHandle *handle) | 288 | ego_info_response (struct RequestHandle *handle) |
257 | { | 289 | { |
@@ -263,16 +295,16 @@ ego_info_response (struct RequestHandle *handle) | |||
263 | struct GNUNET_HashCode key; | 295 | struct GNUNET_HashCode key; |
264 | json_t *ego_arr; | 296 | json_t *ego_arr; |
265 | json_t *ego_json; | 297 | json_t *ego_json; |
298 | json_t *root_json; | ||
266 | 299 | ||
267 | if (strlen (EGO_NAMESPACE) > strlen (handle->url)) | 300 | if (GNUNET_NO == check_namespace (handle->url, EGO_NAMESPACE)) |
268 | { | 301 | { |
269 | handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR); | 302 | handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR); |
270 | cleanup_handle (handle); | 303 | cleanup_handle (handle); |
271 | GNUNET_break (0); | 304 | GNUNET_break (0); |
272 | return; | 305 | return; |
273 | } | 306 | } |
274 | 307 | if ( (strlen (EGO_NAMESPACE) == strlen (handle->url) )) { | |
275 | if ( (strlen (EGO_NAMESPACE) + 1 >= strlen (handle->url) )) { | ||
276 | GNUNET_CRYPTO_hash (URL_PARAM_SUBSYS, strlen (URL_PARAM_SUBSYS), &key); | 308 | GNUNET_CRYPTO_hash (URL_PARAM_SUBSYS, strlen (URL_PARAM_SUBSYS), &key); |
277 | if ( GNUNET_YES == | 309 | if ( GNUNET_YES == |
278 | GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map, | 310 | GNUNET_CONTAINER_multihashmap_contains (handle->conndata_handle->url_param_map, |
@@ -283,7 +315,7 @@ ego_info_response (struct RequestHandle *handle) | |||
283 | if (NULL != subsys_val) | 315 | if (NULL != subsys_val) |
284 | { | 316 | { |
285 | GNUNET_asprintf (&handle->subsys, "%s", subsys_val); | 317 | GNUNET_asprintf (&handle->subsys, "%s", subsys_val); |
286 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Looking for %s's ego\n", subsys_val); | 318 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking for %s's ego\n", subsys_val); |
287 | handle->op = GNUNET_IDENTITY_get (handle->identity_handle, | 319 | handle->op = GNUNET_IDENTITY_get (handle->identity_handle, |
288 | handle->subsys, | 320 | handle->subsys, |
289 | &get_ego_for_subsys, | 321 | &get_ego_for_subsys, |
@@ -292,12 +324,11 @@ ego_info_response (struct RequestHandle *handle) | |||
292 | } | 324 | } |
293 | } | 325 | } |
294 | } | 326 | } |
295 | |||
296 | ego_arr = json_array (); | 327 | ego_arr = json_array (); |
328 | root_json = json_object (); | ||
329 | egoname = &handle->url[strlen (EGO_NAMESPACE)+1]; | ||
297 | 330 | ||
298 | egoname = &handle->url[strlen (EGO_NAMESPACE)]; | 331 | if (strlen (EGO_NAMESPACE) == strlen (handle->url)) |
299 | |||
300 | if (strlen (EGO_NAMESPACE) + 1 >= strlen (handle->url)) | ||
301 | { | 332 | { |
302 | egoname = NULL; | 333 | egoname = NULL; |
303 | } | 334 | } |
@@ -311,15 +342,31 @@ ego_info_response (struct RequestHandle *handle) | |||
311 | continue; | 342 | continue; |
312 | ego_json = json_object (); | 343 | ego_json = json_object (); |
313 | keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&ego_entry->pk); | 344 | keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&ego_entry->pk); |
314 | json_object_set_new (ego_json, "identity", json_string (ego_entry->identifier)); | 345 | json_object_set_new (ego_json, "id", json_string (ego_entry->identifier)); |
315 | json_object_set_new (ego_json, "key", json_string (keystring)); | 346 | json_object_set_new (ego_json, "key", json_string (keystring)); |
316 | json_array_append (ego_arr, ego_json); | 347 | json_object_set_new (ego_json, "type", json_string (JSON_API_TYPE_EGO)); |
317 | json_decref (ego_json); | ||
318 | GNUNET_free (keystring); | 348 | GNUNET_free (keystring); |
349 | if (NULL == egoname) | ||
350 | { | ||
351 | GNUNET_break (0); | ||
352 | json_array_append (ego_arr, ego_json); | ||
353 | json_decref (ego_json); | ||
354 | } | ||
355 | else | ||
356 | break; | ||
319 | } | 357 | } |
320 | result_str = json_dumps (ego_arr, JSON_COMPACT); | 358 | GNUNET_break (0); |
321 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); | 359 | if (NULL == egoname) |
360 | json_object_set (root_json, JSON_API_TYPE_DATA, ego_arr); | ||
361 | else | ||
362 | json_object_set (root_json, JSON_API_TYPE_DATA, ego_json); | ||
363 | |||
364 | result_str = json_dumps (root_json, JSON_COMPACT); | ||
322 | json_decref (ego_arr); | 365 | json_decref (ego_arr); |
366 | if (NULL != egoname) | ||
367 | json_decref (ego_json); | ||
368 | |||
369 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); | ||
323 | handle->proc (handle->proc_cls, result_str, strlen (result_str), GNUNET_OK); | 370 | handle->proc (handle->proc_cls, result_str, strlen (result_str), GNUNET_OK); |
324 | GNUNET_free (result_str); | 371 | GNUNET_free (result_str); |
325 | cleanup_handle (handle); | 372 | cleanup_handle (handle); |
@@ -419,7 +466,7 @@ subsys_set_cont (struct RequestHandle *handle) | |||
419 | json_t *subsys_json; | 466 | json_t *subsys_json; |
420 | json_error_t error; | 467 | json_error_t error; |
421 | 468 | ||
422 | if (strlen (API_NAMESPACE)+1 >= strlen (handle->url)) | 469 | if (strlen (API_NAMESPACE) >= strlen (handle->url)) |
423 | { | 470 | { |
424 | GNUNET_break(0); | 471 | GNUNET_break(0); |
425 | handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR); | 472 | handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR); |
@@ -491,7 +538,7 @@ ego_delete_cont (struct RequestHandle *handle) | |||
491 | struct EgoEntry *ego_entry; | 538 | struct EgoEntry *ego_entry; |
492 | int ego_exists = GNUNET_NO; | 539 | int ego_exists = GNUNET_NO; |
493 | 540 | ||
494 | if (strlen (API_NAMESPACE)+1 >= strlen (handle->url)) | 541 | if (strlen (API_NAMESPACE) >= strlen (handle->url)) |
495 | { | 542 | { |
496 | GNUNET_break(0); | 543 | GNUNET_break(0); |
497 | handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR); | 544 | handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR); |
@@ -623,6 +670,12 @@ rest_identity_process_request(struct RestConnectionDataHandle *conndata_handle, | |||
623 | handle->proc = proc; | 670 | handle->proc = proc; |
624 | handle->state = ID_REST_STATE_INIT; | 671 | handle->state = ID_REST_STATE_INIT; |
625 | handle->conndata_handle = conndata_handle; | 672 | handle->conndata_handle = conndata_handle; |
673 | handle->data = conndata_handle->data; | ||
674 | handle->data_size = conndata_handle->data_size; | ||
675 | handle->method = conndata_handle->method; | ||
676 | GNUNET_asprintf (&handle->url, "%s", conndata_handle->url); | ||
677 | if (handle->url[strlen (handle->url)-1] == '/') | ||
678 | handle->url[strlen (handle->url)-1] = '\0'; | ||
626 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 679 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
627 | "Connecting...\n"); | 680 | "Connecting...\n"); |
628 | handle->identity_handle = GNUNET_IDENTITY_connect (cfg, | 681 | handle->identity_handle = GNUNET_IDENTITY_connect (cfg, |
@@ -632,12 +685,10 @@ rest_identity_process_request(struct RestConnectionDataHandle *conndata_handle, | |||
632 | GNUNET_SCHEDULER_add_delayed (handle->timeout, | 685 | GNUNET_SCHEDULER_add_delayed (handle->timeout, |
633 | &do_error, | 686 | &do_error, |
634 | handle); | 687 | handle); |
635 | handle->data = conndata_handle->data; | 688 | |
636 | handle->data_size = conndata_handle->data_size; | 689 | |
637 | handle->url = conndata_handle->url; | ||
638 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 690 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
639 | "Connected\n"); | 691 | "Connected\n"); |
640 | handle->method = conndata_handle->method; | ||
641 | } | 692 | } |
642 | 693 | ||
643 | /** | 694 | /** |