aboutsummaryrefslogtreecommitdiff
path: root/src
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
parente2963510a62c68113535f885365137083d9f474c (diff)
downloadgnunet-7ae2282ee296b24e0132d155ece49ae87e21215b.tar.gz
gnunet-7ae2282ee296b24e0132d155ece49ae87e21215b.zip
-change API
Diffstat (limited to 'src')
-rw-r--r--src/gns/Makefile.am2
-rw-r--r--src/gns/plugin_rest_gns.c75
-rw-r--r--src/identity/plugin_rest_identity.c158
-rw-r--r--src/include/gnunet_rest_plugin.h7
-rw-r--r--src/rest/gnunet-rest-server.c7
5 files changed, 166 insertions, 83 deletions
diff --git a/src/gns/Makefile.am b/src/gns/Makefile.am
index 0d9ef5de5..d2ff0d14d 100644
--- a/src/gns/Makefile.am
+++ b/src/gns/Makefile.am
@@ -102,7 +102,7 @@ libgnunet_plugin_rest_gns_la_LIBADD = \
102 $(top_builddir)/src/gns/libgnunetgns.la \ 102 $(top_builddir)/src/gns/libgnunetgns.la \
103 $(top_builddir)/src/identity/libgnunetidentity.la \ 103 $(top_builddir)/src/identity/libgnunetidentity.la \
104 $(top_builddir)/src/util/libgnunetutil.la $(XLIBS) \ 104 $(top_builddir)/src/util/libgnunetutil.la $(XLIBS) \
105 $(LTLIBINTL) -ljansson 105 $(LTLIBINTL) -ljansson -lmicrohttpd
106libgnunet_plugin_rest_gns_la_LDFLAGS = \ 106libgnunet_plugin_rest_gns_la_LDFLAGS = \
107 $(GN_PLUGIN_LDFLAGS) 107 $(GN_PLUGIN_LDFLAGS)
108 108
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);
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 }
diff --git a/src/include/gnunet_rest_plugin.h b/src/include/gnunet_rest_plugin.h
index a7318e628..1d5a2db59 100644
--- a/src/include/gnunet_rest_plugin.h
+++ b/src/include/gnunet_rest_plugin.h
@@ -27,6 +27,7 @@
27#define GNUNET_REST_PLUGIN_H 27#define GNUNET_REST_PLUGIN_H
28 28
29#include "gnunet_util_lib.h" 29#include "gnunet_util_lib.h"
30#include "microhttpd.h"
30 31
31#ifdef __cplusplus 32#ifdef __cplusplus
32extern "C" 33extern "C"
@@ -40,13 +41,11 @@ extern "C"
40 * Iterator called on obtained result for a REST result. 41 * Iterator called on obtained result for a REST result.
41 * 42 *
42 * @param cls closure 43 * @param cls closure
43 * @param data REST result 44 * @param resp the response
44 * @param data_len length of result
45 * @param status status code (HTTP) 45 * @param status status code (HTTP)
46 */ 46 */
47typedef void (*GNUNET_REST_ResultProcessor) (void *cls, 47typedef void (*GNUNET_REST_ResultProcessor) (void *cls,
48 const char *data, 48 struct MHD_Response *resp,
49 size_t data_len,
50 int status); 49 int status);
51 50
52struct RestConnectionDataHandle 51struct RestConnectionDataHandle
diff --git a/src/rest/gnunet-rest-server.c b/src/rest/gnunet-rest-server.c
index 1f909c58c..1a6ab73b0 100644
--- a/src/rest/gnunet-rest-server.c
+++ b/src/rest/gnunet-rest-server.c
@@ -168,15 +168,10 @@ run_mhd_now ()
168 */ 168 */
169void 169void
170plugin_callback (void *cls, 170plugin_callback (void *cls,
171 const char *data, 171 struct MHD_Response *resp,
172 size_t len,
173 int status) 172 int status)
174{ 173{
175 struct MhdConnectionHandle *handle = cls; 174 struct MhdConnectionHandle *handle = cls;
176 struct MHD_Response *resp = MHD_create_response_from_buffer (len,
177 (void*)data,
178 MHD_RESPMEM_MUST_COPY);
179 (void) MHD_add_response_header (resp,MHD_HTTP_HEADER_CONTENT_TYPE,"application/json");
180 handle->status = status; 175 handle->status = status;
181 handle->response = resp; 176 handle->response = resp;
182 run_mhd_now(); 177 run_mhd_now();