aboutsummaryrefslogtreecommitdiff
path: root/src/gns/plugin_rest_gns.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/gns/plugin_rest_gns.c
parente2963510a62c68113535f885365137083d9f474c (diff)
downloadgnunet-7ae2282ee296b24e0132d155ece49ae87e21215b.tar.gz
gnunet-7ae2282ee296b24e0132d155ece49ae87e21215b.zip
-change API
Diffstat (limited to 'src/gns/plugin_rest_gns.c')
-rw-r--r--src/gns/plugin_rest_gns.c75
1 files changed, 51 insertions, 24 deletions
diff --git a/src/gns/plugin_rest_gns.c b/src/gns/plugin_rest_gns.c
index e837fe963..345298849 100644
--- a/src/gns/plugin_rest_gns.c
+++ b/src/gns/plugin_rest_gns.c
@@ -35,6 +35,14 @@
35 35
36#define API_NAMESPACE "/gns" 36#define API_NAMESPACE "/gns"
37 37
38#define GNUNET_REST_JSON_ATTR_ID "id"
39
40#define GNUNET_REST_JSON_ATTR_TYPE "type"
41
42#define GNUNET_GNS_JSON_RECORD_TYPE "rtype"
43
44#define GNUNET_REST_JSON_ATTR_DATA "data"
45
38/** 46/**
39 * @brief struct returned by the initialization function of the plugin 47 * @brief struct returned by the initialization function of the plugin
40 */ 48 */
@@ -190,6 +198,27 @@ cleanup_handle (struct LookupHandle *handle)
190 198
191 199
192/** 200/**
201 * Create s JSON Response for MHD
202 * TODO move to lib
203 * @param data the JSON to return (can be NULL)
204 * @return a MHD_Response handle
205 */
206struct MHD_Response*
207create_json_response (const char *data)
208{
209 size_t len;
210 if (NULL == data)
211 len = 0;
212 else
213 len = strlen (data);
214 struct MHD_Response *resp = MHD_create_response_from_buffer (len,
215 (void*)data,
216 MHD_RESPMEM_MUST_COPY);
217 MHD_add_response_header (resp,MHD_HTTP_HEADER_CONTENT_TYPE,"application/json");
218 return resp;
219}
220
221/**
193 * Task run on shutdown. Cleans up everything. 222 * Task run on shutdown. Cleans up everything.
194 * 223 *
195 * @param cls unused 224 * @param cls unused
@@ -200,7 +229,8 @@ do_error (void *cls,
200 const struct GNUNET_SCHEDULER_TaskContext *tc) 229 const struct GNUNET_SCHEDULER_TaskContext *tc)
201{ 230{
202 struct LookupHandle *handle = cls; 231 struct LookupHandle *handle = cls;
203 handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR); 232 struct MHD_Response *resp = create_json_response (NULL);
233 handle->proc (handle->proc_cls, resp, GNUNET_SYSERR);
204 cleanup_handle (handle); 234 cleanup_handle (handle);
205} 235}
206 236
@@ -265,20 +295,22 @@ process_lookup_result (void *cls, uint32_t rd_count,
265 const struct GNUNET_GNSRECORD_Data *rd) 295 const struct GNUNET_GNSRECORD_Data *rd)
266{ 296{
267 struct LookupHandle *handle = cls; 297 struct LookupHandle *handle = cls;
298 struct MHD_Response *resp;
268 uint32_t i; 299 uint32_t i;
269 char *result; 300 char *result;
270 json_t *result_root; 301 json_t *result_root;
271 json_t *result_name; 302 json_t *result_data;
272 json_t *result_array; 303 json_t *result_array;
273 json_t *record_obj; 304 json_t *record_obj;
274 305
275 result_root = json_object(); 306 result_root = json_object();
276 result_name = json_string (handle->name);
277 result_array = json_array(); 307 result_array = json_array();
278 json_object_set (result_root, "name", result_name); 308 result_data = json_object();
279 json_decref (result_name); 309 json_object_set_new (result_root, GNUNET_REST_JSON_ATTR_ID, json_string (handle->name));
310 json_object_set (result_root,
311 GNUNET_REST_JSON_ATTR_TYPE,
312 json_string (GNUNET_GNS_JSON_RECORD_TYPE));
280 handle->lookup_request = NULL; 313 handle->lookup_request = NULL;
281
282 for (i=0; i<rd_count; i++) 314 for (i=0; i<rd_count; i++)
283 { 315 {
284 if ( (rd[i].record_type != handle->type) && 316 if ( (rd[i].record_type != handle->type) &&
@@ -289,12 +321,15 @@ process_lookup_result (void *cls, uint32_t rd_count,
289 json_array_append (result_array, record_obj); 321 json_array_append (result_array, record_obj);
290 json_decref (record_obj); 322 json_decref (record_obj);
291 } 323 }
292 json_object_set (result_root, "query_result", result_array); 324 json_object_set (result_root, GNUNET_REST_JSON_ATTR_DATA, result_data);
325 json_decref (result_data);
326 json_object_set (result_data, "query_result", result_array);
293 json_decref (result_array); 327 json_decref (result_array);
294 result = json_dumps (result_root, JSON_COMPACT); 328 result = json_dumps (result_root, JSON_COMPACT);
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result); 329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result);
296 json_decref (result_root); 330 json_decref (result_root);
297 handle->proc (handle->proc_cls, result, strlen (result), GNUNET_OK); 331 resp = create_json_response (result);
332 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
298 GNUNET_free (result); 333 GNUNET_free (result);
299 cleanup_handle (handle); 334 cleanup_handle (handle);
300} 335}
@@ -329,10 +364,7 @@ lookup_with_keys (struct LookupHandle *handle, const struct GNUNET_CRYPTO_EcdsaP
329 } 364 }
330 else 365 else
331 { 366 {
332 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 367 GNUNET_SCHEDULER_add_now (&do_error, handle);
333 _("Please specify name to lookup!\n"));
334 handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR);
335 cleanup_handle (handle);
336 return; 368 return;
337 } 369 }
338} 370}
@@ -405,8 +437,7 @@ identity_zone_cb (void *cls,
405 { 437 {
406 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 438 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
407 _("Ego for not found, cannot perform lookup.\n")); 439 _("Ego for not found, cannot perform lookup.\n"));
408 handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR); 440 GNUNET_SCHEDULER_add_now (&do_error, handle);
409 cleanup_handle (handle);
410 return; 441 return;
411 } 442 }
412 else 443 else
@@ -443,8 +474,7 @@ identity_master_cb (void *cls,
443 { 474 {
444 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 475 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
445 _("Ego for `gns-master' not found, cannot perform lookup. Did you run gnunet-gns-import.sh?\n")); 476 _("Ego for `gns-master' not found, cannot perform lookup. Did you run gnunet-gns-import.sh?\n"));
446 handle->proc (handle->proc_cls, NULL, 0, GNUNET_SYSERR); 477 GNUNET_SCHEDULER_add_now (&do_error, handle);
447 cleanup_handle (handle);
448 return; 478 return;
449 } 479 }
450 GNUNET_IDENTITY_ego_get_public_key (ego, &handle->pkey); 480 GNUNET_IDENTITY_ego_get_public_key (ego, &handle->pkey);
@@ -501,6 +531,7 @@ parse_url (const char *url, struct LookupHandle *handle)
501 531
502/** 532/**
503 * Parse json from REST request 533 * Parse json from REST request
534 * TODO 1. this leaks 2. Rework JSON API. This is confusing
504 * 535 *
505 * @param data REST data 536 * @param data REST data
506 * @param data_size data size 537 * @param data_size data size
@@ -578,8 +609,7 @@ rest_gns_process_request(struct RestConnectionDataHandle *conndata_handle,
578 if (GNUNET_OK != parse_url (conndata_handle->url, handle)) 609 if (GNUNET_OK != parse_url (conndata_handle->url, handle))
579 { 610 {
580 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error parsing url...\n"); 611 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error parsing url...\n");
581 proc (proc_cls, NULL, 0, GNUNET_SYSERR); 612 GNUNET_SCHEDULER_add_now (&do_error, handle);
582 cleanup_handle (handle);
583 return; 613 return;
584 } 614 }
585 615
@@ -590,8 +620,7 @@ rest_gns_process_request(struct RestConnectionDataHandle *conndata_handle,
590 if (GNUNET_OK != parse_json (conndata_handle->data, conndata_handle->data_size, handle)) 620 if (GNUNET_OK != parse_json (conndata_handle->data, conndata_handle->data_size, handle))
591 { 621 {
592 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error parsing json...\n"); 622 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error parsing json...\n");
593 proc (proc_cls, NULL, 0, GNUNET_SYSERR); 623 GNUNET_SCHEDULER_add_now (&do_error, handle);
594 cleanup_handle (handle);
595 return; 624 return;
596 } 625 }
597 } 626 }
@@ -608,8 +637,7 @@ rest_gns_process_request(struct RestConnectionDataHandle *conndata_handle,
608 { 637 {
609 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 638 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
610 "Connecting to GNS failed\n"); 639 "Connecting to GNS failed\n");
611 proc (proc_cls, NULL, 0, GNUNET_SYSERR); 640 GNUNET_SCHEDULER_add_now (&do_error, handle);
612 cleanup_handle (handle);
613 return; 641 return;
614 } 642 }
615 643
@@ -620,8 +648,7 @@ rest_gns_process_request(struct RestConnectionDataHandle *conndata_handle,
620 strlen(handle->pkey_str), 648 strlen(handle->pkey_str),
621 &(handle->pkey))) 649 &(handle->pkey)))
622 { 650 {
623 proc (proc_cls, NULL, 0, GNUNET_SYSERR); 651 GNUNET_SCHEDULER_add_now (&do_error, handle);
624 cleanup_handle (handle);
625 return; 652 return;
626 } 653 }
627 lookup_with_public_key (handle); 654 lookup_with_public_key (handle);