aboutsummaryrefslogtreecommitdiff
path: root/src/gns/plugin_rest_gns.c
diff options
context:
space:
mode:
authorSchanzenbach, Martin <mschanzenbach@posteo.de>2019-07-17 18:14:37 +0200
committerSchanzenbach, Martin <mschanzenbach@posteo.de>2019-07-17 18:14:37 +0200
commitc3b9d89432edc264a18d6d98665110dff4aeb4f1 (patch)
tree3efd3aebed5f41577fc0057b27e52c713691ee68 /src/gns/plugin_rest_gns.c
parent0c6cb5a5e88c67440382a7a5531601c03f1e6e18 (diff)
downloadgnunet-c3b9d89432edc264a18d6d98665110dff4aeb4f1.tar.gz
gnunet-c3b9d89432edc264a18d6d98665110dff4aeb4f1.zip
revert timeout removal
Diffstat (limited to 'src/gns/plugin_rest_gns.c')
-rw-r--r--src/gns/plugin_rest_gns.c121
1 files changed, 72 insertions, 49 deletions
diff --git a/src/gns/plugin_rest_gns.c b/src/gns/plugin_rest_gns.c
index 46babdc76..1ecde12f0 100644
--- a/src/gns/plugin_rest_gns.c
+++ b/src/gns/plugin_rest_gns.c
@@ -60,7 +60,7 @@ const struct GNUNET_CONFIGURATION_Handle *cfg;
60/** 60/**
61 * HTTP methods allows for this plugin 61 * HTTP methods allows for this plugin
62 */ 62 */
63static char* allow_methods; 63static char *allow_methods;
64 64
65/** 65/**
66 * @brief struct returned by the initialization function of the plugin 66 * @brief struct returned by the initialization function of the plugin
@@ -102,6 +102,16 @@ struct RequestHandle
102 struct GNUNET_REST_RequestHandle *rest_handle; 102 struct GNUNET_REST_RequestHandle *rest_handle;
103 103
104 /** 104 /**
105 * Desired timeout for the lookup (default is no timeout).
106 */
107 struct GNUNET_TIME_Relative timeout;
108
109 /**
110 * ID of a task associated with the resolution process.
111 */
112 struct GNUNET_SCHEDULER_Task *timeout_task;
113
114 /**
105 * The plugin result processor 115 * The plugin result processor
106 */ 116 */
107 GNUNET_REST_ResultProcessor proc; 117 GNUNET_REST_ResultProcessor proc;
@@ -125,7 +135,6 @@ struct RequestHandle
125 * Response code 135 * Response code
126 */ 136 */
127 int response_code; 137 int response_code;
128
129}; 138};
130 139
131 140
@@ -137,8 +146,7 @@ static void
137cleanup_handle (void *cls) 146cleanup_handle (void *cls)
138{ 147{
139 struct RequestHandle *handle = cls; 148 struct RequestHandle *handle = cls;
140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
141 "Cleaning up\n");
142 150
143 if (NULL != handle->gns_lookup) 151 if (NULL != handle->gns_lookup)
144 { 152 {
@@ -151,6 +159,11 @@ cleanup_handle (void *cls)
151 handle->gns = NULL; 159 handle->gns = NULL;
152 } 160 }
153 161
162 if (NULL != handle->timeout_task)
163 {
164 GNUNET_SCHEDULER_cancel (handle->timeout_task);
165 handle->timeout_task = NULL;
166 }
154 if (NULL != handle->url) 167 if (NULL != handle->url)
155 GNUNET_free (handle->url); 168 GNUNET_free (handle->url);
156 if (NULL != handle->name) 169 if (NULL != handle->name)
@@ -172,25 +185,38 @@ do_error (void *cls)
172{ 185{
173 struct RequestHandle *handle = cls; 186 struct RequestHandle *handle = cls;
174 struct MHD_Response *resp; 187 struct MHD_Response *resp;
175 json_t *json_error = json_object(); 188 json_t *json_error = json_object ();
176 char *response; 189 char *response;
177 190
191 if (NULL != handle->timeout_task)
192 GNUNET_SCHEDULER_cancel (handle->timeout_task);
193 handle->timeout_task = NULL;
178 if (NULL == handle->emsg) 194 if (NULL == handle->emsg)
179 handle->emsg = GNUNET_strdup(GNUNET_REST_GNS_ERROR_UNKNOWN); 195 handle->emsg = GNUNET_strdup (GNUNET_REST_GNS_ERROR_UNKNOWN);
180 196
181 json_object_set_new(json_error,"error", json_string(handle->emsg)); 197 json_object_set_new (json_error, "error", json_string (handle->emsg));
182 198
183 if (0 == handle->response_code) 199 if (0 == handle->response_code)
184 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; 200 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
185 response = json_dumps (json_error, 0); 201 response = json_dumps (json_error, 0);
186 resp = GNUNET_REST_create_response (response); 202 resp = GNUNET_REST_create_response (response);
187 handle->proc (handle->proc_cls, resp, handle->response_code); 203 handle->proc (handle->proc_cls, resp, handle->response_code);
188 json_decref(json_error); 204 json_decref (json_error);
189 GNUNET_free(response); 205 GNUNET_free (response);
190 GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); 206 GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
191} 207}
192 208
193 209
210static void
211do_timeout (void *cls)
212{
213 struct RequestHandle *handle = cls;
214 handle->timeout_task = NULL;
215 handle->response_code = MHD_HTTP_REQUEST_TIMEOUT;
216 do_error (handle);
217}
218
219
194/** 220/**
195 * Iterator called on obtained result for a GNS lookup. 221 * Iterator called on obtained result for a GNS lookup.
196 * 222 *
@@ -215,20 +241,20 @@ handle_gns_response (void *cls,
215 if (GNUNET_NO == was_gns) 241 if (GNUNET_NO == was_gns)
216 { 242 {
217 handle->response_code = MHD_HTTP_NOT_FOUND; 243 handle->response_code = MHD_HTTP_NOT_FOUND;
218 handle->emsg = GNUNET_strdup(GNUNET_REST_GNS_NOT_FOUND); 244 handle->emsg = GNUNET_strdup (GNUNET_REST_GNS_NOT_FOUND);
219 GNUNET_SCHEDULER_add_now (&do_error, handle); 245 GNUNET_SCHEDULER_add_now (&do_error, handle);
220 return; 246 return;
221 } 247 }
222 248
223 result_obj = GNUNET_JSON_from_gnsrecord (handle->name, rd, rd_count); 249 result_obj = GNUNET_JSON_from_gnsrecord (handle->name, rd, rd_count);
224 250
225 result = json_dumps(result_obj, 0); 251 result = json_dumps (result_obj, 0);
226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result); 252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result);
227 resp = GNUNET_REST_create_response (result); 253 resp = GNUNET_REST_create_response (result);
228 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); 254 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
229 GNUNET_free (result); 255 GNUNET_free (result);
230 json_decref (result_obj); 256 json_decref (result_obj);
231 GNUNET_SCHEDULER_add_now(&cleanup_handle, handle); 257 GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
232} 258}
233 259
234 260
@@ -241,7 +267,7 @@ handle_gns_response (void *cls,
241 */ 267 */
242void 268void
243get_gns_cont (struct GNUNET_REST_RequestHandle *con_handle, 269get_gns_cont (struct GNUNET_REST_RequestHandle *con_handle,
244 const char* url, 270 const char *url,
245 void *cls) 271 void *cls)
246{ 272{
247 struct RequestHandle *handle = cls; 273 struct RequestHandle *handle = cls;
@@ -259,32 +285,32 @@ get_gns_cont (struct GNUNET_REST_RequestHandle *con_handle,
259 if (NULL == name) 285 if (NULL == name)
260 { 286 {
261 handle->response_code = MHD_HTTP_NOT_FOUND; 287 handle->response_code = MHD_HTTP_NOT_FOUND;
262 handle->emsg = GNUNET_strdup(GNUNET_REST_GNS_NOT_FOUND); 288 handle->emsg = GNUNET_strdup (GNUNET_REST_GNS_NOT_FOUND);
263 GNUNET_SCHEDULER_add_now (&do_error, handle); 289 GNUNET_SCHEDULER_add_now (&do_error, handle);
264 return; 290 return;
265 } 291 }
266 if (0 >= strlen (name)) 292 if (0 >= strlen (name))
267 { 293 {
268 handle->response_code = MHD_HTTP_NOT_FOUND; 294 handle->response_code = MHD_HTTP_NOT_FOUND;
269 handle->emsg = GNUNET_strdup(GNUNET_REST_GNS_NOT_FOUND); 295 handle->emsg = GNUNET_strdup (GNUNET_REST_GNS_NOT_FOUND);
270 GNUNET_SCHEDULER_add_now (&do_error, handle); 296 GNUNET_SCHEDULER_add_now (&do_error, handle);
271 return; 297 return;
272 } 298 }
273 handle->name = GNUNET_strdup(name); 299 handle->name = GNUNET_strdup (name);
274 300
275 handle->record_type = UINT32_MAX; 301 handle->record_type = UINT32_MAX;
276 GNUNET_CRYPTO_hash (GNUNET_REST_GNS_PARAM_RECORD_TYPE, 302 GNUNET_CRYPTO_hash (GNUNET_REST_GNS_PARAM_RECORD_TYPE,
277 strlen (GNUNET_REST_GNS_PARAM_RECORD_TYPE), 303 strlen (GNUNET_REST_GNS_PARAM_RECORD_TYPE),
278 &key); 304 &key);
279 if ( GNUNET_YES 305 if (GNUNET_YES ==
280 == GNUNET_CONTAINER_multihashmap_contains (con_handle->url_param_map, 306 GNUNET_CONTAINER_multihashmap_contains (con_handle->url_param_map, &key))
281 &key))
282 { 307 {
283 record_type = GNUNET_CONTAINER_multihashmap_get (con_handle->url_param_map, &key); 308 record_type =
284 handle->record_type = GNUNET_GNSRECORD_typename_to_number(record_type); 309 GNUNET_CONTAINER_multihashmap_get (con_handle->url_param_map, &key);
310 handle->record_type = GNUNET_GNSRECORD_typename_to_number (record_type);
285 } 311 }
286 312
287 if(UINT32_MAX == handle->record_type) 313 if (UINT32_MAX == handle->record_type)
288 { 314 {
289 handle->record_type = GNUNET_GNSRECORD_TYPE_ANY; 315 handle->record_type = GNUNET_GNSRECORD_TYPE_ANY;
290 } 316 }
@@ -298,7 +324,6 @@ get_gns_cont (struct GNUNET_REST_RequestHandle *con_handle,
298} 324}
299 325
300 326
301
302/** 327/**
303 * Respond to OPTIONS request 328 * Respond to OPTIONS request
304 * 329 *
@@ -308,7 +333,7 @@ get_gns_cont (struct GNUNET_REST_RequestHandle *con_handle,
308 */ 333 */
309static void 334static void
310options_cont (struct GNUNET_REST_RequestHandle *con_handle, 335options_cont (struct GNUNET_REST_RequestHandle *con_handle,
311 const char* url, 336 const char *url,
312 void *cls) 337 void *cls)
313{ 338{
314 struct MHD_Response *resp; 339 struct MHD_Response *resp;
@@ -316,11 +341,9 @@ options_cont (struct GNUNET_REST_RequestHandle *con_handle,
316 341
317 //independent of path return all options 342 //independent of path return all options
318 resp = GNUNET_REST_create_response (NULL); 343 resp = GNUNET_REST_create_response (NULL);
319 MHD_add_response_header (resp, 344 MHD_add_response_header (resp, "Access-Control-Allow-Methods", allow_methods);
320 "Access-Control-Allow-Methods",
321 allow_methods);
322 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); 345 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
323 GNUNET_SCHEDULER_add_now(&cleanup_handle, handle); 346 GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
324 return; 347 return;
325} 348}
326 349
@@ -334,16 +357,13 @@ static void
334init_cont (struct RequestHandle *handle) 357init_cont (struct RequestHandle *handle)
335{ 358{
336 struct GNUNET_REST_RequestHandlerError err; 359 struct GNUNET_REST_RequestHandlerError err;
337 static const struct GNUNET_REST_RequestHandler handlers[] = { 360 static const struct GNUNET_REST_RequestHandler handlers[] =
338 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_GNS, &get_gns_cont}, 361 {{MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_GNS, &get_gns_cont},
339 {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_GNS, &options_cont}, 362 {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_GNS, &options_cont},
340 GNUNET_REST_HANDLER_END 363 GNUNET_REST_HANDLER_END};
341 }; 364
342 365 if (GNUNET_NO ==
343 if (GNUNET_NO == GNUNET_REST_handle_request (handle->rest_handle, 366 GNUNET_REST_handle_request (handle->rest_handle, handlers, &err, handle))
344 handlers,
345 &err,
346 handle))
347 { 367 {
348 handle->response_code = err.error_code; 368 handle->response_code = err.error_code;
349 GNUNET_SCHEDULER_add_now (&do_error, handle); 369 GNUNET_SCHEDULER_add_now (&do_error, handle);
@@ -363,23 +383,28 @@ init_cont (struct RequestHandle *handle)
363 * @return GNUNET_OK if request accepted 383 * @return GNUNET_OK if request accepted
364 */ 384 */
365static void 385static void
366rest_process_request(struct GNUNET_REST_RequestHandle *rest_handle, 386rest_process_request (struct GNUNET_REST_RequestHandle *rest_handle,
367 GNUNET_REST_ResultProcessor proc, 387 GNUNET_REST_ResultProcessor proc,
368 void *proc_cls) 388 void *proc_cls)
369{ 389{
370 struct RequestHandle *handle = GNUNET_new (struct RequestHandle); 390 struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
371 391
372 handle->response_code = 0; 392 handle->response_code = 0;
393 handle->timeout =
394 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60);
373 handle->proc_cls = proc_cls; 395 handle->proc_cls = proc_cls;
374 handle->proc = proc; 396 handle->proc = proc;
375 handle->rest_handle = rest_handle; 397 handle->rest_handle = rest_handle;
376 398
377 handle->url = GNUNET_strdup (rest_handle->url); 399 handle->url = GNUNET_strdup (rest_handle->url);
378 if (handle->url[strlen (handle->url)-1] == '/') 400 if (handle->url[strlen (handle->url) - 1] == '/')
379 handle->url[strlen (handle->url)-1] = '\0'; 401 handle->url[strlen (handle->url) - 1] = '\0';
380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting...\n"); 402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting...\n");
381 handle->gns = GNUNET_GNS_connect (cfg); 403 handle->gns = GNUNET_GNS_connect (cfg);
382 init_cont(handle); 404 init_cont (handle);
405
406 handle->timeout_task =
407 GNUNET_SCHEDULER_add_delayed (handle->timeout, &do_timeout, handle);
383 408
384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected\n"); 409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected\n");
385} 410}
@@ -399,7 +424,7 @@ libgnunet_plugin_rest_gns_init (void *cls)
399 424
400 cfg = cls; 425 cfg = cls;
401 if (NULL != plugin.cfg) 426 if (NULL != plugin.cfg)
402 return NULL; /* can only initialize once! */ 427 return NULL; /* can only initialize once! */
403 memset (&plugin, 0, sizeof (struct Plugin)); 428 memset (&plugin, 0, sizeof (struct Plugin));
404 plugin.cfg = cfg; 429 plugin.cfg = cfg;
405 api = GNUNET_new (struct GNUNET_REST_Plugin); 430 api = GNUNET_new (struct GNUNET_REST_Plugin);
@@ -414,8 +439,7 @@ libgnunet_plugin_rest_gns_init (void *cls)
414 MHD_HTTP_METHOD_DELETE, 439 MHD_HTTP_METHOD_DELETE,
415 MHD_HTTP_METHOD_OPTIONS); 440 MHD_HTTP_METHOD_OPTIONS);
416 441
417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _ ("Gns REST API initialized\n"));
418 _("Gns REST API initialized\n"));
419 return api; 443 return api;
420} 444}
421 445
@@ -435,8 +459,7 @@ libgnunet_plugin_rest_gns_done (void *cls)
435 459
436 GNUNET_free_non_null (allow_methods); 460 GNUNET_free_non_null (allow_methods);
437 GNUNET_free (api); 461 GNUNET_free (api);
438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Gns REST plugin is finished\n");
439 "Gns REST plugin is finished\n");
440 return NULL; 463 return NULL;
441} 464}
442 465