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