diff options
Diffstat (limited to 'src/gns/plugin_rest_gns.c')
-rw-r--r-- | src/gns/plugin_rest_gns.c | 65 |
1 files changed, 24 insertions, 41 deletions
diff --git a/src/gns/plugin_rest_gns.c b/src/gns/plugin_rest_gns.c index 37313c529..a3006ce23 100644 --- a/src/gns/plugin_rest_gns.c +++ b/src/gns/plugin_rest_gns.c | |||
@@ -63,6 +63,11 @@ const struct GNUNET_CONFIGURATION_Handle *cfg; | |||
63 | static char *allow_methods; | 63 | static char *allow_methods; |
64 | 64 | ||
65 | /** | 65 | /** |
66 | * Connection to GNS | ||
67 | */ | ||
68 | static struct GNUNET_GNS_Handle *gns; | ||
69 | |||
70 | /** | ||
66 | * @brief struct returned by the initialization function of the plugin | 71 | * @brief struct returned by the initialization function of the plugin |
67 | */ | 72 | */ |
68 | struct Plugin | 73 | struct Plugin |
@@ -76,11 +81,6 @@ struct Plugin | |||
76 | struct RequestHandle | 81 | struct RequestHandle |
77 | { | 82 | { |
78 | /** | 83 | /** |
79 | * Connection to GNS | ||
80 | */ | ||
81 | struct GNUNET_GNS_Handle *gns; | ||
82 | |||
83 | /** | ||
84 | * Active GNS lookup | 84 | * Active GNS lookup |
85 | */ | 85 | */ |
86 | struct GNUNET_GNS_LookupWithTldRequest *gns_lookup; | 86 | struct GNUNET_GNS_LookupWithTldRequest *gns_lookup; |
@@ -153,12 +153,6 @@ cleanup_handle (void *cls) | |||
153 | GNUNET_GNS_lookup_with_tld_cancel (handle->gns_lookup); | 153 | GNUNET_GNS_lookup_with_tld_cancel (handle->gns_lookup); |
154 | handle->gns_lookup = NULL; | 154 | handle->gns_lookup = NULL; |
155 | } | 155 | } |
156 | if (NULL != handle->gns) | ||
157 | { | ||
158 | GNUNET_GNS_disconnect (handle->gns); | ||
159 | handle->gns = NULL; | ||
160 | } | ||
161 | |||
162 | if (NULL != handle->timeout_task) | 156 | if (NULL != handle->timeout_task) |
163 | { | 157 | { |
164 | GNUNET_SCHEDULER_cancel (handle->timeout_task); | 158 | GNUNET_SCHEDULER_cancel (handle->timeout_task); |
@@ -318,7 +312,7 @@ get_gns_cont (struct GNUNET_REST_RequestHandle *con_handle, | |||
318 | handle->record_type = GNUNET_GNSRECORD_TYPE_ANY; | 312 | handle->record_type = GNUNET_GNSRECORD_TYPE_ANY; |
319 | } | 313 | } |
320 | 314 | ||
321 | handle->gns_lookup = GNUNET_GNS_lookup_with_tld (handle->gns, | 315 | handle->gns_lookup = GNUNET_GNS_lookup_with_tld (gns, |
322 | handle->name, | 316 | handle->name, |
323 | handle->record_type, | 317 | handle->record_type, |
324 | GNUNET_GNS_LO_DEFAULT, | 318 | GNUNET_GNS_LO_DEFAULT, |
@@ -352,29 +346,6 @@ options_cont (struct GNUNET_REST_RequestHandle *con_handle, | |||
352 | 346 | ||
353 | 347 | ||
354 | /** | 348 | /** |
355 | * Handle rest request | ||
356 | * | ||
357 | * @param handle the request handle | ||
358 | */ | ||
359 | static void | ||
360 | init_cont (struct RequestHandle *handle) | ||
361 | { | ||
362 | struct GNUNET_REST_RequestHandlerError err; | ||
363 | static const struct GNUNET_REST_RequestHandler handlers[] = | ||
364 | { { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_GNS, &get_gns_cont }, | ||
365 | { MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_GNS, &options_cont }, | ||
366 | GNUNET_REST_HANDLER_END }; | ||
367 | |||
368 | if (GNUNET_NO == | ||
369 | GNUNET_REST_handle_request (handle->rest_handle, handlers, &err, handle)) | ||
370 | { | ||
371 | handle->response_code = err.error_code; | ||
372 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
373 | } | ||
374 | } | ||
375 | |||
376 | |||
377 | /** | ||
378 | * Function processing the REST call | 349 | * Function processing the REST call |
379 | * | 350 | * |
380 | * @param method HTTP method | 351 | * @param method HTTP method |
@@ -385,12 +356,17 @@ init_cont (struct RequestHandle *handle) | |||
385 | * @param proc_cls closure for callback function | 356 | * @param proc_cls closure for callback function |
386 | * @return GNUNET_OK if request accepted | 357 | * @return GNUNET_OK if request accepted |
387 | */ | 358 | */ |
388 | static void | 359 | static enum GNUNET_GenericReturnValue |
389 | rest_process_request (struct GNUNET_REST_RequestHandle *rest_handle, | 360 | rest_process_request (struct GNUNET_REST_RequestHandle *rest_handle, |
390 | GNUNET_REST_ResultProcessor proc, | 361 | GNUNET_REST_ResultProcessor proc, |
391 | void *proc_cls) | 362 | void *proc_cls) |
392 | { | 363 | { |
393 | struct RequestHandle *handle = GNUNET_new (struct RequestHandle); | 364 | struct RequestHandle *handle = GNUNET_new (struct RequestHandle); |
365 | struct GNUNET_REST_RequestHandlerError err; | ||
366 | static const struct GNUNET_REST_RequestHandler handlers[] = | ||
367 | { { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_GNS, &get_gns_cont }, | ||
368 | { MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_GNS, &options_cont }, | ||
369 | GNUNET_REST_HANDLER_END }; | ||
394 | 370 | ||
395 | handle->response_code = 0; | 371 | handle->response_code = 0; |
396 | handle->timeout = | 372 | handle->timeout = |
@@ -402,14 +378,17 @@ rest_process_request (struct GNUNET_REST_RequestHandle *rest_handle, | |||
402 | handle->url = GNUNET_strdup (rest_handle->url); | 378 | handle->url = GNUNET_strdup (rest_handle->url); |
403 | if (handle->url[strlen (handle->url) - 1] == '/') | 379 | if (handle->url[strlen (handle->url) - 1] == '/') |
404 | handle->url[strlen (handle->url) - 1] = '\0'; | 380 | handle->url[strlen (handle->url) - 1] = '\0'; |
405 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting...\n"); | 381 | if (GNUNET_NO == |
406 | handle->gns = GNUNET_GNS_connect (cfg); | 382 | GNUNET_REST_handle_request (handle->rest_handle, handlers, &err, handle)) |
407 | init_cont (handle); | 383 | { |
384 | cleanup_handle (handle); | ||
385 | return GNUNET_NO; | ||
386 | } | ||
387 | |||
408 | 388 | ||
409 | handle->timeout_task = | 389 | handle->timeout_task = |
410 | GNUNET_SCHEDULER_add_delayed (handle->timeout, &do_timeout, handle); | 390 | GNUNET_SCHEDULER_add_delayed (handle->timeout, &do_timeout, handle); |
411 | 391 | return GNUNET_YES; | |
412 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected\n"); | ||
413 | } | 392 | } |
414 | 393 | ||
415 | 394 | ||
@@ -441,6 +420,7 @@ libgnunet_plugin_rest_gns_init (void *cls) | |||
441 | MHD_HTTP_METHOD_PUT, | 420 | MHD_HTTP_METHOD_PUT, |
442 | MHD_HTTP_METHOD_DELETE, | 421 | MHD_HTTP_METHOD_DELETE, |
443 | MHD_HTTP_METHOD_OPTIONS); | 422 | MHD_HTTP_METHOD_OPTIONS); |
423 | gns = GNUNET_GNS_connect (cfg); | ||
444 | 424 | ||
445 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _ ("Gns REST API initialized\n")); | 425 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _ ("Gns REST API initialized\n")); |
446 | return api; | 426 | return api; |
@@ -460,6 +440,9 @@ libgnunet_plugin_rest_gns_done (void *cls) | |||
460 | struct Plugin *plugin = api->cls; | 440 | struct Plugin *plugin = api->cls; |
461 | 441 | ||
462 | plugin->cfg = NULL; | 442 | plugin->cfg = NULL; |
443 | if (NULL != gns) | ||
444 | GNUNET_GNS_disconnect (gns); | ||
445 | |||
463 | 446 | ||
464 | GNUNET_free (allow_methods); | 447 | GNUNET_free (allow_methods); |
465 | GNUNET_free (api); | 448 | GNUNET_free (api); |