aboutsummaryrefslogtreecommitdiff
path: root/src/rest
diff options
context:
space:
mode:
authorng0 <ng0@n0.is>2019-09-08 12:33:09 +0000
committerng0 <ng0@n0.is>2019-09-08 12:33:09 +0000
commitd41ed82a4ea0cc8e1674b6d5d2c49fd6462610bb (patch)
tree9efd18ea7d425652085ed0bd5e8e45604bc5f6b9 /src/rest
parenta0fce305c565c0937d917a92712f15e9c5736260 (diff)
downloadgnunet-d41ed82a4ea0cc8e1674b6d5d2c49fd6462610bb.tar.gz
gnunet-d41ed82a4ea0cc8e1674b6d5d2c49fd6462610bb.zip
uncrustify as demanded.
Diffstat (limited to 'src/rest')
-rw-r--r--src/rest/gnunet-rest-server.c1067
-rw-r--r--src/rest/plugin_rest_config.c339
-rw-r--r--src/rest/plugin_rest_copying.c112
-rw-r--r--src/rest/rest.c79
4 files changed, 798 insertions, 799 deletions
diff --git a/src/rest/gnunet-rest-server.c b/src/rest/gnunet-rest-server.c
index 30acc1125..eb930c17b 100644
--- a/src/rest/gnunet-rest-server.c
+++ b/src/rest/gnunet-rest-server.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19 */ 19 */
20/** 20/**
21 * @author Martin Schanzenbach 21 * @author Martin Schanzenbach
22 * @file src/rest/gnunet-rest-server.c 22 * @file src/rest/gnunet-rest-server.c
@@ -54,7 +54,7 @@
54 * After how long do we clean up unused MHD SSL/TLS instances? 54 * After how long do we clean up unused MHD SSL/TLS instances?
55 */ 55 */
56#define MHD_CACHE_TIMEOUT \ 56#define MHD_CACHE_TIMEOUT \
57 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5) 57 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
58 58
59#define GN_REST_STATE_INIT 0 59#define GN_REST_STATE_INIT 0
60#define GN_REST_STATE_PROCESSING 1 60#define GN_REST_STATE_PROCESSING 1
@@ -142,8 +142,7 @@ static char *allow_credentials;
142/** 142/**
143 * MHD Connection handle 143 * MHD Connection handle
144 */ 144 */
145struct MhdConnectionHandle 145struct MhdConnectionHandle {
146{
147 struct MHD_Connection *con; 146 struct MHD_Connection *con;
148 147
149 struct MHD_Response *response; 148 struct MHD_Response *response;
@@ -168,21 +167,21 @@ struct MhdConnectionHandle
168 * @param cls NULL 167 * @param cls NULL
169 */ 168 */
170static void 169static void
171do_httpd (void *cls); 170do_httpd(void *cls);
172 171
173 172
174/** 173/**
175 * Run MHD now, we have extra data ready for the callback. 174 * Run MHD now, we have extra data ready for the callback.
176 */ 175 */
177static void 176static void
178run_mhd_now () 177run_mhd_now()
179{ 178{
180 if (NULL != httpd_task) 179 if (NULL != httpd_task)
181 { 180 {
182 GNUNET_SCHEDULER_cancel (httpd_task); 181 GNUNET_SCHEDULER_cancel(httpd_task);
183 httpd_task = NULL; 182 httpd_task = NULL;
184 } 183 }
185 httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd, NULL); 184 httpd_task = GNUNET_SCHEDULER_add_now(&do_httpd, NULL);
186} 185}
187 186
188/** 187/**
@@ -194,121 +193,121 @@ run_mhd_now ()
194 * @param status #GNUNET_OK if successful 193 * @param status #GNUNET_OK if successful
195 */ 194 */
196static void 195static void
197plugin_callback (void *cls, struct MHD_Response *resp, int status) 196plugin_callback(void *cls, struct MHD_Response *resp, int status)
198{ 197{
199 struct MhdConnectionHandle *handle = cls; 198 struct MhdConnectionHandle *handle = cls;
199
200 handle->status = status; 200 handle->status = status;
201 handle->response = resp; 201 handle->response = resp;
202 MHD_resume_connection (handle->con); 202 MHD_resume_connection(handle->con);
203 run_mhd_now (); 203 run_mhd_now();
204} 204}
205 205
206 206
207static int 207static int
208cleanup_url_map (void *cls, const struct GNUNET_HashCode *key, void *value) 208cleanup_url_map(void *cls, const struct GNUNET_HashCode *key, void *value)
209{ 209{
210 GNUNET_free_non_null (value); 210 GNUNET_free_non_null(value);
211 return GNUNET_YES; 211 return GNUNET_YES;
212} 212}
213 213
214 214
215static void 215static void
216cleanup_handle (struct MhdConnectionHandle *handle) 216cleanup_handle(struct MhdConnectionHandle *handle)
217{ 217{
218 if (NULL != handle->response) 218 if (NULL != handle->response)
219 MHD_destroy_response (handle->response); 219 MHD_destroy_response(handle->response);
220 if (NULL != handle->data_handle) 220 if (NULL != handle->data_handle)
221 {
222
223 if (NULL != handle->data_handle->header_param_map)
224 {
225 GNUNET_CONTAINER_multihashmap_iterate (handle->data_handle
226 ->header_param_map,
227 &cleanup_url_map,
228 NULL);
229 GNUNET_CONTAINER_multihashmap_destroy (
230 handle->data_handle->header_param_map);
231 }
232 if (NULL != handle->data_handle->url_param_map)
233 { 221 {
234 GNUNET_CONTAINER_multihashmap_iterate (handle->data_handle->url_param_map, 222 if (NULL != handle->data_handle->header_param_map)
235 &cleanup_url_map, 223 {
236 NULL); 224 GNUNET_CONTAINER_multihashmap_iterate(handle->data_handle
237 GNUNET_CONTAINER_multihashmap_destroy ( 225 ->header_param_map,
238 handle->data_handle->url_param_map); 226 &cleanup_url_map,
227 NULL);
228 GNUNET_CONTAINER_multihashmap_destroy(
229 handle->data_handle->header_param_map);
230 }
231 if (NULL != handle->data_handle->url_param_map)
232 {
233 GNUNET_CONTAINER_multihashmap_iterate(handle->data_handle->url_param_map,
234 &cleanup_url_map,
235 NULL);
236 GNUNET_CONTAINER_multihashmap_destroy(
237 handle->data_handle->url_param_map);
238 }
239 GNUNET_free(handle->data_handle);
239 } 240 }
240 GNUNET_free (handle->data_handle); 241 GNUNET_free(handle);
241 }
242 GNUNET_free (handle);
243} 242}
244 243
245static int 244static int
246header_iterator (void *cls, 245header_iterator(void *cls,
247 enum MHD_ValueKind kind, 246 enum MHD_ValueKind kind,
248 const char *key, 247 const char *key,
249 const char *value) 248 const char *value)
250{ 249{
251 struct GNUNET_REST_RequestHandle *handle = cls; 250 struct GNUNET_REST_RequestHandle *handle = cls;
252 struct GNUNET_HashCode hkey; 251 struct GNUNET_HashCode hkey;
253 char *val; 252 char *val;
254 char *lowerkey; 253 char *lowerkey;
255 254
256 lowerkey = GNUNET_strdup (key); 255 lowerkey = GNUNET_strdup(key);
257 GNUNET_STRINGS_utf8_tolower (key, lowerkey); 256 GNUNET_STRINGS_utf8_tolower(key, lowerkey);
258 GNUNET_CRYPTO_hash (lowerkey, strlen (lowerkey), &hkey); 257 GNUNET_CRYPTO_hash(lowerkey, strlen(lowerkey), &hkey);
259 GNUNET_asprintf (&val, "%s", value); 258 GNUNET_asprintf(&val, "%s", value);
260 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put ( 259 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put(
261 handle->header_param_map, 260 handle->header_param_map,
262 &hkey, 261 &hkey,
263 val, 262 val,
264 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) 263 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
265 { 264 {
266 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 265 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
267 "Could not load add header `%s'=%s\n", 266 "Could not load add header `%s'=%s\n",
268 lowerkey, 267 lowerkey,
269 value); 268 value);
270 } 269 }
271 GNUNET_free (lowerkey); 270 GNUNET_free(lowerkey);
272 return MHD_YES; 271 return MHD_YES;
273} 272}
274 273
275 274
276static int 275static int
277url_iterator (void *cls, 276url_iterator(void *cls,
278 enum MHD_ValueKind kind, 277 enum MHD_ValueKind kind,
279 const char *key, 278 const char *key,
280 const char *value) 279 const char *value)
281{ 280{
282 struct GNUNET_REST_RequestHandle *handle = cls; 281 struct GNUNET_REST_RequestHandle *handle = cls;
283 struct GNUNET_HashCode hkey; 282 struct GNUNET_HashCode hkey;
284 char *val; 283 char *val;
285 284
286 GNUNET_CRYPTO_hash (key, strlen (key), &hkey); 285 GNUNET_CRYPTO_hash(key, strlen(key), &hkey);
287 GNUNET_asprintf (&val, "%s", value); 286 GNUNET_asprintf(&val, "%s", value);
288 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put ( 287 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put(
289 handle->url_param_map, 288 handle->url_param_map,
290 &hkey, 289 &hkey,
291 val, 290 val,
292 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) 291 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
293 { 292 {
294 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 293 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
295 "Could not load add url param `%s'=%s\n", 294 "Could not load add url param `%s'=%s\n",
296 key, 295 key,
297 value); 296 value);
298 } 297 }
299 return MHD_YES; 298 return MHD_YES;
300} 299}
301 300
302static int 301static int
303post_data_iter (void *cls, 302post_data_iter(void *cls,
304 enum MHD_ValueKind kind, 303 enum MHD_ValueKind kind,
305 const char *key, 304 const char *key,
306 const char *filename, 305 const char *filename,
307 const char *content_type, 306 const char *content_type,
308 const char *transfer_encoding, 307 const char *transfer_encoding,
309 const char *data, 308 const char *data,
310 uint64_t off, 309 uint64_t off,
311 size_t size) 310 size_t size)
312{ 311{
313 struct GNUNET_REST_RequestHandle *handle = cls; 312 struct GNUNET_REST_RequestHandle *handle = cls;
314 struct GNUNET_HashCode hkey; 313 struct GNUNET_HashCode hkey;
@@ -317,20 +316,20 @@ post_data_iter (void *cls,
317 if (MHD_POSTDATA_KIND != kind) 316 if (MHD_POSTDATA_KIND != kind)
318 return MHD_YES; 317 return MHD_YES;
319 318
320 GNUNET_CRYPTO_hash (key, strlen (key), &hkey); 319 GNUNET_CRYPTO_hash(key, strlen(key), &hkey);
321 GNUNET_asprintf (&val, "%s", data); 320 GNUNET_asprintf(&val, "%s", data);
322 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put ( 321 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put(
323 handle->url_param_map, 322 handle->url_param_map,
324 &hkey, 323 &hkey,
325 val, 324 val,
326 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) 325 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
327 { 326 {
328 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 327 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
329 "Could not load add url param '%s'=%s\n", 328 "Could not load add url param '%s'=%s\n",
330 key, 329 key,
331 data); 330 data);
332 GNUNET_free (val); 331 GNUNET_free(val);
333 } 332 }
334 return MHD_YES; 333 return MHD_YES;
335} 334}
336 335
@@ -360,14 +359,14 @@ post_data_iter (void *cls,
360 * error while handling the request 359 * error while handling the request
361 */ 360 */
362static int 361static int
363create_response (void *cls, 362create_response(void *cls,
364 struct MHD_Connection *con, 363 struct MHD_Connection *con,
365 const char *url, 364 const char *url,
366 const char *meth, 365 const char *meth,
367 const char *ver, 366 const char *ver,
368 const char *upload_data, 367 const char *upload_data,
369 size_t *upload_data_size, 368 size_t *upload_data_size,
370 void **con_cls) 369 void **con_cls)
371{ 370{
372 char *plugin_name; 371 char *plugin_name;
373 char *origin; 372 char *origin;
@@ -378,135 +377,135 @@ create_response (void *cls,
378 con_handle = *con_cls; 377 con_handle = *con_cls;
379 378
380 if (NULL == *con_cls) 379 if (NULL == *con_cls)
381 {
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New connection %s\n", url);
383 char tmp_url[strlen (url) + 1];
384 strcpy (tmp_url, url);
385 con_handle = GNUNET_new (struct MhdConnectionHandle);
386 con_handle->con = con;
387 con_handle->state = GN_REST_STATE_INIT;
388 *con_cls = con_handle;
389
390 plugin_name = strtok (tmp_url, "/");
391
392 if (NULL != plugin_name)
393 { 380 {
394 GNUNET_CRYPTO_hash (plugin_name, strlen (plugin_name), &key); 381 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "New connection %s\n", url);
382 char tmp_url[strlen(url) + 1];
383 strcpy(tmp_url, url);
384 con_handle = GNUNET_new(struct MhdConnectionHandle);
385 con_handle->con = con;
386 con_handle->state = GN_REST_STATE_INIT;
387 *con_cls = con_handle;
395 388
396 con_handle->plugin = GNUNET_CONTAINER_multihashmap_get (plugin_map, &key); 389 plugin_name = strtok(tmp_url, "/");
397 }
398 if (NULL == con_handle->plugin)
399 {
400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Queueing response with MHD\n");
401 GNUNET_free (con_handle);
402 return MHD_queue_response (con, MHD_HTTP_NOT_FOUND, failure_response);
403 }
404 390
405 return MHD_YES; 391 if (NULL != plugin_name)
406 } 392 {
393 GNUNET_CRYPTO_hash(plugin_name, strlen(plugin_name), &key);
394
395 con_handle->plugin = GNUNET_CONTAINER_multihashmap_get(plugin_map, &key);
396 }
397 if (NULL == con_handle->plugin)
398 {
399 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Queueing response with MHD\n");
400 GNUNET_free(con_handle);
401 return MHD_queue_response(con, MHD_HTTP_NOT_FOUND, failure_response);
402 }
403
404 return MHD_YES;
405 }
407 if (GN_REST_STATE_INIT == con_handle->state) 406 if (GN_REST_STATE_INIT == con_handle->state)
408 {
409 rest_conndata_handle = GNUNET_new (struct GNUNET_REST_RequestHandle);
410 rest_conndata_handle->method = meth;
411 rest_conndata_handle->url = url;
412 rest_conndata_handle->data = upload_data;
413 rest_conndata_handle->data_size = *upload_data_size;
414 rest_conndata_handle->url_param_map =
415 GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
416 rest_conndata_handle->header_param_map =
417 GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
418 con_handle->data_handle = rest_conndata_handle;
419 MHD_get_connection_values (con,
420 MHD_GET_ARGUMENT_KIND,
421 (MHD_KeyValueIterator) &url_iterator,
422 rest_conndata_handle);
423 MHD_get_connection_values (con,
424 MHD_HEADER_KIND,
425 (MHD_KeyValueIterator) &header_iterator,
426 rest_conndata_handle);
427 con_handle->pp = MHD_create_post_processor (con,
428 65536,
429 post_data_iter,
430 rest_conndata_handle);
431 if (*upload_data_size)
432 { 407 {
433 MHD_post_process (con_handle->pp, upload_data, *upload_data_size); 408 rest_conndata_handle = GNUNET_new(struct GNUNET_REST_RequestHandle);
409 rest_conndata_handle->method = meth;
410 rest_conndata_handle->url = url;
411 rest_conndata_handle->data = upload_data;
412 rest_conndata_handle->data_size = *upload_data_size;
413 rest_conndata_handle->url_param_map =
414 GNUNET_CONTAINER_multihashmap_create(16, GNUNET_NO);
415 rest_conndata_handle->header_param_map =
416 GNUNET_CONTAINER_multihashmap_create(16, GNUNET_NO);
417 con_handle->data_handle = rest_conndata_handle;
418 MHD_get_connection_values(con,
419 MHD_GET_ARGUMENT_KIND,
420 (MHD_KeyValueIterator) & url_iterator,
421 rest_conndata_handle);
422 MHD_get_connection_values(con,
423 MHD_HEADER_KIND,
424 (MHD_KeyValueIterator) & header_iterator,
425 rest_conndata_handle);
426 con_handle->pp = MHD_create_post_processor(con,
427 65536,
428 post_data_iter,
429 rest_conndata_handle);
430 if (*upload_data_size)
431 {
432 MHD_post_process(con_handle->pp, upload_data, *upload_data_size);
433 }
434 MHD_destroy_post_processor(con_handle->pp);
435
436 con_handle->state = GN_REST_STATE_PROCESSING;
437 con_handle->plugin->process_request(rest_conndata_handle,
438 &plugin_callback,
439 con_handle);
440 *upload_data_size = 0;
441 run_mhd_now();
442 return MHD_YES;
434 } 443 }
435 MHD_destroy_post_processor (con_handle->pp);
436
437 con_handle->state = GN_REST_STATE_PROCESSING;
438 con_handle->plugin->process_request (rest_conndata_handle,
439 &plugin_callback,
440 con_handle);
441 *upload_data_size = 0;
442 run_mhd_now ();
443 return MHD_YES;
444 }
445 if (NULL == con_handle->response) 444 if (NULL == con_handle->response)
446 {
447 //Suspend connection until plugin is done
448 MHD_suspend_connection (con_handle->con);
449 return MHD_YES;
450 }
451 MHD_resume_connection (con_handle->con);
452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
453 "Queueing response from plugin with MHD\n");
454 //Handle Preflights for extensions
455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Checking origin\n");
456 GNUNET_CRYPTO_hash ("origin", strlen ("origin"), &key);
457 origin = GNUNET_CONTAINER_multihashmap_get (con_handle->data_handle
458 ->header_param_map,
459 &key);
460 if (NULL != origin)
461 {
462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Origin: %s\n", origin);
463 //Only echo for browser plugins
464 if (GNUNET_YES == echo_origin)
465 { 445 {
466 if ((0 == 446 //Suspend connection until plugin is done
467 strncmp ("moz-extension://", origin, strlen ("moz-extension://"))) || 447 MHD_suspend_connection(con_handle->con);
468 (0 == strncmp ("chrome-extension://", 448 return MHD_YES;
469 origin,
470 strlen ("chrome-extension://"))))
471 {
472 MHD_add_response_header (con_handle->response,
473 MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN,
474 origin);
475 }
476 } 449 }
477 if (NULL != allow_origins) 450 MHD_resume_connection(con_handle->con);
451 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
452 "Queueing response from plugin with MHD\n");
453 //Handle Preflights for extensions
454 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Checking origin\n");
455 GNUNET_CRYPTO_hash("origin", strlen("origin"), &key);
456 origin = GNUNET_CONTAINER_multihashmap_get(con_handle->data_handle
457 ->header_param_map,
458 &key);
459 if (NULL != origin)
478 { 460 {
479 char *tmp = GNUNET_strdup (allow_origins); 461 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Origin: %s\n", origin);
480 char *allow_origin = strtok (tmp, ","); 462 //Only echo for browser plugins
481 while (NULL != allow_origin) 463 if (GNUNET_YES == echo_origin)
482 { 464 {
483 if (0 == strncmp (allow_origin, origin, strlen (allow_origin))) 465 if ((0 ==
466 strncmp("moz-extension://", origin, strlen("moz-extension://"))) ||
467 (0 == strncmp("chrome-extension://",
468 origin,
469 strlen("chrome-extension://"))))
470 {
471 MHD_add_response_header(con_handle->response,
472 MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN,
473 origin);
474 }
475 }
476 if (NULL != allow_origins)
484 { 477 {
485 MHD_add_response_header (con_handle->response, 478 char *tmp = GNUNET_strdup(allow_origins);
486 MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, 479 char *allow_origin = strtok(tmp, ",");
487 allow_origin); 480 while (NULL != allow_origin)
488 break; 481 {
482 if (0 == strncmp(allow_origin, origin, strlen(allow_origin)))
483 {
484 MHD_add_response_header(con_handle->response,
485 MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN,
486 allow_origin);
487 break;
488 }
489 allow_origin = strtok(NULL, ",");
490 }
491 GNUNET_free(tmp);
489 } 492 }
490 allow_origin = strtok (NULL, ",");
491 }
492 GNUNET_free (tmp);
493 } 493 }
494 }
495 if (NULL != allow_credentials) 494 if (NULL != allow_credentials)
496 { 495 {
497 MHD_add_response_header (con_handle->response, 496 MHD_add_response_header(con_handle->response,
498 "Access-Control-Allow-Credentials", 497 "Access-Control-Allow-Credentials",
499 allow_credentials); 498 allow_credentials);
500 } 499 }
501 if (NULL != allow_headers) 500 if (NULL != allow_headers)
502 { 501 {
503 MHD_add_response_header (con_handle->response, 502 MHD_add_response_header(con_handle->response,
504 "Access-Control-Allow-Headers", 503 "Access-Control-Allow-Headers",
505 allow_headers); 504 allow_headers);
506 } 505 }
507 run_mhd_now (); 506 run_mhd_now();
508 int ret = MHD_queue_response (con, con_handle->status, con_handle->response); 507 int ret = MHD_queue_response(con, con_handle->status, con_handle->response);
509 cleanup_handle (con_handle); 508 cleanup_handle(con_handle);
510 return ret; 509 return ret;
511} 510}
512 511
@@ -523,15 +522,15 @@ create_response (void *cls,
523 * @param toe reason for request termination (ignored) 522 * @param toe reason for request termination (ignored)
524 */ 523 */
525static void 524static void
526mhd_completed_cb (void *cls, 525mhd_completed_cb(void *cls,
527 struct MHD_Connection *connection, 526 struct MHD_Connection *connection,
528 void **con_cls, 527 void **con_cls,
529 enum MHD_RequestTerminationCode toe) 528 enum MHD_RequestTerminationCode toe)
530{ 529{
531 if (MHD_REQUEST_TERMINATED_COMPLETED_OK != toe) 530 if (MHD_REQUEST_TERMINATED_COMPLETED_OK != toe)
532 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 531 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
533 "MHD encountered error handling request: %d\n", 532 "MHD encountered error handling request: %d\n",
534 toe); 533 toe);
535} 534}
536 535
537 536
@@ -539,39 +538,39 @@ mhd_completed_cb (void *cls,
539 * Kill the MHD daemon. 538 * Kill the MHD daemon.
540 */ 539 */
541static void 540static void
542kill_httpd () 541kill_httpd()
543{ 542{
544 if (NULL != httpd) 543 if (NULL != httpd)
545 { 544 {
546 MHD_stop_daemon (httpd); 545 MHD_stop_daemon(httpd);
547 httpd = NULL; 546 httpd = NULL;
548 } 547 }
549 if (NULL != httpd_task) 548 if (NULL != httpd_task)
550 { 549 {
551 GNUNET_SCHEDULER_cancel (httpd_task); 550 GNUNET_SCHEDULER_cancel(httpd_task);
552 httpd_task = NULL; 551 httpd_task = NULL;
553 } 552 }
554 if (NULL != ltask4) 553 if (NULL != ltask4)
555 { 554 {
556 GNUNET_SCHEDULER_cancel (ltask4); 555 GNUNET_SCHEDULER_cancel(ltask4);
557 ltask4 = NULL; 556 ltask4 = NULL;
558 } 557 }
559 if (NULL != ltask6) 558 if (NULL != ltask6)
560 { 559 {
561 GNUNET_SCHEDULER_cancel (ltask6); 560 GNUNET_SCHEDULER_cancel(ltask6);
562 ltask6 = NULL; 561 ltask6 = NULL;
563 } 562 }
564 563
565 if (NULL != lsock4) 564 if (NULL != lsock4)
566 { 565 {
567 GNUNET_NETWORK_socket_close (lsock4); 566 GNUNET_NETWORK_socket_close(lsock4);
568 lsock4 = NULL; 567 lsock4 = NULL;
569 } 568 }
570 if (NULL != lsock6) 569 if (NULL != lsock6)
571 { 570 {
572 GNUNET_NETWORK_socket_close (lsock6); 571 GNUNET_NETWORK_socket_close(lsock6);
573 lsock6 = NULL; 572 lsock6 = NULL;
574 } 573 }
575} 574}
576 575
577 576
@@ -583,7 +582,7 @@ kill_httpd ()
583 * @param hd the daemon to schedule 582 * @param hd the daemon to schedule
584 */ 583 */
585static void 584static void
586schedule_httpd () 585schedule_httpd()
587{ 586{
588 fd_set rs; 587 fd_set rs;
589 fd_set ws; 588 fd_set ws;
@@ -595,50 +594,50 @@ schedule_httpd ()
595 MHD_UNSIGNED_LONG_LONG timeout; 594 MHD_UNSIGNED_LONG_LONG timeout;
596 struct GNUNET_TIME_Relative tv; 595 struct GNUNET_TIME_Relative tv;
597 596
598 FD_ZERO (&rs); 597 FD_ZERO(&rs);
599 FD_ZERO (&ws); 598 FD_ZERO(&ws);
600 FD_ZERO (&es); 599 FD_ZERO(&es);
601 max = -1; 600 max = -1;
602 if (MHD_YES != MHD_get_fdset (httpd, &rs, &ws, &es, &max)) 601 if (MHD_YES != MHD_get_fdset(httpd, &rs, &ws, &es, &max))
603 { 602 {
604 kill_httpd (); 603 kill_httpd();
605 return; 604 return;
606 } 605 }
607 haveto = MHD_get_timeout (httpd, &timeout); 606 haveto = MHD_get_timeout(httpd, &timeout);
608 if (MHD_YES == haveto) 607 if (MHD_YES == haveto)
609 tv.rel_value_us = (uint64_t) timeout * 1000LL; 608 tv.rel_value_us = (uint64_t)timeout * 1000LL;
610 else 609 else
611 tv = GNUNET_TIME_UNIT_FOREVER_REL; 610 tv = GNUNET_TIME_UNIT_FOREVER_REL;
612 if (-1 != max) 611 if (-1 != max)
613 { 612 {
614 wrs = GNUNET_NETWORK_fdset_create (); 613 wrs = GNUNET_NETWORK_fdset_create();
615 wws = GNUNET_NETWORK_fdset_create (); 614 wws = GNUNET_NETWORK_fdset_create();
616 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1); 615 GNUNET_NETWORK_fdset_copy_native(wrs, &rs, max + 1);
617 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1); 616 GNUNET_NETWORK_fdset_copy_native(wws, &ws, max + 1);
618 } 617 }
619 else 618 else
620 { 619 {
621 wrs = NULL; 620 wrs = NULL;
622 wws = NULL; 621 wws = NULL;
623 } 622 }
624 if (NULL != httpd_task) 623 if (NULL != httpd_task)
625 { 624 {
626 GNUNET_SCHEDULER_cancel (httpd_task); 625 GNUNET_SCHEDULER_cancel(httpd_task);
627 httpd_task = NULL; 626 httpd_task = NULL;
628 } 627 }
629 if ((MHD_YES == haveto) || (-1 != max)) 628 if ((MHD_YES == haveto) || (-1 != max))
630 { 629 {
631 httpd_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, 630 httpd_task = GNUNET_SCHEDULER_add_select(GNUNET_SCHEDULER_PRIORITY_DEFAULT,
632 tv, 631 tv,
633 wrs, 632 wrs,
634 wws, 633 wws,
635 &do_httpd, 634 &do_httpd,
636 NULL); 635 NULL);
637 } 636 }
638 if (NULL != wrs) 637 if (NULL != wrs)
639 GNUNET_NETWORK_fdset_destroy (wrs); 638 GNUNET_NETWORK_fdset_destroy(wrs);
640 if (NULL != wws) 639 if (NULL != wws)
641 GNUNET_NETWORK_fdset_destroy (wws); 640 GNUNET_NETWORK_fdset_destroy(wws);
642} 641}
643 642
644/** 643/**
@@ -647,11 +646,11 @@ schedule_httpd ()
647 * @param cls NULL 646 * @param cls NULL
648 */ 647 */
649static void 648static void
650do_httpd (void *cls) 649do_httpd(void *cls)
651{ 650{
652 httpd_task = NULL; 651 httpd_task = NULL;
653 MHD_run (httpd); 652 MHD_run(httpd);
654 schedule_httpd (); 653 schedule_httpd();
655} 654}
656 655
657 656
@@ -662,7 +661,7 @@ do_httpd (void *cls)
662 * @param tc the scheduler context 661 * @param tc the scheduler context
663 */ 662 */
664static void 663static void
665do_accept (void *cls) 664do_accept(void *cls)
666{ 665{
667 struct GNUNET_NETWORK_Handle *lsock = cls; 666 struct GNUNET_NETWORK_Handle *lsock = cls;
668 struct GNUNET_NETWORK_Handle *s; 667 struct GNUNET_NETWORK_Handle *s;
@@ -670,42 +669,42 @@ do_accept (void *cls)
670 const struct sockaddr *addr; 669 const struct sockaddr *addr;
671 socklen_t len; 670 socklen_t len;
672 671
673 GNUNET_assert (NULL != lsock); 672 GNUNET_assert(NULL != lsock);
674 if (lsock == lsock4) 673 if (lsock == lsock4)
675 { 674 {
676 ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 675 ltask4 = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
677 lsock, 676 lsock,
678 &do_accept, 677 &do_accept,
679 lsock); 678 lsock);
680 } 679 }
681 else if (lsock == lsock6) 680 else if (lsock == lsock6)
682 { 681 {
683 ltask6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 682 ltask6 = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
684 lsock, 683 lsock,
685 &do_accept, 684 &do_accept,
686 lsock); 685 lsock);
687 } 686 }
688 else 687 else
689 GNUNET_assert (0); 688 GNUNET_assert(0);
690 s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL); 689 s = GNUNET_NETWORK_socket_accept(lsock, NULL, NULL);
691 if (NULL == s) 690 if (NULL == s)
692 { 691 {
693 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "accept"); 692 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "accept");
694 return; 693 return;
695 } 694 }
696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 695 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
697 "Got an inbound connection, waiting for data\n"); 696 "Got an inbound connection, waiting for data\n");
698 fd = GNUNET_NETWORK_get_fd (s); 697 fd = GNUNET_NETWORK_get_fd(s);
699 addr = GNUNET_NETWORK_get_addr (s); 698 addr = GNUNET_NETWORK_get_addr(s);
700 len = GNUNET_NETWORK_get_addrlen (s); 699 len = GNUNET_NETWORK_get_addrlen(s);
701 if (MHD_YES != MHD_add_connection (httpd, fd, addr, len)) 700 if (MHD_YES != MHD_add_connection(httpd, fd, addr, len))
702 { 701 {
703 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 702 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
704 _ ("Failed to pass client to MHD\n")); 703 _("Failed to pass client to MHD\n"));
705 return; 704 return;
706 } 705 }
707 GNUNET_free (s); 706 GNUNET_free(s);
708 schedule_httpd (); 707 schedule_httpd();
709} 708}
710 709
711 710
@@ -715,12 +714,12 @@ do_accept (void *cls)
715 * @param cls closure 714 * @param cls closure
716 */ 715 */
717static void 716static void
718do_shutdown (void *cls) 717do_shutdown(void *cls)
719{ 718{
720 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutting down...\n"); 719 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Shutting down...\n");
721 kill_httpd (); 720 kill_httpd();
722 GNUNET_free_non_null (allow_credentials); 721 GNUNET_free_non_null(allow_credentials);
723 GNUNET_free_non_null (allow_headers); 722 GNUNET_free_non_null(allow_headers);
724} 723}
725 724
726 725
@@ -730,31 +729,31 @@ do_shutdown (void *cls)
730 * @return NULL on error 729 * @return NULL on error
731 */ 730 */
732static struct GNUNET_NETWORK_Handle * 731static struct GNUNET_NETWORK_Handle *
733bind_v4 () 732bind_v4()
734{ 733{
735 struct GNUNET_NETWORK_Handle *ls; 734 struct GNUNET_NETWORK_Handle *ls;
736 struct sockaddr_in sa4; 735 struct sockaddr_in sa4;
737 int eno; 736 int eno;
738 737
739 memset (&sa4, 0, sizeof (sa4)); 738 memset(&sa4, 0, sizeof(sa4));
740 sa4.sin_family = AF_INET; 739 sa4.sin_family = AF_INET;
741 sa4.sin_port = htons (port); 740 sa4.sin_port = htons(port);
742 sa4.sin_addr.s_addr = address; 741 sa4.sin_addr.s_addr = address;
743#if HAVE_SOCKADDR_IN_SIN_LEN 742#if HAVE_SOCKADDR_IN_SIN_LEN
744 sa4.sin_len = sizeof (sa4); 743 sa4.sin_len = sizeof(sa4);
745#endif 744#endif
746 ls = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0); 745 ls = GNUNET_NETWORK_socket_create(AF_INET, SOCK_STREAM, 0);
747 if (NULL == ls) 746 if (NULL == ls)
748 return NULL; 747 return NULL;
749 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (ls, 748 if (GNUNET_OK != GNUNET_NETWORK_socket_bind(ls,
750 (const struct sockaddr *) &sa4, 749 (const struct sockaddr *)&sa4,
751 sizeof (sa4))) 750 sizeof(sa4)))
752 { 751 {
753 eno = errno; 752 eno = errno;
754 GNUNET_NETWORK_socket_close (ls); 753 GNUNET_NETWORK_socket_close(ls);
755 errno = eno; 754 errno = eno;
756 return NULL; 755 return NULL;
757 } 756 }
758 return ls; 757 return ls;
759} 758}
760 759
@@ -765,31 +764,31 @@ bind_v4 ()
765 * @return NULL on error 764 * @return NULL on error
766 */ 765 */
767static struct GNUNET_NETWORK_Handle * 766static struct GNUNET_NETWORK_Handle *
768bind_v6 () 767bind_v6()
769{ 768{
770 struct GNUNET_NETWORK_Handle *ls; 769 struct GNUNET_NETWORK_Handle *ls;
771 struct sockaddr_in6 sa6; 770 struct sockaddr_in6 sa6;
772 int eno; 771 int eno;
773 772
774 memset (&sa6, 0, sizeof (sa6)); 773 memset(&sa6, 0, sizeof(sa6));
775 sa6.sin6_family = AF_INET6; 774 sa6.sin6_family = AF_INET6;
776 sa6.sin6_port = htons (port); 775 sa6.sin6_port = htons(port);
777 sa6.sin6_addr = address6; 776 sa6.sin6_addr = address6;
778#if HAVE_SOCKADDR_IN_SIN_LEN 777#if HAVE_SOCKADDR_IN_SIN_LEN
779 sa6.sin6_len = sizeof (sa6); 778 sa6.sin6_len = sizeof(sa6);
780#endif 779#endif
781 ls = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_STREAM, 0); 780 ls = GNUNET_NETWORK_socket_create(AF_INET6, SOCK_STREAM, 0);
782 if (NULL == ls) 781 if (NULL == ls)
783 return NULL; 782 return NULL;
784 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (ls, 783 if (GNUNET_OK != GNUNET_NETWORK_socket_bind(ls,
785 (const struct sockaddr *) &sa6, 784 (const struct sockaddr *)&sa6,
786 sizeof (sa6))) 785 sizeof(sa6)))
787 { 786 {
788 eno = errno; 787 eno = errno;
789 GNUNET_NETWORK_socket_close (ls); 788 GNUNET_NETWORK_socket_close(ls);
790 errno = eno; 789 errno = eno;
791 return NULL; 790 return NULL;
792 } 791 }
793 return ls; 792 return ls;
794} 793}
795 794
@@ -802,32 +801,33 @@ bind_v6 ()
802 * @param lib_ret the object returned by the plugin initializer 801 * @param lib_ret the object returned by the plugin initializer
803 */ 802 */
804static void 803static void
805load_plugin (void *cls, const char *libname, void *lib_ret) 804load_plugin(void *cls, const char *libname, void *lib_ret)
806{ 805{
807 struct GNUNET_REST_Plugin *plugin = lib_ret; 806 struct GNUNET_REST_Plugin *plugin = lib_ret;
808 struct GNUNET_HashCode key; 807 struct GNUNET_HashCode key;
808
809 if (NULL == lib_ret) 809 if (NULL == lib_ret)
810 { 810 {
811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 811 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
812 "Could not load plugin `%s'\n", 812 "Could not load plugin `%s'\n",
813 libname); 813 libname);
814 return; 814 return;
815 } 815 }
816 GNUNET_assert (1 < strlen (plugin->name)); 816 GNUNET_assert(1 < strlen(plugin->name));
817 GNUNET_assert ('/' == *plugin->name); 817 GNUNET_assert('/' == *plugin->name);
818 GNUNET_CRYPTO_hash (plugin->name + 1, strlen (plugin->name + 1), &key); 818 GNUNET_CRYPTO_hash(plugin->name + 1, strlen(plugin->name + 1), &key);
819 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put ( 819 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put(
820 plugin_map, 820 plugin_map,
821 &key, 821 &key,
822 plugin, 822 plugin,
823 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) 823 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
824 { 824 {
825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 825 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
826 "Could not load add plugin `%s'\n", 826 "Could not load add plugin `%s'\n",
827 libname); 827 libname);
828 return; 828 return;
829 } 829 }
830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded plugin `%s'\n", libname); 830 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Loaded plugin `%s'\n", libname);
831} 831}
832 832
833 833
@@ -840,174 +840,175 @@ load_plugin (void *cls, const char *libname, void *lib_ret)
840 * @param c configuration 840 * @param c configuration
841 */ 841 */
842static void 842static void
843run (void *cls, 843run(void *cls,
844 char *const *args, 844 char *const *args,
845 const char *cfgfile, 845 const char *cfgfile,
846 const struct GNUNET_CONFIGURATION_Handle *c) 846 const struct GNUNET_CONFIGURATION_Handle *c)
847{ 847{
848 char *addr_str; 848 char *addr_str;
849
849 cfg = c; 850 cfg = c;
850 plugin_map = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO); 851 plugin_map = GNUNET_CONTAINER_multihashmap_create(10, GNUNET_NO);
851 852
852 /* Get port to bind to */ 853 /* Get port to bind to */
853 if (GNUNET_OK != 854 if (GNUNET_OK !=
854 GNUNET_CONFIGURATION_get_value_number (cfg, "rest", "HTTP_PORT", &port)) 855 GNUNET_CONFIGURATION_get_value_number(cfg, "rest", "HTTP_PORT", &port))
855 { 856 {
856 //No address specified 857 //No address specified
857 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Using default port...\n"); 858 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Using default port...\n");
858 port = GNUNET_REST_SERVICE_PORT; 859 port = GNUNET_REST_SERVICE_PORT;
859 } 860 }
860 861
861 /* Get address to bind to */ 862 /* Get address to bind to */
862 if (GNUNET_OK != 863 if (GNUNET_OK !=
863 GNUNET_CONFIGURATION_get_value_string (cfg, "rest", "BIND_TO", &addr_str)) 864 GNUNET_CONFIGURATION_get_value_string(cfg, "rest", "BIND_TO", &addr_str))
864 { 865 {
865 //No address specified 866 //No address specified
866 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Don't know what to bind to...\n"); 867 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Don't know what to bind to...\n");
867 GNUNET_SCHEDULER_shutdown (); 868 GNUNET_SCHEDULER_shutdown();
868 return; 869 return;
869 } 870 }
870 if (1 != inet_pton (AF_INET, addr_str, &address)) 871 if (1 != inet_pton(AF_INET, addr_str, &address))
871 { 872 {
872 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 873 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
873 "Unable to parse address %s\n", 874 "Unable to parse address %s\n",
874 addr_str); 875 addr_str);
875 GNUNET_free (addr_str); 876 GNUNET_free(addr_str);
876 GNUNET_SCHEDULER_shutdown (); 877 GNUNET_SCHEDULER_shutdown();
877 return; 878 return;
878 } 879 }
879 GNUNET_free (addr_str); 880 GNUNET_free(addr_str);
880 /* Get address to bind to */ 881 /* Get address to bind to */
881 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, 882 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg,
882 "rest", 883 "rest",
883 "BIND_TO6", 884 "BIND_TO6",
884 &addr_str)) 885 &addr_str))
885 { 886 {
886 //No address specified 887 //No address specified
887 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Don't know what to bind6 to...\n"); 888 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Don't know what to bind6 to...\n");
888 GNUNET_SCHEDULER_shutdown (); 889 GNUNET_SCHEDULER_shutdown();
889 return; 890 return;
890 } 891 }
891 if (1 != inet_pton (AF_INET6, addr_str, &address6)) 892 if (1 != inet_pton(AF_INET6, addr_str, &address6))
892 { 893 {
893 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 894 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
894 "Unable to parse IPv6 address %s\n", 895 "Unable to parse IPv6 address %s\n",
895 addr_str); 896 addr_str);
896 GNUNET_free (addr_str); 897 GNUNET_free(addr_str);
897 GNUNET_SCHEDULER_shutdown (); 898 GNUNET_SCHEDULER_shutdown();
898 return; 899 return;
899 } 900 }
900 GNUNET_free (addr_str); 901 GNUNET_free(addr_str);
901 902
902 903
903 /* Get CORS data from cfg */ 904 /* Get CORS data from cfg */
904 echo_origin = 905 echo_origin =
905 GNUNET_CONFIGURATION_get_value_yesno (cfg, 906 GNUNET_CONFIGURATION_get_value_yesno(cfg,
906 "rest", 907 "rest",
907 "REST_ECHO_ORIGIN_WEBEXT"); 908 "REST_ECHO_ORIGIN_WEBEXT");
908 allow_origins = NULL; 909 allow_origins = NULL;
909 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, 910 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg,
910 "rest", 911 "rest",
911 "REST_ALLOW_ORIGIN", 912 "REST_ALLOW_ORIGIN",
912 &allow_origins)) 913 &allow_origins))
913 { 914 {
914 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 915 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
915 "No CORS Access-Control-Allow-Origin header will be sent...\n"); 916 "No CORS Access-Control-Allow-Origin header will be sent...\n");
916 } 917 }
917 if (GNUNET_OK != 918 if (GNUNET_OK !=
918 GNUNET_CONFIGURATION_get_value_string (cfg, 919 GNUNET_CONFIGURATION_get_value_string(cfg,
919 "rest", 920 "rest",
920 "REST_ALLOW_CREDENTIALS", 921 "REST_ALLOW_CREDENTIALS",
921 &allow_credentials)) 922 &allow_credentials))
922 { 923 {
923 //No origin specified 924 //No origin specified
924 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 925 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
925 "No CORS Credential Header will be sent...\n"); 926 "No CORS Credential Header will be sent...\n");
926 } 927 }
927 928
928 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, 929 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg,
929 "rest", 930 "rest",
930 "REST_ALLOW_HEADERS", 931 "REST_ALLOW_HEADERS",
931 &allow_headers)) 932 &allow_headers))
932 { 933 {
933 //No origin specified 934 //No origin specified
934 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 935 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
935 "No CORS Access-Control-Allow-Headers Header will be sent...\n"); 936 "No CORS Access-Control-Allow-Headers Header will be sent...\n");
936 } 937 }
937 938
938 /* Open listen socket proxy */ 939 /* Open listen socket proxy */
939 lsock6 = bind_v6 (); 940 lsock6 = bind_v6();
940 if (NULL == lsock6) 941 if (NULL == lsock6)
941 {
942 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
943 }
944 else
945 {
946 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock6, 5))
947 { 942 {
948 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen"); 943 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "bind");
949 GNUNET_NETWORK_socket_close (lsock6);
950 lsock6 = NULL;
951 } 944 }
952 else 945 else
953 { 946 {
954 ltask6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 947 if (GNUNET_OK != GNUNET_NETWORK_socket_listen(lsock6, 5))
955 lsock6, 948 {
956 &do_accept, 949 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "listen");
957 lsock6); 950 GNUNET_NETWORK_socket_close(lsock6);
951 lsock6 = NULL;
952 }
953 else
954 {
955 ltask6 = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
956 lsock6,
957 &do_accept,
958 lsock6);
959 }
958 } 960 }
959 } 961 lsock4 = bind_v4();
960 lsock4 = bind_v4 ();
961 if (NULL == lsock4) 962 if (NULL == lsock4)
962 {
963 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
964 }
965 else
966 {
967 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock4, 5))
968 { 963 {
969 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen"); 964 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "bind");
970 GNUNET_NETWORK_socket_close (lsock4);
971 lsock4 = NULL;
972 } 965 }
973 else 966 else
974 { 967 {
975 ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 968 if (GNUNET_OK != GNUNET_NETWORK_socket_listen(lsock4, 5))
976 lsock4, 969 {
977 &do_accept, 970 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "listen");
978 lsock4); 971 GNUNET_NETWORK_socket_close(lsock4);
972 lsock4 = NULL;
973 }
974 else
975 {
976 ltask4 = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
977 lsock4,
978 &do_accept,
979 lsock4);
980 }
979 } 981 }
980 }
981 if ((NULL == lsock4) && (NULL == lsock6)) 982 if ((NULL == lsock4) && (NULL == lsock6))
982 { 983 {
983 GNUNET_SCHEDULER_shutdown (); 984 GNUNET_SCHEDULER_shutdown();
984 return; 985 return;
985 } 986 }
986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service listens on port %llu\n", port); 987 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Service listens on port %llu\n", port);
987 httpd = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_NO_LISTEN_SOCKET | 988 httpd = MHD_start_daemon(MHD_USE_DEBUG | MHD_USE_NO_LISTEN_SOCKET |
988 MHD_ALLOW_SUSPEND_RESUME, 989 MHD_ALLOW_SUSPEND_RESUME,
989 0, 990 0,
990 NULL, 991 NULL,
991 NULL, 992 NULL,
992 &create_response, 993 &create_response,
993 NULL, 994 NULL,
994 MHD_OPTION_CONNECTION_TIMEOUT, 995 MHD_OPTION_CONNECTION_TIMEOUT,
995 (unsigned int) 16, 996 (unsigned int)16,
996 MHD_OPTION_NOTIFY_COMPLETED, 997 MHD_OPTION_NOTIFY_COMPLETED,
997 &mhd_completed_cb, 998 &mhd_completed_cb,
998 NULL, 999 NULL,
999 MHD_OPTION_END); 1000 MHD_OPTION_END);
1000 if (NULL == httpd) 1001 if (NULL == httpd)
1001 { 1002 {
1002 GNUNET_SCHEDULER_shutdown (); 1003 GNUNET_SCHEDULER_shutdown();
1003 return; 1004 return;
1004 } 1005 }
1005 /* Load plugins */ 1006 /* Load plugins */
1006 GNUNET_PLUGIN_load_all ("libgnunet_plugin_rest", 1007 GNUNET_PLUGIN_load_all("libgnunet_plugin_rest",
1007 (void *) cfg, 1008 (void *)cfg,
1008 &load_plugin, 1009 &load_plugin,
1009 NULL); 1010 NULL);
1010 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL); 1011 GNUNET_SCHEDULER_add_shutdown(&do_shutdown, NULL);
1011} 1012}
1012 1013
1013 1014
@@ -1021,29 +1022,29 @@ run (void *cls,
1021 * 1022 *
1022 */ 1023 */
1023int 1024int
1024main (int argc, char *const *argv) 1025main(int argc, char *const *argv)
1025{ 1026{
1026 struct GNUNET_GETOPT_CommandLineOption options[] = {GNUNET_GETOPT_OPTION_END}; 1027 struct GNUNET_GETOPT_CommandLineOption options[] = { GNUNET_GETOPT_OPTION_END };
1027 static const char *err_page = "{}"; 1028 static const char *err_page = "{}";
1028 int ret; 1029 int ret;
1029 1030
1030 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) 1031 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv))
1031 return 2; 1032 return 2;
1032 GNUNET_log_setup ("gnunet-rest-server", "WARNING", NULL); 1033 GNUNET_log_setup("gnunet-rest-server", "WARNING", NULL);
1033 failure_response = MHD_create_response_from_buffer (strlen (err_page), 1034 failure_response = MHD_create_response_from_buffer(strlen(err_page),
1034 (void *) err_page, 1035 (void *)err_page,
1035 MHD_RESPMEM_PERSISTENT); 1036 MHD_RESPMEM_PERSISTENT);
1036 ret = (GNUNET_OK == GNUNET_PROGRAM_run (argc, 1037 ret = (GNUNET_OK == GNUNET_PROGRAM_run(argc,
1037 argv, 1038 argv,
1038 "gnunet-rest-server", 1039 "gnunet-rest-server",
1039 _ ("GNUnet REST server"), 1040 _("GNUnet REST server"),
1040 options, 1041 options,
1041 &run, 1042 &run,
1042 NULL)) 1043 NULL))
1043 ? 0 1044 ? 0
1044 : 1; 1045 : 1;
1045 MHD_destroy_response (failure_response); 1046 MHD_destroy_response(failure_response);
1046 GNUNET_free_non_null ((char *) argv); 1047 GNUNET_free_non_null((char *)argv);
1047 return ret; 1048 return ret;
1048} 1049}
1049 1050
diff --git a/src/rest/plugin_rest_config.c b/src/rest/plugin_rest_config.c
index 7f1d0eedf..55797f543 100644
--- a/src/rest/plugin_rest_config.c
+++ b/src/rest/plugin_rest_config.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19 */ 19 */
20/** 20/**
21 * @author Martin Schanzenbach 21 * @author Martin Schanzenbach
22 * @file gns/plugin_rest_config.c 22 * @file gns/plugin_rest_config.c
@@ -35,15 +35,13 @@
35/** 35/**
36 * @brief struct returned by the initialization function of the plugin 36 * @brief struct returned by the initialization function of the plugin
37 */ 37 */
38struct Plugin 38struct Plugin {
39{
40 const struct GNUNET_CONFIGURATION_Handle *cfg; 39 const struct GNUNET_CONFIGURATION_Handle *cfg;
41}; 40};
42 41
43const struct GNUNET_CONFIGURATION_Handle *cfg; 42const struct GNUNET_CONFIGURATION_Handle *cfg;
44 43
45struct RequestHandle 44struct RequestHandle {
46{
47 /** 45 /**
48 * Handle to rest request 46 * Handle to rest request
49 */ 47 */
@@ -77,12 +75,12 @@ struct RequestHandle
77 * @param handle Handle to clean up 75 * @param handle Handle to clean up
78 */ 76 */
79static void 77static void
80cleanup_handle (struct RequestHandle *handle) 78cleanup_handle(struct RequestHandle *handle)
81{ 79{
82 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n"); 80 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
83 if (NULL != handle->url) 81 if (NULL != handle->url)
84 GNUNET_free (handle->url); 82 GNUNET_free(handle->url);
85 GNUNET_free (handle); 83 GNUNET_free(handle);
86} 84}
87 85
88 86
@@ -93,45 +91,46 @@ cleanup_handle (struct RequestHandle *handle)
93 * @param tc scheduler context 91 * @param tc scheduler context
94 */ 92 */
95static void 93static void
96do_error (void *cls) 94do_error(void *cls)
97{ 95{
98 struct RequestHandle *handle = cls; 96 struct RequestHandle *handle = cls;
99 struct MHD_Response *resp; 97 struct MHD_Response *resp;
100 98
101 resp = GNUNET_REST_create_response (NULL); 99 resp = GNUNET_REST_create_response(NULL);
102 handle->proc (handle->proc_cls, resp, handle->response_code); 100 handle->proc(handle->proc_cls, resp, handle->response_code);
103 cleanup_handle (handle); 101 cleanup_handle(handle);
104} 102}
105 103
106 104
107static void 105static void
108add_sections (void *cls, 106add_sections(void *cls,
109 const char *section, 107 const char *section,
110 const char *option, 108 const char *option,
111 const char *value) 109 const char *value)
112{ 110{
113 json_t *sections_obj = cls; 111 json_t *sections_obj = cls;
114 json_t *sec_obj; 112 json_t *sec_obj;
115 113
116 sec_obj = json_object_get (sections_obj, section); 114 sec_obj = json_object_get(sections_obj, section);
117 if (NULL != sec_obj) 115 if (NULL != sec_obj)
118 { 116 {
119 json_object_set_new (sec_obj, option, json_string (value)); 117 json_object_set_new(sec_obj, option, json_string(value));
120 return; 118 return;
121 } 119 }
122 sec_obj = json_object (); 120 sec_obj = json_object();
123 json_object_set_new (sec_obj, option, json_string (value)); 121 json_object_set_new(sec_obj, option, json_string(value));
124 json_object_set_new (sections_obj, section, sec_obj); 122 json_object_set_new(sections_obj, section, sec_obj);
125} 123}
126 124
127static void 125static void
128add_section_contents (void *cls, 126add_section_contents(void *cls,
129 const char *section, 127 const char *section,
130 const char *option, 128 const char *option,
131 const char *value) 129 const char *value)
132{ 130{
133 json_t *section_obj = cls; 131 json_t *section_obj = cls;
134 json_object_set_new (section_obj, option, json_string (value)); 132
133 json_object_set_new(section_obj, option, json_string(value));
135} 134}
136 135
137/** 136/**
@@ -140,9 +139,9 @@ add_section_contents (void *cls,
140 * @param handle the lookup handle 139 * @param handle the lookup handle
141 */ 140 */
142static void 141static void
143get_cont (struct GNUNET_REST_RequestHandle *con_handle, 142get_cont(struct GNUNET_REST_RequestHandle *con_handle,
144 const char *url, 143 const char *url,
145 void *cls) 144 void *cls)
146{ 145{
147 struct MHD_Response *resp; 146 struct MHD_Response *resp;
148 struct RequestHandle *handle = cls; 147 struct RequestHandle *handle = cls;
@@ -150,50 +149,50 @@ get_cont (struct GNUNET_REST_RequestHandle *con_handle,
150 char *response; 149 char *response;
151 json_t *result; 150 json_t *result;
152 151
153 if (strlen (GNUNET_REST_API_NS_CONFIG) > strlen (handle->url)) 152 if (strlen(GNUNET_REST_API_NS_CONFIG) > strlen(handle->url))
154 { 153 {
155 handle->response_code = MHD_HTTP_BAD_REQUEST; 154 handle->response_code = MHD_HTTP_BAD_REQUEST;
156 GNUNET_SCHEDULER_add_now (&do_error, handle); 155 GNUNET_SCHEDULER_add_now(&do_error, handle);
157 return; 156 return;
158 } 157 }
159 if (strlen (GNUNET_REST_API_NS_CONFIG) == strlen (handle->url)) 158 if (strlen(GNUNET_REST_API_NS_CONFIG) == strlen(handle->url))
160 { 159 {
161 result = json_object (); 160 result = json_object();
162 GNUNET_CONFIGURATION_iterate (cfg, &add_sections, result); 161 GNUNET_CONFIGURATION_iterate(cfg, &add_sections, result);
163 } 162 }
164 else 163 else
165 { 164 {
166 result = json_object (); 165 result = json_object();
167 section = &handle->url[strlen (GNUNET_REST_API_NS_CONFIG) + 1]; 166 section = &handle->url[strlen(GNUNET_REST_API_NS_CONFIG) + 1];
168 GNUNET_CONFIGURATION_iterate_section_values (cfg, 167 GNUNET_CONFIGURATION_iterate_section_values(cfg,
169 section, 168 section,
170 &add_section_contents, 169 &add_section_contents,
171 result); 170 result);
172 } 171 }
173 response = json_dumps (result, 0); 172 response = json_dumps(result, 0);
174 resp = GNUNET_REST_create_response (response); 173 resp = GNUNET_REST_create_response(response);
175 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); 174 handle->proc(handle->proc_cls, resp, MHD_HTTP_OK);
176 cleanup_handle (handle); 175 cleanup_handle(handle);
177 GNUNET_free (response); 176 GNUNET_free(response);
178 json_decref (result); 177 json_decref(result);
179} 178}
180 179
181struct GNUNET_CONFIGURATION_Handle * 180struct GNUNET_CONFIGURATION_Handle *
182set_value (struct GNUNET_CONFIGURATION_Handle *config, 181set_value(struct GNUNET_CONFIGURATION_Handle *config,
183 const char *section, 182 const char *section,
184 const char *option, 183 const char *option,
185 json_t *value) 184 json_t *value)
186{ 185{
187 if (json_is_string (value)) 186 if (json_is_string(value))
188 GNUNET_CONFIGURATION_set_value_string (config, section, option, json_string_value (value)); 187 GNUNET_CONFIGURATION_set_value_string(config, section, option, json_string_value(value));
189 else if (json_is_number (value)) 188 else if (json_is_number(value))
190 GNUNET_CONFIGURATION_set_value_number (config, section, option, json_integer_value (value)); 189 GNUNET_CONFIGURATION_set_value_number(config, section, option, json_integer_value(value));
191 else if (json_is_null (value)) 190 else if (json_is_null(value))
192 GNUNET_CONFIGURATION_set_value_string (config, section, option, NULL); 191 GNUNET_CONFIGURATION_set_value_string(config, section, option, NULL);
193 else if (json_is_true (value)) 192 else if (json_is_true(value))
194 GNUNET_CONFIGURATION_set_value_string (config, section, option, "yes"); 193 GNUNET_CONFIGURATION_set_value_string(config, section, option, "yes");
195 else if (json_is_false (value)) 194 else if (json_is_false(value))
196 GNUNET_CONFIGURATION_set_value_string (config, section, option, "no"); 195 GNUNET_CONFIGURATION_set_value_string(config, section, option, "no");
197 else 196 else
198 return NULL; 197 return NULL;
199 return config; // for error handling (0 -> success, 1 -> error) 198 return config; // for error handling (0 -> success, 1 -> error)
@@ -205,13 +204,13 @@ set_value (struct GNUNET_CONFIGURATION_Handle *config,
205 * @param handle the lookup handle 204 * @param handle the lookup handle
206 */ 205 */
207static void 206static void
208set_cont (struct GNUNET_REST_RequestHandle *con_handle, 207set_cont(struct GNUNET_REST_RequestHandle *con_handle,
209 const char *url, 208 const char *url,
210 void *cls) 209 void *cls)
211{ 210{
212 struct RequestHandle *handle = cls; 211 struct RequestHandle *handle = cls;
213 char term_data[handle->rest_handle->data_size + 1]; 212 char term_data[handle->rest_handle->data_size + 1];
214 struct GNUNET_CONFIGURATION_Handle *out = GNUNET_CONFIGURATION_dup (cfg); 213 struct GNUNET_CONFIGURATION_Handle *out = GNUNET_CONFIGURATION_dup(cfg);
215 214
216 json_error_t err; 215 json_error_t err;
217 json_t *data_json; 216 json_t *data_json;
@@ -220,89 +219,89 @@ set_cont (struct GNUNET_REST_RequestHandle *con_handle,
220 json_t *sec_obj; 219 json_t *sec_obj;
221 json_t *value; 220 json_t *value;
222 char *cfg_fn; 221 char *cfg_fn;
223 222
224 // invalid url 223 // invalid url
225 if (strlen (GNUNET_REST_API_NS_CONFIG) > strlen (handle->url)) 224 if (strlen(GNUNET_REST_API_NS_CONFIG) > strlen(handle->url))
226 { 225 {
227 handle->response_code = MHD_HTTP_BAD_REQUEST; 226 handle->response_code = MHD_HTTP_BAD_REQUEST;
228 GNUNET_SCHEDULER_add_now (&do_error, handle); 227 GNUNET_SCHEDULER_add_now(&do_error, handle);
229 return; 228 return;
230 } 229 }
231 230
232 // extract data from handle 231 // extract data from handle
233 term_data[handle->rest_handle->data_size] = '\0'; 232 term_data[handle->rest_handle->data_size] = '\0';
234 GNUNET_memcpy (term_data, 233 GNUNET_memcpy(term_data,
235 handle->rest_handle->data, 234 handle->rest_handle->data,
236 handle->rest_handle->data_size); 235 handle->rest_handle->data_size);
237 data_json = json_loads (term_data, JSON_DECODE_ANY, &err); 236 data_json = json_loads(term_data, JSON_DECODE_ANY, &err);
238 237
239 if (NULL == data_json) 238 if (NULL == data_json)
240 { 239 {
241 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 240 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
242 "Unable to parse JSON Object from %s\n", 241 "Unable to parse JSON Object from %s\n",
243 term_data); 242 term_data);
244 GNUNET_SCHEDULER_add_now (&do_error, handle); 243 GNUNET_SCHEDULER_add_now(&do_error, handle);
245 return; 244 return;
246 } 245 }
247 246
248 // POST /config => {<section> : {<option> : <value>}} 247 // POST /config => {<section> : {<option> : <value>}}
249 if (strlen (GNUNET_REST_API_NS_CONFIG) == strlen (handle->url)) // POST /config 248 if (strlen(GNUNET_REST_API_NS_CONFIG) == strlen(handle->url)) // POST /config
250 { 249 {
251 // iterate over sections 250 // iterate over sections
252 json_object_foreach (data_json, section, sec_obj) 251 json_object_foreach(data_json, section, sec_obj)
253 {
254 // iterate over options
255 json_object_foreach (sec_obj, option, value)
256 { 252 {
257 out = set_value (out, section, option, value); 253 // iterate over options
258 if (NULL == out) 254 json_object_foreach(sec_obj, option, value)
259 { 255 {
260 handle->response_code = MHD_HTTP_BAD_REQUEST; 256 out = set_value(out, section, option, value);
261 GNUNET_SCHEDULER_add_now (&do_error, handle); 257 if (NULL == out)
262 json_decref (data_json); 258 {
263 return; 259 handle->response_code = MHD_HTTP_BAD_REQUEST;
260 GNUNET_SCHEDULER_add_now(&do_error, handle);
261 json_decref(data_json);
262 return;
263 }
264 } 264 }
265 } 265 }
266 } 266 }
267 }
268 else // POST /config/<section> => {<option> : <value>} 267 else // POST /config/<section> => {<option> : <value>}
269 {
270 // extract the "<section>" part from the url
271 section = &handle->url[strlen (GNUNET_REST_API_NS_CONFIG) + 1];
272 // iterate over options
273 json_object_foreach (data_json, option, value)
274 { 268 {
275 out = set_value (out, section, option, value); 269 // extract the "<section>" part from the url
276 if (NULL == out) 270 section = &handle->url[strlen(GNUNET_REST_API_NS_CONFIG) + 1];
271 // iterate over options
272 json_object_foreach(data_json, option, value)
277 { 273 {
278 handle->response_code = MHD_HTTP_BAD_REQUEST; 274 out = set_value(out, section, option, value);
279 GNUNET_SCHEDULER_add_now (&do_error, handle); 275 if (NULL == out)
280 json_decref (data_json); 276 {
281 return; 277 handle->response_code = MHD_HTTP_BAD_REQUEST;
278 GNUNET_SCHEDULER_add_now(&do_error, handle);
279 json_decref(data_json);
280 return;
281 }
282 } 282 }
283 } 283 }
284 } 284 json_decref(data_json);
285 json_decref (data_json);
286 285
287 286
288 // get cfg file path 287 // get cfg file path
289 cfg_fn = NULL; 288 cfg_fn = NULL;
290 const char *xdg = getenv ("XDG_CONFIG_HOME"); 289 const char *xdg = getenv("XDG_CONFIG_HOME");
291 if (NULL != xdg) 290 if (NULL != xdg)
292 GNUNET_asprintf (&cfg_fn, 291 GNUNET_asprintf(&cfg_fn,
293 "%s%s%s", 292 "%s%s%s",
294 xdg, 293 xdg,
295 DIR_SEPARATOR_STR, 294 DIR_SEPARATOR_STR,
296 GNUNET_OS_project_data_get ()->config_file); 295 GNUNET_OS_project_data_get()->config_file);
297 else 296 else
298 cfg_fn = GNUNET_strdup (GNUNET_OS_project_data_get ()->user_config_file); 297 cfg_fn = GNUNET_strdup(GNUNET_OS_project_data_get()->user_config_file);
299 298
300 GNUNET_CONFIGURATION_write (out, cfg_fn); 299 GNUNET_CONFIGURATION_write(out, cfg_fn);
301 cfg = out; 300 cfg = out;
302 handle->proc (handle->proc_cls, 301 handle->proc(handle->proc_cls,
303 GNUNET_REST_create_response (NULL), 302 GNUNET_REST_create_response(NULL),
304 MHD_HTTP_OK); 303 MHD_HTTP_OK);
305 cleanup_handle (handle); 304 cleanup_handle(handle);
306} 305}
307 306
308/** 307/**
@@ -311,19 +310,19 @@ set_cont (struct GNUNET_REST_RequestHandle *con_handle,
311 * @param handle the lookup handle 310 * @param handle the lookup handle
312 */ 311 */
313static void 312static void
314options_cont (struct GNUNET_REST_RequestHandle *con_handle, 313options_cont(struct GNUNET_REST_RequestHandle *con_handle,
315 const char *url, 314 const char *url,
316 void *cls) 315 void *cls)
317{ 316{
318 struct MHD_Response *resp; 317 struct MHD_Response *resp;
319 struct RequestHandle *handle = cls; 318 struct RequestHandle *handle = cls;
320 319
321 resp = GNUNET_REST_create_response (NULL); 320 resp = GNUNET_REST_create_response(NULL);
322 MHD_add_response_header (resp, 321 MHD_add_response_header(resp,
323 "Access-Control-Allow-Methods", 322 "Access-Control-Allow-Methods",
324 MHD_HTTP_METHOD_GET); 323 MHD_HTTP_METHOD_GET);
325 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); 324 handle->proc(handle->proc_cls, resp, MHD_HTTP_OK);
326 cleanup_handle (handle); 325 cleanup_handle(handle);
327} 326}
328 327
329 328
@@ -339,31 +338,32 @@ options_cont (struct GNUNET_REST_RequestHandle *con_handle,
339 * @return #GNUNET_OK if request accepted 338 * @return #GNUNET_OK if request accepted
340 */ 339 */
341static void 340static void
342rest_config_process_request (struct GNUNET_REST_RequestHandle *conndata_handle, 341rest_config_process_request(struct GNUNET_REST_RequestHandle *conndata_handle,
343 GNUNET_REST_ResultProcessor proc, 342 GNUNET_REST_ResultProcessor proc,
344 void *proc_cls) 343 void *proc_cls)
345{ 344{
346 static const struct GNUNET_REST_RequestHandler handlers[] = { 345 static const struct GNUNET_REST_RequestHandler handlers[] = {
347 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_CONFIG, &get_cont}, 346 { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_CONFIG, &get_cont },
348 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_CONFIG, &set_cont}, 347 { MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_CONFIG, &set_cont },
349 {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_CONFIG, &options_cont}, 348 { MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_CONFIG, &options_cont },
350 GNUNET_REST_HANDLER_END}; 349 GNUNET_REST_HANDLER_END
351 struct RequestHandle *handle = GNUNET_new (struct RequestHandle); 350 };
351 struct RequestHandle *handle = GNUNET_new(struct RequestHandle);
352 struct GNUNET_REST_RequestHandlerError err; 352 struct GNUNET_REST_RequestHandlerError err;
353 353
354 handle->proc_cls = proc_cls; 354 handle->proc_cls = proc_cls;
355 handle->proc = proc; 355 handle->proc = proc;
356 handle->rest_handle = conndata_handle; 356 handle->rest_handle = conndata_handle;
357 handle->url = GNUNET_strdup (conndata_handle->url); 357 handle->url = GNUNET_strdup(conndata_handle->url);
358 if (handle->url[strlen (handle->url) - 1] == '/') 358 if (handle->url[strlen(handle->url) - 1] == '/')
359 handle->url[strlen (handle->url) - 1] = '\0'; 359 handle->url[strlen(handle->url) - 1] = '\0';
360 360
361 if (GNUNET_NO == 361 if (GNUNET_NO ==
362 GNUNET_REST_handle_request (conndata_handle, handlers, &err, handle)) 362 GNUNET_REST_handle_request(conndata_handle, handlers, &err, handle))
363 { 363 {
364 handle->response_code = err.error_code; 364 handle->response_code = err.error_code;
365 GNUNET_SCHEDULER_add_now (&do_error, handle); 365 GNUNET_SCHEDULER_add_now(&do_error, handle);
366 } 366 }
367} 367}
368 368
369 369
@@ -374,21 +374,22 @@ rest_config_process_request (struct GNUNET_REST_RequestHandle *conndata_handle,
374 * @return NULL on error, otherwise the plugin context 374 * @return NULL on error, otherwise the plugin context
375 */ 375 */
376void * 376void *
377libgnunet_plugin_rest_config_init (void *cls) 377libgnunet_plugin_rest_config_init(void *cls)
378{ 378{
379 static struct Plugin plugin; 379 static struct Plugin plugin;
380
380 cfg = cls; 381 cfg = cls;
381 struct GNUNET_REST_Plugin *api; 382 struct GNUNET_REST_Plugin *api;
382 383
383 if (NULL != plugin.cfg) 384 if (NULL != plugin.cfg)
384 return NULL; /* can only initialize once! */ 385 return NULL; /* can only initialize once! */
385 memset (&plugin, 0, sizeof (struct Plugin)); 386 memset(&plugin, 0, sizeof(struct Plugin));
386 plugin.cfg = cfg; 387 plugin.cfg = cfg;
387 api = GNUNET_new (struct GNUNET_REST_Plugin); 388 api = GNUNET_new(struct GNUNET_REST_Plugin);
388 api->cls = &plugin; 389 api->cls = &plugin;
389 api->name = GNUNET_REST_API_NS_CONFIG; 390 api->name = GNUNET_REST_API_NS_CONFIG;
390 api->process_request = &rest_config_process_request; 391 api->process_request = &rest_config_process_request;
391 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("CONFIG REST API initialized\n")); 392 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("CONFIG REST API initialized\n"));
392 return api; 393 return api;
393} 394}
394 395
@@ -400,14 +401,14 @@ libgnunet_plugin_rest_config_init (void *cls)
400 * @return always NULL 401 * @return always NULL
401 */ 402 */
402void * 403void *
403libgnunet_plugin_rest_config_done (void *cls) 404libgnunet_plugin_rest_config_done(void *cls)
404{ 405{
405 struct GNUNET_REST_Plugin *api = cls; 406 struct GNUNET_REST_Plugin *api = cls;
406 struct Plugin *plugin = api->cls; 407 struct Plugin *plugin = api->cls;
407 408
408 plugin->cfg = NULL; 409 plugin->cfg = NULL;
409 GNUNET_free (api); 410 GNUNET_free(api);
410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONFIG REST plugin is finished\n"); 411 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "CONFIG REST plugin is finished\n");
411 return NULL; 412 return NULL;
412} 413}
413 414
diff --git a/src/rest/plugin_rest_copying.c b/src/rest/plugin_rest_copying.c
index 92c2c6601..4ebf032a7 100644
--- a/src/rest/plugin_rest_copying.c
+++ b/src/rest/plugin_rest_copying.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19 */ 19 */
20/** 20/**
21 * @author Martin Schanzenbach 21 * @author Martin Schanzenbach
22 * @file gns/plugin_rest_copying.c 22 * @file gns/plugin_rest_copying.c
@@ -35,15 +35,13 @@
35/** 35/**
36 * @brief struct returned by the initialization function of the plugin 36 * @brief struct returned by the initialization function of the plugin
37 */ 37 */
38struct Plugin 38struct Plugin {
39{
40 const struct GNUNET_CONFIGURATION_Handle *cfg; 39 const struct GNUNET_CONFIGURATION_Handle *cfg;
41}; 40};
42 41
43const struct GNUNET_CONFIGURATION_Handle *cfg; 42const struct GNUNET_CONFIGURATION_Handle *cfg;
44 43
45struct RequestHandle 44struct RequestHandle {
46{
47 /** 45 /**
48 * Handle to rest request 46 * Handle to rest request
49 */ 47 */
@@ -63,7 +61,6 @@ struct RequestHandle
63 * HTTP response code 61 * HTTP response code
64 */ 62 */
65 int response_code; 63 int response_code;
66
67}; 64};
68 65
69 66
@@ -73,11 +70,11 @@ struct RequestHandle
73 * @param handle Handle to clean up 70 * @param handle Handle to clean up
74 */ 71 */
75static void 72static void
76cleanup_handle (struct RequestHandle *handle) 73cleanup_handle(struct RequestHandle *handle)
77{ 74{
78 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 75 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
79 "Cleaning up\n"); 76 "Cleaning up\n");
80 GNUNET_free (handle); 77 GNUNET_free(handle);
81} 78}
82 79
83 80
@@ -88,14 +85,14 @@ cleanup_handle (struct RequestHandle *handle)
88 * @param tc scheduler context 85 * @param tc scheduler context
89 */ 86 */
90static void 87static void
91do_error (void *cls) 88do_error(void *cls)
92{ 89{
93 struct RequestHandle *handle = cls; 90 struct RequestHandle *handle = cls;
94 struct MHD_Response *resp; 91 struct MHD_Response *resp;
95 92
96 resp = GNUNET_REST_create_response (NULL); 93 resp = GNUNET_REST_create_response(NULL);
97 handle->proc (handle->proc_cls, resp, handle->response_code); 94 handle->proc(handle->proc_cls, resp, handle->response_code);
98 cleanup_handle (handle); 95 cleanup_handle(handle);
99} 96}
100 97
101 98
@@ -105,18 +102,18 @@ do_error (void *cls)
105 * @param handle the lookup handle 102 * @param handle the lookup handle
106 */ 103 */
107static void 104static void
108get_cont (struct GNUNET_REST_RequestHandle *con_handle, 105get_cont(struct GNUNET_REST_RequestHandle *con_handle,
109 const char* url, 106 const char* url,
110 void *cls) 107 void *cls)
111{ 108{
112 struct MHD_Response *resp; 109 struct MHD_Response *resp;
113 struct RequestHandle *handle = cls; 110 struct RequestHandle *handle = cls;
114 111
115 resp = GNUNET_REST_create_response (GNUNET_REST_COPYING_TEXT); 112 resp = GNUNET_REST_create_response(GNUNET_REST_COPYING_TEXT);
116 handle->proc (handle->proc_cls, 113 handle->proc(handle->proc_cls,
117 resp, 114 resp,
118 MHD_HTTP_OK); 115 MHD_HTTP_OK);
119 cleanup_handle (handle); 116 cleanup_handle(handle);
120} 117}
121 118
122 119
@@ -127,21 +124,21 @@ get_cont (struct GNUNET_REST_RequestHandle *con_handle,
127 * @param handle the lookup handle 124 * @param handle the lookup handle
128 */ 125 */
129static void 126static void
130options_cont (struct GNUNET_REST_RequestHandle *con_handle, 127options_cont(struct GNUNET_REST_RequestHandle *con_handle,
131 const char* url, 128 const char* url,
132 void *cls) 129 void *cls)
133{ 130{
134 struct MHD_Response *resp; 131 struct MHD_Response *resp;
135 struct RequestHandle *handle = cls; 132 struct RequestHandle *handle = cls;
136 133
137 resp = GNUNET_REST_create_response (NULL); 134 resp = GNUNET_REST_create_response(NULL);
138 MHD_add_response_header (resp, 135 MHD_add_response_header(resp,
139 "Access-Control-Allow-Methods", 136 "Access-Control-Allow-Methods",
140 MHD_HTTP_METHOD_GET); 137 MHD_HTTP_METHOD_GET);
141 handle->proc (handle->proc_cls, 138 handle->proc(handle->proc_cls,
142 resp, 139 resp,
143 MHD_HTTP_OK); 140 MHD_HTTP_OK);
144 cleanup_handle (handle); 141 cleanup_handle(handle);
145} 142}
146 143
147 144
@@ -157,30 +154,30 @@ options_cont (struct GNUNET_REST_RequestHandle *con_handle,
157 * @return #GNUNET_OK if request accepted 154 * @return #GNUNET_OK if request accepted
158 */ 155 */
159static void 156static void
160rest_copying_process_request (struct GNUNET_REST_RequestHandle *conndata_handle, 157rest_copying_process_request(struct GNUNET_REST_RequestHandle *conndata_handle,
161 GNUNET_REST_ResultProcessor proc, 158 GNUNET_REST_ResultProcessor proc,
162 void *proc_cls) 159 void *proc_cls)
163{ 160{
164 static const struct GNUNET_REST_RequestHandler handlers[] = { 161 static const struct GNUNET_REST_RequestHandler handlers[] = {
165 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_COPYING, &get_cont}, 162 { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_COPYING, &get_cont },
166 {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_COPYING, &options_cont}, 163 { MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_COPYING, &options_cont },
167 GNUNET_REST_HANDLER_END 164 GNUNET_REST_HANDLER_END
168 }; 165 };
169 struct RequestHandle *handle = GNUNET_new (struct RequestHandle); 166 struct RequestHandle *handle = GNUNET_new(struct RequestHandle);
170 struct GNUNET_REST_RequestHandlerError err; 167 struct GNUNET_REST_RequestHandlerError err;
171 168
172 handle->proc_cls = proc_cls; 169 handle->proc_cls = proc_cls;
173 handle->proc = proc; 170 handle->proc = proc;
174 handle->rest_handle = conndata_handle; 171 handle->rest_handle = conndata_handle;
175 172
176 if (GNUNET_NO == GNUNET_REST_handle_request (conndata_handle, 173 if (GNUNET_NO == GNUNET_REST_handle_request(conndata_handle,
177 handlers, 174 handlers,
178 &err, 175 &err,
179 handle)) 176 handle))
180 { 177 {
181 handle->response_code = err.error_code; 178 handle->response_code = err.error_code;
182 GNUNET_SCHEDULER_add_now (&do_error, handle); 179 GNUNET_SCHEDULER_add_now(&do_error, handle);
183 } 180 }
184} 181}
185 182
186 183
@@ -191,22 +188,23 @@ rest_copying_process_request (struct GNUNET_REST_RequestHandle *conndata_handle,
191 * @return NULL on error, otherwise the plugin context 188 * @return NULL on error, otherwise the plugin context
192 */ 189 */
193void * 190void *
194libgnunet_plugin_rest_copying_init (void *cls) 191libgnunet_plugin_rest_copying_init(void *cls)
195{ 192{
196 static struct Plugin plugin; 193 static struct Plugin plugin;
194
197 cfg = cls; 195 cfg = cls;
198 struct GNUNET_REST_Plugin *api; 196 struct GNUNET_REST_Plugin *api;
199 197
200 if (NULL != plugin.cfg) 198 if (NULL != plugin.cfg)
201 return NULL; /* can only initialize once! */ 199 return NULL; /* can only initialize once! */
202 memset (&plugin, 0, sizeof (struct Plugin)); 200 memset(&plugin, 0, sizeof(struct Plugin));
203 plugin.cfg = cfg; 201 plugin.cfg = cfg;
204 api = GNUNET_new (struct GNUNET_REST_Plugin); 202 api = GNUNET_new(struct GNUNET_REST_Plugin);
205 api->cls = &plugin; 203 api->cls = &plugin;
206 api->name = GNUNET_REST_API_NS_COPYING; 204 api->name = GNUNET_REST_API_NS_COPYING;
207 api->process_request = &rest_copying_process_request; 205 api->process_request = &rest_copying_process_request;
208 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 206 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
209 _("COPYING REST API initialized\n")); 207 _("COPYING REST API initialized\n"));
210 return api; 208 return api;
211} 209}
212 210
@@ -218,15 +216,15 @@ libgnunet_plugin_rest_copying_init (void *cls)
218 * @return always NULL 216 * @return always NULL
219 */ 217 */
220void * 218void *
221libgnunet_plugin_rest_copying_done (void *cls) 219libgnunet_plugin_rest_copying_done(void *cls)
222{ 220{
223 struct GNUNET_REST_Plugin *api = cls; 221 struct GNUNET_REST_Plugin *api = cls;
224 struct Plugin *plugin = api->cls; 222 struct Plugin *plugin = api->cls;
225 223
226 plugin->cfg = NULL; 224 plugin->cfg = NULL;
227 GNUNET_free (api); 225 GNUNET_free(api);
228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 226 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
229 "COPYING REST plugin is finished\n"); 227 "COPYING REST plugin is finished\n");
230 return NULL; 228 return NULL;
231} 229}
232 230
diff --git a/src/rest/rest.c b/src/rest/rest.c
index b0c2da6c2..44af5f2e0 100644
--- a/src/rest/rest.c
+++ b/src/rest/rest.c
@@ -11,7 +11,7 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
@@ -33,17 +33,17 @@
33 * REST Utilities 33 * REST Utilities
34 */ 34 */
35 35
36 /** 36/**
37 * Check if namespace is in URL. 37 * Check if namespace is in URL.
38 * 38 *
39 * @param url URL to check 39 * @param url URL to check
40 * @param namespace namespace to check against 40 * @param namespace namespace to check against
41 * @retun GNUNET_YES if namespace matches 41 * @retun GNUNET_YES if namespace matches
42 */ 42 */
43int 43int
44GNUNET_REST_namespace_match (const char *url, const char *namespace) 44GNUNET_REST_namespace_match(const char *url, const char *namespace)
45{ 45{
46 return 0 == strncmp (namespace, url, strlen (namespace)); 46 return 0 == strncmp(namespace, url, strlen(namespace));
47} 47}
48 48
49/** 49/**
@@ -53,30 +53,29 @@ GNUNET_REST_namespace_match (const char *url, const char *namespace)
53 * @retun MHD response 53 * @retun MHD response
54 */ 54 */
55struct MHD_Response* 55struct MHD_Response*
56GNUNET_REST_create_response (const char *data) 56GNUNET_REST_create_response(const char *data)
57{ 57{
58 struct MHD_Response *resp; 58 struct MHD_Response *resp;
59 size_t len; 59 size_t len;
60 60
61 if (NULL == data) 61 if (NULL == data)
62 { 62 {
63 len = 0; 63 len = 0;
64 data = ""; 64 data = "";
65 } 65 }
66 else 66 else
67 len = strlen (data); 67 len = strlen(data);
68 resp = MHD_create_response_from_buffer (len, 68 resp = MHD_create_response_from_buffer(len,
69 (void*)data, 69 (void*)data,
70 MHD_RESPMEM_MUST_COPY); 70 MHD_RESPMEM_MUST_COPY);
71 return resp; 71 return resp;
72
73} 72}
74 73
75int 74int
76GNUNET_REST_handle_request (struct GNUNET_REST_RequestHandle *conn, 75GNUNET_REST_handle_request(struct GNUNET_REST_RequestHandle *conn,
77 const struct GNUNET_REST_RequestHandler *handlers, 76 const struct GNUNET_REST_RequestHandler *handlers,
78 struct GNUNET_REST_RequestHandlerError *err, 77 struct GNUNET_REST_RequestHandlerError *err,
79 void *cls) 78 void *cls)
80{ 79{
81 int count; 80 int count;
82 int i; 81 int i;
@@ -86,23 +85,23 @@ GNUNET_REST_handle_request (struct GNUNET_REST_RequestHandle *conn,
86 while (NULL != handlers[count].method) 85 while (NULL != handlers[count].method)
87 count++; 86 count++;
88 87
89 GNUNET_asprintf (&url, "%s", conn->url); 88 GNUNET_asprintf(&url, "%s", conn->url);
90 if (url[strlen (url)-1] == '/') 89 if (url[strlen(url) - 1] == '/')
91 url[strlen (url)-1] = '\0'; 90 url[strlen(url) - 1] = '\0';
92 for (i = 0; i < count; i++) 91 for (i = 0; i < count; i++)
93 { 92 {
94 if (0 != strcasecmp (conn->method, handlers[i].method)) 93 if (0 != strcasecmp(conn->method, handlers[i].method))
95 continue; 94 continue;
96 if (strlen (url) < strlen (handlers[i].namespace)) 95 if (strlen(url) < strlen(handlers[i].namespace))
97 continue; 96 continue;
98 if (GNUNET_NO == GNUNET_REST_namespace_match (url, handlers[i].namespace)) 97 if (GNUNET_NO == GNUNET_REST_namespace_match(url, handlers[i].namespace))
99 continue; 98 continue;
100 //Match 99 //Match
101 handlers[i].proc (conn, (const char*)url, cls); 100 handlers[i].proc(conn, (const char*)url, cls);
102 GNUNET_free (url); 101 GNUNET_free(url);
103 return GNUNET_YES; 102 return GNUNET_YES;
104 } 103 }
105 GNUNET_free (url); 104 GNUNET_free(url);
106 err->error_code = MHD_HTTP_BAD_REQUEST; 105 err->error_code = MHD_HTTP_BAD_REQUEST;
107 return GNUNET_NO; 106 return GNUNET_NO;
108} 107}