aboutsummaryrefslogtreecommitdiff
path: root/src/gns/plugin_rest_gns.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
committerChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
commitc4e9ba925ffd758aaa3feee2ccfc0b76f26fe207 (patch)
treecac3ce030d77b4cbe7c7dc62ed58cfe6d24f73e1 /src/gns/plugin_rest_gns.c
parentfbb71d527c7d6babf269a8fefce1db291b9f7068 (diff)
downloadgnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.tar.gz
gnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.zip
global reindent, now with uncrustify hook enabled
Diffstat (limited to 'src/gns/plugin_rest_gns.c')
-rw-r--r--src/gns/plugin_rest_gns.c275
1 files changed, 138 insertions, 137 deletions
diff --git a/src/gns/plugin_rest_gns.c b/src/gns/plugin_rest_gns.c
index 2fc103ef6..cdcdd6cf8 100644
--- a/src/gns/plugin_rest_gns.c
+++ b/src/gns/plugin_rest_gns.c
@@ -65,14 +65,16 @@ static char *allow_methods;
65/** 65/**
66 * @brief struct returned by the initialization function of the plugin 66 * @brief struct returned by the initialization function of the plugin
67 */ 67 */
68struct Plugin { 68struct Plugin
69{
69 const struct GNUNET_CONFIGURATION_Handle *cfg; 70 const struct GNUNET_CONFIGURATION_Handle *cfg;
70}; 71};
71 72
72/** 73/**
73 * The request handle 74 * The request handle
74 */ 75 */
75struct RequestHandle { 76struct RequestHandle
77{
76 /** 78 /**
77 * Connection to GNS 79 * Connection to GNS
78 */ 80 */
@@ -140,36 +142,36 @@ struct RequestHandle {
140 * @param handle Handle to clean up 142 * @param handle Handle to clean up
141 */ 143 */
142static void 144static void
143cleanup_handle(void *cls) 145cleanup_handle (void *cls)
144{ 146{
145 struct RequestHandle *handle = cls; 147 struct RequestHandle *handle = cls;
146 148
147 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n"); 149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
148 150
149 if (NULL != handle->gns_lookup) 151 if (NULL != handle->gns_lookup)
150 { 152 {
151 GNUNET_GNS_lookup_with_tld_cancel(handle->gns_lookup); 153 GNUNET_GNS_lookup_with_tld_cancel (handle->gns_lookup);
152 handle->gns_lookup = NULL; 154 handle->gns_lookup = NULL;
153 } 155 }
154 if (NULL != handle->gns) 156 if (NULL != handle->gns)
155 { 157 {
156 GNUNET_GNS_disconnect(handle->gns); 158 GNUNET_GNS_disconnect (handle->gns);
157 handle->gns = NULL; 159 handle->gns = NULL;
158 } 160 }
159 161
160 if (NULL != handle->timeout_task) 162 if (NULL != handle->timeout_task)
161 { 163 {
162 GNUNET_SCHEDULER_cancel(handle->timeout_task); 164 GNUNET_SCHEDULER_cancel (handle->timeout_task);
163 handle->timeout_task = NULL; 165 handle->timeout_task = NULL;
164 } 166 }
165 if (NULL != handle->url) 167 if (NULL != handle->url)
166 GNUNET_free(handle->url); 168 GNUNET_free (handle->url);
167 if (NULL != handle->name) 169 if (NULL != handle->name)
168 GNUNET_free(handle->name); 170 GNUNET_free (handle->name);
169 if (NULL != handle->emsg) 171 if (NULL != handle->emsg)
170 GNUNET_free(handle->emsg); 172 GNUNET_free (handle->emsg);
171 173
172 GNUNET_free(handle); 174 GNUNET_free (handle);
173} 175}
174 176
175 177
@@ -179,40 +181,40 @@ cleanup_handle(void *cls)
179 * @param cls the `struct RequestHandle` 181 * @param cls the `struct RequestHandle`
180 */ 182 */
181static void 183static void
182do_error(void *cls) 184do_error (void *cls)
183{ 185{
184 struct RequestHandle *handle = cls; 186 struct RequestHandle *handle = cls;
185 struct MHD_Response *resp; 187 struct MHD_Response *resp;
186 json_t *json_error = json_object(); 188 json_t *json_error = json_object ();
187 char *response; 189 char *response;
188 190
189 if (NULL != handle->timeout_task) 191 if (NULL != handle->timeout_task)
190 GNUNET_SCHEDULER_cancel(handle->timeout_task); 192 GNUNET_SCHEDULER_cancel (handle->timeout_task);
191 handle->timeout_task = NULL; 193 handle->timeout_task = NULL;
192 if (NULL == handle->emsg) 194 if (NULL == handle->emsg)
193 handle->emsg = GNUNET_strdup(GNUNET_REST_GNS_ERROR_UNKNOWN); 195 handle->emsg = GNUNET_strdup (GNUNET_REST_GNS_ERROR_UNKNOWN);
194 196
195 json_object_set_new(json_error, "error", json_string(handle->emsg)); 197 json_object_set_new (json_error, "error", json_string (handle->emsg));
196 198
197 if (0 == handle->response_code) 199 if (0 == handle->response_code)
198 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; 200 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
199 response = json_dumps(json_error, 0); 201 response = json_dumps (json_error, 0);
200 resp = GNUNET_REST_create_response(response); 202 resp = GNUNET_REST_create_response (response);
201 handle->proc(handle->proc_cls, resp, handle->response_code); 203 handle->proc (handle->proc_cls, resp, handle->response_code);
202 json_decref(json_error); 204 json_decref (json_error);
203 GNUNET_free(response); 205 GNUNET_free (response);
204 GNUNET_SCHEDULER_add_now(&cleanup_handle, handle); 206 GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
205} 207}
206 208
207 209
208static void 210static void
209do_timeout(void *cls) 211do_timeout (void *cls)
210{ 212{
211 struct RequestHandle *handle = cls; 213 struct RequestHandle *handle = cls;
212 214
213 handle->timeout_task = NULL; 215 handle->timeout_task = NULL;
214 handle->response_code = MHD_HTTP_REQUEST_TIMEOUT; 216 handle->response_code = MHD_HTTP_REQUEST_TIMEOUT;
215 do_error(handle); 217 do_error (handle);
216} 218}
217 219
218 220
@@ -225,10 +227,10 @@ do_timeout(void *cls)
225 * @param rd the records in reply 227 * @param rd the records in reply
226 */ 228 */
227static void 229static void
228handle_gns_response(void *cls, 230handle_gns_response (void *cls,
229 int was_gns, 231 int was_gns,
230 uint32_t rd_count, 232 uint32_t rd_count,
231 const struct GNUNET_GNSRECORD_Data *rd) 233 const struct GNUNET_GNSRECORD_Data *rd)
232{ 234{
233 struct RequestHandle *handle = cls; 235 struct RequestHandle *handle = cls;
234 struct MHD_Response *resp; 236 struct MHD_Response *resp;
@@ -238,22 +240,22 @@ handle_gns_response(void *cls,
238 handle->gns_lookup = NULL; 240 handle->gns_lookup = NULL;
239 241
240 if (GNUNET_NO == was_gns) 242 if (GNUNET_NO == was_gns)
241 { 243 {
242 handle->response_code = MHD_HTTP_NOT_FOUND; 244 handle->response_code = MHD_HTTP_NOT_FOUND;
243 handle->emsg = GNUNET_strdup(GNUNET_REST_GNS_NOT_FOUND); 245 handle->emsg = GNUNET_strdup (GNUNET_REST_GNS_NOT_FOUND);
244 GNUNET_SCHEDULER_add_now(&do_error, handle); 246 GNUNET_SCHEDULER_add_now (&do_error, handle);
245 return; 247 return;
246 } 248 }
247 249
248 result_obj = GNUNET_JSON_from_gnsrecord(handle->name, rd, rd_count); 250 result_obj = GNUNET_JSON_from_gnsrecord (handle->name, rd, rd_count);
249 251
250 result = json_dumps(result_obj, 0); 252 result = json_dumps (result_obj, 0);
251 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result); 253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result);
252 resp = GNUNET_REST_create_response(result); 254 resp = GNUNET_REST_create_response (result);
253 handle->proc(handle->proc_cls, resp, MHD_HTTP_OK); 255 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
254 GNUNET_free(result); 256 GNUNET_free (result);
255 json_decref(result_obj); 257 json_decref (result_obj);
256 GNUNET_SCHEDULER_add_now(&cleanup_handle, handle); 258 GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
257} 259}
258 260
259 261
@@ -265,9 +267,9 @@ handle_gns_response(void *cls,
265 * @param cls the RequestHandle 267 * @param cls the RequestHandle
266 */ 268 */
267void 269void
268get_gns_cont(struct GNUNET_REST_RequestHandle *con_handle, 270get_gns_cont (struct GNUNET_REST_RequestHandle *con_handle,
269 const char *url, 271 const char *url,
270 void *cls) 272 void *cls)
271{ 273{
272 struct RequestHandle *handle = cls; 274 struct RequestHandle *handle = cls;
273 struct GNUNET_HashCode key; 275 struct GNUNET_HashCode key;
@@ -276,50 +278,50 @@ get_gns_cont(struct GNUNET_REST_RequestHandle *con_handle,
276 278
277 name = NULL; 279 name = NULL;
278 handle->name = NULL; 280 handle->name = NULL;
279 if (strlen(GNUNET_REST_API_NS_GNS) < strlen(handle->url)) 281 if (strlen (GNUNET_REST_API_NS_GNS) < strlen (handle->url))
280 { 282 {
281 name = &handle->url[strlen(GNUNET_REST_API_NS_GNS) + 1]; 283 name = &handle->url[strlen (GNUNET_REST_API_NS_GNS) + 1];
282 } 284 }
283 285
284 if (NULL == name) 286 if (NULL == name)
285 { 287 {
286 handle->response_code = MHD_HTTP_NOT_FOUND; 288 handle->response_code = MHD_HTTP_NOT_FOUND;
287 handle->emsg = GNUNET_strdup(GNUNET_REST_GNS_NOT_FOUND); 289 handle->emsg = GNUNET_strdup (GNUNET_REST_GNS_NOT_FOUND);
288 GNUNET_SCHEDULER_add_now(&do_error, handle); 290 GNUNET_SCHEDULER_add_now (&do_error, handle);
289 return; 291 return;
290 } 292 }
291 if (0 >= strlen(name)) 293 if (0 >= strlen (name))
292 { 294 {
293 handle->response_code = MHD_HTTP_NOT_FOUND; 295 handle->response_code = MHD_HTTP_NOT_FOUND;
294 handle->emsg = GNUNET_strdup(GNUNET_REST_GNS_NOT_FOUND); 296 handle->emsg = GNUNET_strdup (GNUNET_REST_GNS_NOT_FOUND);
295 GNUNET_SCHEDULER_add_now(&do_error, handle); 297 GNUNET_SCHEDULER_add_now (&do_error, handle);
296 return; 298 return;
297 } 299 }
298 handle->name = GNUNET_strdup(name); 300 handle->name = GNUNET_strdup (name);
299 301
300 handle->record_type = UINT32_MAX; 302 handle->record_type = UINT32_MAX;
301 GNUNET_CRYPTO_hash(GNUNET_REST_GNS_PARAM_RECORD_TYPE, 303 GNUNET_CRYPTO_hash (GNUNET_REST_GNS_PARAM_RECORD_TYPE,
302 strlen(GNUNET_REST_GNS_PARAM_RECORD_TYPE), 304 strlen (GNUNET_REST_GNS_PARAM_RECORD_TYPE),
303 &key); 305 &key);
304 if (GNUNET_YES == 306 if (GNUNET_YES ==
305 GNUNET_CONTAINER_multihashmap_contains(con_handle->url_param_map, &key)) 307 GNUNET_CONTAINER_multihashmap_contains (con_handle->url_param_map, &key))
306 { 308 {
307 record_type = 309 record_type =
308 GNUNET_CONTAINER_multihashmap_get(con_handle->url_param_map, &key); 310 GNUNET_CONTAINER_multihashmap_get (con_handle->url_param_map, &key);
309 handle->record_type = GNUNET_GNSRECORD_typename_to_number(record_type); 311 handle->record_type = GNUNET_GNSRECORD_typename_to_number (record_type);
310 } 312 }
311 313
312 if (UINT32_MAX == handle->record_type) 314 if (UINT32_MAX == handle->record_type)
313 { 315 {
314 handle->record_type = GNUNET_GNSRECORD_TYPE_ANY; 316 handle->record_type = GNUNET_GNSRECORD_TYPE_ANY;
315 } 317 }
316 318
317 handle->gns_lookup = GNUNET_GNS_lookup_with_tld(handle->gns, 319 handle->gns_lookup = GNUNET_GNS_lookup_with_tld (handle->gns,
318 handle->name, 320 handle->name,
319 handle->record_type, 321 handle->record_type,
320 GNUNET_NO, 322 GNUNET_NO,
321 &handle_gns_response, 323 &handle_gns_response,
322 handle); 324 handle);
323} 325}
324 326
325 327
@@ -331,18 +333,18 @@ get_gns_cont(struct GNUNET_REST_RequestHandle *con_handle,
331 * @param cls the RequestHandle 333 * @param cls the RequestHandle
332 */ 334 */
333static void 335static void
334options_cont(struct GNUNET_REST_RequestHandle *con_handle, 336options_cont (struct GNUNET_REST_RequestHandle *con_handle,
335 const char *url, 337 const char *url,
336 void *cls) 338 void *cls)
337{ 339{
338 struct MHD_Response *resp; 340 struct MHD_Response *resp;
339 struct RequestHandle *handle = cls; 341 struct RequestHandle *handle = cls;
340 342
341 //independent of path return all options 343 // independent of path return all options
342 resp = GNUNET_REST_create_response(NULL); 344 resp = GNUNET_REST_create_response (NULL);
343 MHD_add_response_header(resp, "Access-Control-Allow-Methods", allow_methods); 345 MHD_add_response_header (resp, "Access-Control-Allow-Methods", allow_methods);
344 handle->proc(handle->proc_cls, resp, MHD_HTTP_OK); 346 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
345 GNUNET_SCHEDULER_add_now(&cleanup_handle, handle); 347 GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
346 return; 348 return;
347} 349}
348 350
@@ -353,7 +355,7 @@ options_cont(struct GNUNET_REST_RequestHandle *con_handle,
353 * @param handle the request handle 355 * @param handle the request handle
354 */ 356 */
355static void 357static void
356init_cont(struct RequestHandle *handle) 358init_cont (struct RequestHandle *handle)
357{ 359{
358 struct GNUNET_REST_RequestHandlerError err; 360 struct GNUNET_REST_RequestHandlerError err;
359 static const struct GNUNET_REST_RequestHandler handlers[] = 361 static const struct GNUNET_REST_RequestHandler handlers[] =
@@ -362,11 +364,11 @@ init_cont(struct RequestHandle *handle)
362 GNUNET_REST_HANDLER_END }; 364 GNUNET_REST_HANDLER_END };
363 365
364 if (GNUNET_NO == 366 if (GNUNET_NO ==
365 GNUNET_REST_handle_request(handle->rest_handle, handlers, &err, handle)) 367 GNUNET_REST_handle_request (handle->rest_handle, handlers, &err, handle))
366 { 368 {
367 handle->response_code = err.error_code; 369 handle->response_code = err.error_code;
368 GNUNET_SCHEDULER_add_now(&do_error, handle); 370 GNUNET_SCHEDULER_add_now (&do_error, handle);
369 } 371 }
370} 372}
371 373
372 374
@@ -382,30 +384,30 @@ init_cont(struct RequestHandle *handle)
382 * @return GNUNET_OK if request accepted 384 * @return GNUNET_OK if request accepted
383 */ 385 */
384static void 386static void
385rest_process_request(struct GNUNET_REST_RequestHandle *rest_handle, 387rest_process_request (struct GNUNET_REST_RequestHandle *rest_handle,
386 GNUNET_REST_ResultProcessor proc, 388 GNUNET_REST_ResultProcessor proc,
387 void *proc_cls) 389 void *proc_cls)
388{ 390{
389 struct RequestHandle *handle = GNUNET_new(struct RequestHandle); 391 struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
390 392
391 handle->response_code = 0; 393 handle->response_code = 0;
392 handle->timeout = 394 handle->timeout =
393 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60); 395 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60);
394 handle->proc_cls = proc_cls; 396 handle->proc_cls = proc_cls;
395 handle->proc = proc; 397 handle->proc = proc;
396 handle->rest_handle = rest_handle; 398 handle->rest_handle = rest_handle;
397 399
398 handle->url = GNUNET_strdup(rest_handle->url); 400 handle->url = GNUNET_strdup (rest_handle->url);
399 if (handle->url[strlen(handle->url) - 1] == '/') 401 if (handle->url[strlen (handle->url) - 1] == '/')
400 handle->url[strlen(handle->url) - 1] = '\0'; 402 handle->url[strlen (handle->url) - 1] = '\0';
401 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connecting...\n"); 403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting...\n");
402 handle->gns = GNUNET_GNS_connect(cfg); 404 handle->gns = GNUNET_GNS_connect (cfg);
403 init_cont(handle); 405 init_cont (handle);
404 406
405 handle->timeout_task = 407 handle->timeout_task =
406 GNUNET_SCHEDULER_add_delayed(handle->timeout, &do_timeout, handle); 408 GNUNET_SCHEDULER_add_delayed (handle->timeout, &do_timeout, handle);
407 409
408 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connected\n"); 410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected\n");
409} 411}
410 412
411 413
@@ -416,7 +418,7 @@ rest_process_request(struct GNUNET_REST_RequestHandle *rest_handle,
416 * @return NULL on error, otherwise the plugin context 418 * @return NULL on error, otherwise the plugin context
417 */ 419 */
418void * 420void *
419libgnunet_plugin_rest_gns_init(void *cls) 421libgnunet_plugin_rest_gns_init (void *cls)
420{ 422{
421 static struct Plugin plugin; 423 static struct Plugin plugin;
422 struct GNUNET_REST_Plugin *api; 424 struct GNUNET_REST_Plugin *api;
@@ -424,21 +426,21 @@ libgnunet_plugin_rest_gns_init(void *cls)
424 cfg = cls; 426 cfg = cls;
425 if (NULL != plugin.cfg) 427 if (NULL != plugin.cfg)
426 return NULL; /* can only initialize once! */ 428 return NULL; /* can only initialize once! */
427 memset(&plugin, 0, sizeof(struct Plugin)); 429 memset (&plugin, 0, sizeof(struct Plugin));
428 plugin.cfg = cfg; 430 plugin.cfg = cfg;
429 api = GNUNET_new(struct GNUNET_REST_Plugin); 431 api = GNUNET_new (struct GNUNET_REST_Plugin);
430 api->cls = &plugin; 432 api->cls = &plugin;
431 api->name = GNUNET_REST_API_NS_GNS; 433 api->name = GNUNET_REST_API_NS_GNS;
432 api->process_request = &rest_process_request; 434 api->process_request = &rest_process_request;
433 GNUNET_asprintf(&allow_methods, 435 GNUNET_asprintf (&allow_methods,
434 "%s, %s, %s, %s, %s", 436 "%s, %s, %s, %s, %s",
435 MHD_HTTP_METHOD_GET, 437 MHD_HTTP_METHOD_GET,
436 MHD_HTTP_METHOD_POST, 438 MHD_HTTP_METHOD_POST,
437 MHD_HTTP_METHOD_PUT, 439 MHD_HTTP_METHOD_PUT,
438 MHD_HTTP_METHOD_DELETE, 440 MHD_HTTP_METHOD_DELETE,
439 MHD_HTTP_METHOD_OPTIONS); 441 MHD_HTTP_METHOD_OPTIONS);
440 442
441 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, _("Gns REST API initialized\n")); 443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _ ("Gns REST API initialized\n"));
442 return api; 444 return api;
443} 445}
444 446
@@ -450,18 +452,17 @@ libgnunet_plugin_rest_gns_init(void *cls)
450 * @return always NULL 452 * @return always NULL
451 */ 453 */
452void * 454void *
453libgnunet_plugin_rest_gns_done(void *cls) 455libgnunet_plugin_rest_gns_done (void *cls)
454{ 456{
455 struct GNUNET_REST_Plugin *api = cls; 457 struct GNUNET_REST_Plugin *api = cls;
456 struct Plugin *plugin = api->cls; 458 struct Plugin *plugin = api->cls;
457 459
458 plugin->cfg = NULL; 460 plugin->cfg = NULL;
459 461
460 GNUNET_free_non_null(allow_methods); 462 GNUNET_free_non_null (allow_methods);
461 GNUNET_free(api); 463 GNUNET_free (api);
462 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Gns REST plugin is finished\n"); 464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Gns REST plugin is finished\n");
463 return NULL; 465 return NULL;
464} 466}
465 467
466/* end of plugin_rest_gns.c */ 468/* end of plugin_rest_gns.c */
467