diff options
Diffstat (limited to 'src/rest/gnunet-rest-server.c')
-rw-r--r-- | src/rest/gnunet-rest-server.c | 1067 |
1 files changed, 534 insertions, 533 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 | */ |
145 | struct MhdConnectionHandle | 145 | struct 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 | */ |
170 | static void | 169 | static void |
171 | do_httpd (void *cls); | 170 | do_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 | */ |
177 | static void | 176 | static void |
178 | run_mhd_now () | 177 | run_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 | */ |
196 | static void | 195 | static void |
197 | plugin_callback (void *cls, struct MHD_Response *resp, int status) | 196 | plugin_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 | ||
207 | static int | 207 | static int |
208 | cleanup_url_map (void *cls, const struct GNUNET_HashCode *key, void *value) | 208 | cleanup_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 | ||
215 | static void | 215 | static void |
216 | cleanup_handle (struct MhdConnectionHandle *handle) | 216 | cleanup_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 | ||
245 | static int | 244 | static int |
246 | header_iterator (void *cls, | 245 | header_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 | ||
276 | static int | 275 | static int |
277 | url_iterator (void *cls, | 276 | url_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 | ||
302 | static int | 301 | static int |
303 | post_data_iter (void *cls, | 302 | post_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 | */ |
362 | static int | 361 | static int |
363 | create_response (void *cls, | 362 | create_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 | */ |
525 | static void | 524 | static void |
526 | mhd_completed_cb (void *cls, | 525 | mhd_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 | */ |
541 | static void | 540 | static void |
542 | kill_httpd () | 541 | kill_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 | */ |
585 | static void | 584 | static void |
586 | schedule_httpd () | 585 | schedule_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 | */ |
649 | static void | 648 | static void |
650 | do_httpd (void *cls) | 649 | do_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 | */ |
664 | static void | 663 | static void |
665 | do_accept (void *cls) | 664 | do_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 | */ |
717 | static void | 716 | static void |
718 | do_shutdown (void *cls) | 717 | do_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 | */ |
732 | static struct GNUNET_NETWORK_Handle * | 731 | static struct GNUNET_NETWORK_Handle * |
733 | bind_v4 () | 732 | bind_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 | */ |
767 | static struct GNUNET_NETWORK_Handle * | 766 | static struct GNUNET_NETWORK_Handle * |
768 | bind_v6 () | 767 | bind_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 | */ |
804 | static void | 803 | static void |
805 | load_plugin (void *cls, const char *libname, void *lib_ret) | 804 | load_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 | */ |
842 | static void | 842 | static void |
843 | run (void *cls, | 843 | run(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 | */ |
1023 | int | 1024 | int |
1024 | main (int argc, char *const *argv) | 1025 | main(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 | ||