diff options
Diffstat (limited to 'src/curl')
-rw-r--r-- | src/curl/Makefile.am | 48 | ||||
-rw-r--r-- | src/curl/curl.c | 862 | ||||
-rw-r--r-- | src/curl/curl_reschedule.c | 181 |
3 files changed, 0 insertions, 1091 deletions
diff --git a/src/curl/Makefile.am b/src/curl/Makefile.am deleted file mode 100644 index c6b350bc7..000000000 --- a/src/curl/Makefile.am +++ /dev/null | |||
@@ -1,48 +0,0 @@ | |||
1 | # This Makefile.am is in the public domain | ||
2 | AM_CPPFLAGS = -I$(top_srcdir)/src/include | ||
3 | |||
4 | if USE_COVERAGE | ||
5 | AM_CFLAGS = --coverage -O0 | ||
6 | XLIB = -lgcov | ||
7 | endif | ||
8 | |||
9 | if HAVE_LIBGNURL | ||
10 | LIB_GNURL=@LIBGNURL@ | ||
11 | CPP_GNURL=@LIBGNURL_CPPFLAGS@ | ||
12 | else | ||
13 | if HAVE_LIBCURL | ||
14 | LIB_GNURL=@LIBCURL@ | ||
15 | CPP_GNURL=@LIBCURL_CPPFLAGS@ | ||
16 | endif | ||
17 | endif | ||
18 | |||
19 | lib_LTLIBRARIES = \ | ||
20 | libgnunetcurl.la | ||
21 | |||
22 | libgnunetcurl_la_LDFLAGS = \ | ||
23 | $(GN_LIBINTL) \ | ||
24 | -version-info 0:0:0 \ | ||
25 | -no-undefined | ||
26 | libgnunetcurl_la_SOURCES = \ | ||
27 | curl.c \ | ||
28 | curl_reschedule.c | ||
29 | libgnunetcurl_la_LIBADD = \ | ||
30 | $(top_builddir)/src/util/libgnunetutil.la \ | ||
31 | -ljansson \ | ||
32 | $(LIB_GNURL) \ | ||
33 | $(XLIB) | ||
34 | libgnunetcurl_la_CPPFLAGS = \ | ||
35 | $(CPP_GNURL) $(AM_CPPFLAGS) $(MHD_CFLAGS) | ||
36 | |||
37 | #check_PROGRAMS = \ | ||
38 | # test_curl | ||
39 | |||
40 | #TESTS = \ | ||
41 | # $(check_PROGRAMS) | ||
42 | |||
43 | #test_curl_SOURCES = \ | ||
44 | # test_curl.c | ||
45 | #test_curl_LDADD = \ | ||
46 | # libgnunetcurl.la \ | ||
47 | # $(top_builddir)/src/util/libgnunetutil.la \ | ||
48 | # -ljansson -lcurl | ||
diff --git a/src/curl/curl.c b/src/curl/curl.c deleted file mode 100644 index e45612e94..000000000 --- a/src/curl/curl.c +++ /dev/null | |||
@@ -1,862 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2014, 2015, 2016, 2018 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /** | ||
21 | * @file curl/curl.c | ||
22 | * @brief API for downloading JSON via CURL | ||
23 | * @author Sree Harsha Totakura <sreeharsha@totakura.in> | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include <jansson.h> | ||
28 | #include <microhttpd.h> | ||
29 | #include "gnunet_curl_lib.h" | ||
30 | |||
31 | #if ENABLE_BENCHMARK | ||
32 | #include "../util/benchmark.h" | ||
33 | #endif | ||
34 | |||
35 | |||
36 | /** | ||
37 | * Log error related to CURL operations. | ||
38 | * | ||
39 | * @param type log level | ||
40 | * @param function which function failed to run | ||
41 | * @param code what was the curl error code | ||
42 | */ | ||
43 | #define CURL_STRERROR(type, function, code) \ | ||
44 | GNUNET_log (type, \ | ||
45 | "Curl function `%s' has failed at `%s:%d' with error: %s\n", \ | ||
46 | function, \ | ||
47 | __FILE__, \ | ||
48 | __LINE__, \ | ||
49 | curl_easy_strerror (code)); | ||
50 | |||
51 | /** | ||
52 | * Print JSON parsing related error information | ||
53 | */ | ||
54 | #define JSON_WARN(error) \ | ||
55 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, \ | ||
56 | "JSON parsing failed at %s:%u: %s (%s)\n", \ | ||
57 | __FILE__, \ | ||
58 | __LINE__, \ | ||
59 | error.text, \ | ||
60 | error.source) | ||
61 | |||
62 | |||
63 | /** | ||
64 | * Failsafe flag. Raised if our constructor fails to initialize | ||
65 | * the Curl library. | ||
66 | */ | ||
67 | static int curl_fail; | ||
68 | |||
69 | /** | ||
70 | * Jobs are CURL requests running within a `struct GNUNET_CURL_Context`. | ||
71 | */ | ||
72 | struct GNUNET_CURL_Job | ||
73 | { | ||
74 | /** | ||
75 | * We keep jobs in a DLL. | ||
76 | */ | ||
77 | struct GNUNET_CURL_Job *next; | ||
78 | |||
79 | /** | ||
80 | * We keep jobs in a DLL. | ||
81 | */ | ||
82 | struct GNUNET_CURL_Job *prev; | ||
83 | |||
84 | /** | ||
85 | * Easy handle of the job. | ||
86 | */ | ||
87 | CURL *easy_handle; | ||
88 | |||
89 | /** | ||
90 | * Context this job runs in. | ||
91 | */ | ||
92 | struct GNUNET_CURL_Context *ctx; | ||
93 | |||
94 | /** | ||
95 | * Function to call upon completion. | ||
96 | */ | ||
97 | GNUNET_CURL_JobCompletionCallback jcc; | ||
98 | |||
99 | /** | ||
100 | * Closure for @e jcc. | ||
101 | */ | ||
102 | void *jcc_cls; | ||
103 | |||
104 | /** | ||
105 | * Function to call upon completion. | ||
106 | */ | ||
107 | GNUNET_CURL_RawJobCompletionCallback jcc_raw; | ||
108 | |||
109 | /** | ||
110 | * Closure for @e jcc_raw. | ||
111 | */ | ||
112 | void *jcc_raw_cls; | ||
113 | |||
114 | /** | ||
115 | * Buffer for response received from CURL. | ||
116 | */ | ||
117 | struct GNUNET_CURL_DownloadBuffer db; | ||
118 | |||
119 | /** | ||
120 | * Headers used for this job, the list needs to be freed | ||
121 | * after the job has finished. | ||
122 | */ | ||
123 | struct curl_slist *job_headers; | ||
124 | }; | ||
125 | |||
126 | |||
127 | /** | ||
128 | * Context | ||
129 | */ | ||
130 | struct GNUNET_CURL_Context | ||
131 | { | ||
132 | /** | ||
133 | * Curl multi handle | ||
134 | */ | ||
135 | CURLM *multi; | ||
136 | |||
137 | /** | ||
138 | * Curl share handle | ||
139 | */ | ||
140 | CURLSH *share; | ||
141 | |||
142 | /** | ||
143 | * We keep jobs in a DLL. | ||
144 | */ | ||
145 | struct GNUNET_CURL_Job *jobs_head; | ||
146 | |||
147 | /** | ||
148 | * We keep jobs in a DLL. | ||
149 | */ | ||
150 | struct GNUNET_CURL_Job *jobs_tail; | ||
151 | |||
152 | /** | ||
153 | * Headers common for all requests in the context. | ||
154 | */ | ||
155 | struct curl_slist *common_headers; | ||
156 | |||
157 | /** | ||
158 | * If non-NULL, the async scope ID is sent in a request | ||
159 | * header of this name. | ||
160 | */ | ||
161 | const char *async_scope_id_header; | ||
162 | |||
163 | /** | ||
164 | * Function we need to call whenever the event loop's | ||
165 | * socket set changed. | ||
166 | */ | ||
167 | GNUNET_CURL_RescheduleCallback cb; | ||
168 | |||
169 | /** | ||
170 | * Closure for @e cb. | ||
171 | */ | ||
172 | void *cb_cls; | ||
173 | |||
174 | /** | ||
175 | * USERNAME:PASSWORD to use for client-authentication | ||
176 | * with all requests of this context, or NULL. | ||
177 | */ | ||
178 | char *userpass; | ||
179 | |||
180 | /** | ||
181 | * Type of the TLS client certificate used, or NULL. | ||
182 | */ | ||
183 | char *certtype; | ||
184 | |||
185 | /** | ||
186 | * File with the TLS client certificate, or NULL. | ||
187 | */ | ||
188 | char *certfile; | ||
189 | |||
190 | /** | ||
191 | * File with the private key to authenticate the | ||
192 | * TLS client, or NULL. | ||
193 | */ | ||
194 | char *keyfile; | ||
195 | |||
196 | /** | ||
197 | * Passphrase to decrypt @e keyfile, or NULL. | ||
198 | */ | ||
199 | char *keypass; | ||
200 | |||
201 | }; | ||
202 | |||
203 | |||
204 | void | ||
205 | GNUNET_CURL_set_userpass (struct GNUNET_CURL_Context *ctx, | ||
206 | const char *userpass) | ||
207 | { | ||
208 | GNUNET_free (ctx->userpass); | ||
209 | if (NULL != userpass) | ||
210 | ctx->userpass = GNUNET_strdup (userpass); | ||
211 | } | ||
212 | |||
213 | |||
214 | void | ||
215 | GNUNET_CURL_set_tlscert (struct GNUNET_CURL_Context *ctx, | ||
216 | const char *certtype, | ||
217 | const char *certfile, | ||
218 | const char *keyfile, | ||
219 | const char *keypass) | ||
220 | { | ||
221 | GNUNET_free (ctx->certtype); | ||
222 | GNUNET_free (ctx->certfile); | ||
223 | GNUNET_free (ctx->keyfile); | ||
224 | GNUNET_free (ctx->keypass); | ||
225 | if (NULL != certtype) | ||
226 | ctx->certtype = GNUNET_strdup (certtype); | ||
227 | if (NULL != certfile) | ||
228 | ctx->certfile = GNUNET_strdup (certfile); | ||
229 | if (NULL != keyfile) | ||
230 | ctx->certtype = GNUNET_strdup (keyfile); | ||
231 | if (NULL != keypass) | ||
232 | ctx->certtype = GNUNET_strdup (keypass); | ||
233 | } | ||
234 | |||
235 | |||
236 | struct GNUNET_CURL_Context * | ||
237 | GNUNET_CURL_init (GNUNET_CURL_RescheduleCallback cb, | ||
238 | void *cb_cls) | ||
239 | { | ||
240 | struct GNUNET_CURL_Context *ctx; | ||
241 | CURLM *multi; | ||
242 | CURLSH *share; | ||
243 | |||
244 | if (curl_fail) | ||
245 | { | ||
246 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
247 | "Curl was not initialised properly\n"); | ||
248 | return NULL; | ||
249 | } | ||
250 | if (NULL == (multi = curl_multi_init ())) | ||
251 | { | ||
252 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
253 | "Failed to create a Curl multi handle\n"); | ||
254 | return NULL; | ||
255 | } | ||
256 | if (NULL == (share = curl_share_init ())) | ||
257 | { | ||
258 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
259 | "Failed to create a Curl share handle\n"); | ||
260 | return NULL; | ||
261 | } | ||
262 | ctx = GNUNET_new (struct GNUNET_CURL_Context); | ||
263 | ctx->cb = cb; | ||
264 | ctx->cb_cls = cb_cls; | ||
265 | ctx->multi = multi; | ||
266 | ctx->share = share; | ||
267 | return ctx; | ||
268 | } | ||
269 | |||
270 | |||
271 | void | ||
272 | GNUNET_CURL_enable_async_scope_header (struct GNUNET_CURL_Context *ctx, | ||
273 | const char *header_name) | ||
274 | { | ||
275 | ctx->async_scope_id_header = header_name; | ||
276 | } | ||
277 | |||
278 | |||
279 | int | ||
280 | GNUNET_CURL_is_valid_scope_id (const char *scope_id) | ||
281 | { | ||
282 | if (strlen (scope_id) >= 64) | ||
283 | return GNUNET_NO; | ||
284 | for (size_t i = 0; i < strlen (scope_id); i++) | ||
285 | if (! (isalnum (scope_id[i]) || (scope_id[i] == '-'))) | ||
286 | return GNUNET_NO; | ||
287 | return GNUNET_YES; | ||
288 | } | ||
289 | |||
290 | |||
291 | /** | ||
292 | * Callback used when downloading the reply to an HTTP request. | ||
293 | * Just appends all of the data to the `buf` in the | ||
294 | * `struct DownloadBuffer` for further processing. The size of | ||
295 | * the download is limited to #GNUNET_MAX_MALLOC_CHECKED, if | ||
296 | * the download exceeds this size, we abort with an error. | ||
297 | * | ||
298 | * @param bufptr data downloaded via HTTP | ||
299 | * @param size size of an item in @a bufptr | ||
300 | * @param nitems number of items in @a bufptr | ||
301 | * @param cls the `struct DownloadBuffer` | ||
302 | * @return number of bytes processed from @a bufptr | ||
303 | */ | ||
304 | static size_t | ||
305 | download_cb (char *bufptr, | ||
306 | size_t size, | ||
307 | size_t nitems, | ||
308 | void *cls) | ||
309 | { | ||
310 | struct GNUNET_CURL_DownloadBuffer *db = cls; | ||
311 | size_t msize; | ||
312 | void *buf; | ||
313 | |||
314 | if (0 == size * nitems) | ||
315 | { | ||
316 | /* Nothing (left) to do */ | ||
317 | return 0; | ||
318 | } | ||
319 | msize = size * nitems; | ||
320 | if ((msize + db->buf_size) >= GNUNET_MAX_MALLOC_CHECKED) | ||
321 | { | ||
322 | db->eno = ENOMEM; | ||
323 | return 0; /* signals an error to curl */ | ||
324 | } | ||
325 | db->buf = GNUNET_realloc (db->buf, | ||
326 | db->buf_size + msize); | ||
327 | buf = db->buf + db->buf_size; | ||
328 | GNUNET_memcpy (buf, bufptr, msize); | ||
329 | db->buf_size += msize; | ||
330 | return msize; | ||
331 | } | ||
332 | |||
333 | |||
334 | /** | ||
335 | * Create the HTTP headers for the request | ||
336 | * | ||
337 | * @param ctx context we run in | ||
338 | * @param job_headers job-specific headers | ||
339 | * @return all headers to use | ||
340 | */ | ||
341 | static struct curl_slist * | ||
342 | setup_job_headers (struct GNUNET_CURL_Context *ctx, | ||
343 | const struct curl_slist *job_headers) | ||
344 | { | ||
345 | struct curl_slist *all_headers = NULL; | ||
346 | |||
347 | for (const struct curl_slist *curr = job_headers; | ||
348 | NULL != curr; | ||
349 | curr = curr->next) | ||
350 | { | ||
351 | GNUNET_assert (NULL != | ||
352 | (all_headers = curl_slist_append (all_headers, | ||
353 | curr->data))); | ||
354 | } | ||
355 | |||
356 | for (const struct curl_slist *curr = ctx->common_headers; | ||
357 | NULL != curr; | ||
358 | curr = curr->next) | ||
359 | { | ||
360 | GNUNET_assert (NULL != | ||
361 | (all_headers = curl_slist_append (all_headers, | ||
362 | curr->data))); | ||
363 | } | ||
364 | |||
365 | if (NULL != ctx->async_scope_id_header) | ||
366 | { | ||
367 | struct GNUNET_AsyncScopeSave scope; | ||
368 | |||
369 | GNUNET_async_scope_get (&scope); | ||
370 | if (GNUNET_YES == scope.have_scope) | ||
371 | { | ||
372 | char *aid_header; | ||
373 | |||
374 | aid_header = | ||
375 | GNUNET_STRINGS_data_to_string_alloc ( | ||
376 | &scope.scope_id, | ||
377 | sizeof(struct GNUNET_AsyncScopeId)); | ||
378 | GNUNET_assert (NULL != aid_header); | ||
379 | GNUNET_assert (NULL != curl_slist_append (all_headers, | ||
380 | aid_header)); | ||
381 | GNUNET_free (aid_header); | ||
382 | } | ||
383 | } | ||
384 | return all_headers; | ||
385 | } | ||
386 | |||
387 | |||
388 | /** | ||
389 | * Create a job. | ||
390 | * | ||
391 | * @param eh easy handle to use | ||
392 | * @param ctx context to run the job in | ||
393 | * @param all_headers HTTP client headers to use (free'd) | ||
394 | * @return NULL on error | ||
395 | */ | ||
396 | static struct GNUNET_CURL_Job * | ||
397 | setup_job (CURL *eh, | ||
398 | struct GNUNET_CURL_Context *ctx, | ||
399 | struct curl_slist *all_headers) | ||
400 | { | ||
401 | struct GNUNET_CURL_Job *job; | ||
402 | |||
403 | if (CURLE_OK != | ||
404 | curl_easy_setopt (eh, | ||
405 | CURLOPT_HTTPHEADER, | ||
406 | all_headers)) | ||
407 | { | ||
408 | GNUNET_break (0); | ||
409 | curl_slist_free_all (all_headers); | ||
410 | curl_easy_cleanup (eh); | ||
411 | return NULL; | ||
412 | } | ||
413 | job = GNUNET_new (struct GNUNET_CURL_Job); | ||
414 | job->job_headers = all_headers; | ||
415 | |||
416 | if ( (CURLE_OK != | ||
417 | curl_easy_setopt (eh, | ||
418 | CURLOPT_PRIVATE, | ||
419 | job)) || | ||
420 | (CURLE_OK != | ||
421 | curl_easy_setopt (eh, | ||
422 | CURLOPT_WRITEFUNCTION, | ||
423 | &download_cb)) || | ||
424 | (CURLE_OK != | ||
425 | curl_easy_setopt (eh, | ||
426 | CURLOPT_WRITEDATA, | ||
427 | &job->db)) || | ||
428 | (CURLE_OK != | ||
429 | curl_easy_setopt (eh, | ||
430 | CURLOPT_SHARE, | ||
431 | ctx->share)) || | ||
432 | (CURLM_OK != | ||
433 | curl_multi_add_handle (ctx->multi, | ||
434 | eh)) ) | ||
435 | { | ||
436 | GNUNET_break (0); | ||
437 | GNUNET_free (job); | ||
438 | curl_easy_cleanup (eh); | ||
439 | return NULL; | ||
440 | } | ||
441 | job->easy_handle = eh; | ||
442 | job->ctx = ctx; | ||
443 | GNUNET_CONTAINER_DLL_insert (ctx->jobs_head, | ||
444 | ctx->jobs_tail, | ||
445 | job); | ||
446 | return job; | ||
447 | } | ||
448 | |||
449 | |||
450 | void | ||
451 | GNUNET_CURL_extend_headers (struct GNUNET_CURL_Job *job, | ||
452 | const struct curl_slist *extra_headers) | ||
453 | { | ||
454 | struct curl_slist *all_headers = job->job_headers; | ||
455 | |||
456 | for (const struct curl_slist *curr = extra_headers; | ||
457 | NULL != curr; | ||
458 | curr = curr->next) | ||
459 | { | ||
460 | GNUNET_assert (NULL != | ||
461 | (all_headers = curl_slist_append (all_headers, | ||
462 | curr->data))); | ||
463 | } | ||
464 | job->job_headers = all_headers; | ||
465 | } | ||
466 | |||
467 | |||
468 | struct GNUNET_CURL_Job * | ||
469 | GNUNET_CURL_job_add_raw (struct GNUNET_CURL_Context *ctx, | ||
470 | CURL *eh, | ||
471 | const struct curl_slist *job_headers, | ||
472 | GNUNET_CURL_RawJobCompletionCallback jcc, | ||
473 | void *jcc_cls) | ||
474 | { | ||
475 | struct GNUNET_CURL_Job *job; | ||
476 | struct curl_slist *all_headers; | ||
477 | |||
478 | GNUNET_assert (NULL != jcc); | ||
479 | all_headers = setup_job_headers (ctx, | ||
480 | job_headers); | ||
481 | if (NULL == (job = setup_job (eh, | ||
482 | ctx, | ||
483 | all_headers))) | ||
484 | return NULL; | ||
485 | job->jcc_raw = jcc; | ||
486 | job->jcc_raw_cls = jcc_cls; | ||
487 | ctx->cb (ctx->cb_cls); | ||
488 | return job; | ||
489 | } | ||
490 | |||
491 | |||
492 | struct GNUNET_CURL_Job * | ||
493 | GNUNET_CURL_job_add2 (struct GNUNET_CURL_Context *ctx, | ||
494 | CURL *eh, | ||
495 | const struct curl_slist *job_headers, | ||
496 | GNUNET_CURL_JobCompletionCallback jcc, | ||
497 | void *jcc_cls) | ||
498 | { | ||
499 | struct GNUNET_CURL_Job *job; | ||
500 | struct curl_slist *all_headers; | ||
501 | |||
502 | GNUNET_assert (NULL != jcc); | ||
503 | if ( (NULL != ctx->userpass) && | ||
504 | (0 != curl_easy_setopt (eh, | ||
505 | CURLOPT_USERPWD, | ||
506 | ctx->userpass)) ) | ||
507 | return NULL; | ||
508 | if ( (NULL != ctx->certfile) && | ||
509 | (0 != curl_easy_setopt (eh, | ||
510 | CURLOPT_SSLCERT, | ||
511 | ctx->certfile)) ) | ||
512 | return NULL; | ||
513 | if ( (NULL != ctx->certtype) && | ||
514 | (0 != curl_easy_setopt (eh, | ||
515 | CURLOPT_SSLCERTTYPE, | ||
516 | ctx->certtype)) ) | ||
517 | return NULL; | ||
518 | if ( (NULL != ctx->keyfile) && | ||
519 | (0 != curl_easy_setopt (eh, | ||
520 | CURLOPT_SSLKEY, | ||
521 | ctx->keyfile)) ) | ||
522 | return NULL; | ||
523 | if ( (NULL != ctx->keypass) && | ||
524 | (0 != curl_easy_setopt (eh, | ||
525 | CURLOPT_KEYPASSWD, | ||
526 | ctx->keypass)) ) | ||
527 | return NULL; | ||
528 | |||
529 | all_headers = setup_job_headers (ctx, | ||
530 | job_headers); | ||
531 | if (NULL == (job = setup_job (eh, | ||
532 | ctx, | ||
533 | all_headers))) | ||
534 | return NULL; | ||
535 | |||
536 | job->jcc = jcc; | ||
537 | job->jcc_cls = jcc_cls; | ||
538 | ctx->cb (ctx->cb_cls); | ||
539 | return job; | ||
540 | } | ||
541 | |||
542 | |||
543 | struct GNUNET_CURL_Job * | ||
544 | GNUNET_CURL_job_add_with_ct_json (struct GNUNET_CURL_Context *ctx, | ||
545 | CURL *eh, | ||
546 | GNUNET_CURL_JobCompletionCallback jcc, | ||
547 | void *jcc_cls) | ||
548 | { | ||
549 | struct GNUNET_CURL_Job *job; | ||
550 | struct curl_slist *job_headers = NULL; | ||
551 | |||
552 | GNUNET_assert (NULL != (job_headers = | ||
553 | curl_slist_append (NULL, | ||
554 | "Content-Type: application/json"))); | ||
555 | job = GNUNET_CURL_job_add2 (ctx, | ||
556 | eh, | ||
557 | job_headers, | ||
558 | jcc, | ||
559 | jcc_cls); | ||
560 | curl_slist_free_all (job_headers); | ||
561 | return job; | ||
562 | } | ||
563 | |||
564 | |||
565 | struct GNUNET_CURL_Job * | ||
566 | GNUNET_CURL_job_add (struct GNUNET_CURL_Context *ctx, | ||
567 | CURL *eh, | ||
568 | GNUNET_CURL_JobCompletionCallback jcc, | ||
569 | void *jcc_cls) | ||
570 | { | ||
571 | return GNUNET_CURL_job_add2 (ctx, | ||
572 | eh, | ||
573 | NULL, | ||
574 | jcc, | ||
575 | jcc_cls); | ||
576 | } | ||
577 | |||
578 | |||
579 | void | ||
580 | GNUNET_CURL_job_cancel (struct GNUNET_CURL_Job *job) | ||
581 | { | ||
582 | struct GNUNET_CURL_Context *ctx = job->ctx; | ||
583 | |||
584 | GNUNET_CONTAINER_DLL_remove (ctx->jobs_head, | ||
585 | ctx->jobs_tail, | ||
586 | job); | ||
587 | GNUNET_break (CURLM_OK == | ||
588 | curl_multi_remove_handle (ctx->multi, | ||
589 | job->easy_handle)); | ||
590 | curl_easy_cleanup (job->easy_handle); | ||
591 | GNUNET_free (job->db.buf); | ||
592 | curl_slist_free_all (job->job_headers); | ||
593 | ctx->cb (ctx->cb_cls); | ||
594 | GNUNET_free (job); | ||
595 | } | ||
596 | |||
597 | |||
598 | /** | ||
599 | * Test if the given content type @a ct is JSON | ||
600 | * | ||
601 | * @param ct a content type, e.g. "application/json; charset=UTF-8" | ||
602 | * @return true if @a ct denotes JSON | ||
603 | */ | ||
604 | static bool | ||
605 | is_json (const char *ct) | ||
606 | { | ||
607 | const char *semi; | ||
608 | |||
609 | /* check for "application/json" exact match */ | ||
610 | if (0 == strcasecmp (ct, | ||
611 | "application/json")) | ||
612 | return true; | ||
613 | /* check for "application/json;[ANYTHING]" */ | ||
614 | semi = strchr (ct, | ||
615 | ';'); | ||
616 | /* also allow "application/json [ANYTHING]" (note the space!) */ | ||
617 | if (NULL == semi) | ||
618 | semi = strchr (ct, | ||
619 | ' '); | ||
620 | if (NULL == semi) | ||
621 | return false; /* no delimiter we accept, forget it */ | ||
622 | if (semi - ct != strlen ("application/json")) | ||
623 | return false; /* delimiter past desired length, forget it */ | ||
624 | if (0 == strncasecmp (ct, | ||
625 | "application/json", | ||
626 | strlen ("application/json"))) | ||
627 | return true; /* OK */ | ||
628 | return false; | ||
629 | } | ||
630 | |||
631 | |||
632 | void * | ||
633 | GNUNET_CURL_download_get_result_ (struct GNUNET_CURL_DownloadBuffer *db, | ||
634 | CURL *eh, | ||
635 | long *response_code) | ||
636 | { | ||
637 | json_t *json; | ||
638 | json_error_t error; | ||
639 | char *ct; | ||
640 | |||
641 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
642 | "Downloaded body: %.*s\n", | ||
643 | (int) db->buf_size, | ||
644 | (char *) db->buf); | ||
645 | if (CURLE_OK != | ||
646 | curl_easy_getinfo (eh, | ||
647 | CURLINFO_RESPONSE_CODE, | ||
648 | response_code)) | ||
649 | { | ||
650 | /* unexpected error... */ | ||
651 | GNUNET_break (0); | ||
652 | *response_code = 0; | ||
653 | } | ||
654 | if ((CURLE_OK != | ||
655 | curl_easy_getinfo (eh, | ||
656 | CURLINFO_CONTENT_TYPE, | ||
657 | &ct)) || | ||
658 | (NULL == ct) || | ||
659 | (! is_json (ct))) | ||
660 | { | ||
661 | /* No content type or explicitly not JSON, refuse to parse | ||
662 | (but keep response code) */ | ||
663 | if (0 != db->buf_size) | ||
664 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
665 | "Did NOT detect response `%.*s' as JSON\n", | ||
666 | (int) db->buf_size, | ||
667 | (const char *) db->buf); | ||
668 | return NULL; | ||
669 | } | ||
670 | if (MHD_HTTP_NO_CONTENT == *response_code) | ||
671 | return NULL; | ||
672 | if (0 == *response_code) | ||
673 | { | ||
674 | char *url; | ||
675 | |||
676 | if (CURLE_OK != | ||
677 | curl_easy_getinfo (eh, | ||
678 | CURLINFO_EFFECTIVE_URL, | ||
679 | &url)) | ||
680 | url = "<unknown URL>"; | ||
681 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
682 | "Failed to download response from `%s': \n", | ||
683 | url); | ||
684 | return NULL; | ||
685 | } | ||
686 | json = NULL; | ||
687 | if (0 == db->eno) | ||
688 | { | ||
689 | json = json_loadb (db->buf, | ||
690 | db->buf_size, | ||
691 | JSON_REJECT_DUPLICATES | JSON_DISABLE_EOF_CHECK, | ||
692 | &error); | ||
693 | if (NULL == json) | ||
694 | { | ||
695 | JSON_WARN (error); | ||
696 | *response_code = 0; | ||
697 | } | ||
698 | } | ||
699 | GNUNET_free (db->buf); | ||
700 | db->buf = NULL; | ||
701 | db->buf_size = 0; | ||
702 | return json; | ||
703 | } | ||
704 | |||
705 | |||
706 | enum GNUNET_GenericReturnValue | ||
707 | GNUNET_CURL_append_header (struct GNUNET_CURL_Context *ctx, | ||
708 | const char *header) | ||
709 | { | ||
710 | ctx->common_headers = curl_slist_append (ctx->common_headers, | ||
711 | header); | ||
712 | if (NULL == ctx->common_headers) | ||
713 | return GNUNET_SYSERR; | ||
714 | |||
715 | return GNUNET_OK; | ||
716 | } | ||
717 | |||
718 | |||
719 | void | ||
720 | GNUNET_CURL_perform2 (struct GNUNET_CURL_Context *ctx, | ||
721 | GNUNET_CURL_RawParser rp, | ||
722 | GNUNET_CURL_ResponseCleaner rc) | ||
723 | { | ||
724 | CURLMsg *cmsg; | ||
725 | int n_running; | ||
726 | int n_completed; | ||
727 | |||
728 | (void) curl_multi_perform (ctx->multi, | ||
729 | &n_running); | ||
730 | while (NULL != (cmsg = curl_multi_info_read (ctx->multi, | ||
731 | &n_completed))) | ||
732 | { | ||
733 | struct GNUNET_CURL_Job *job; | ||
734 | long response_code; | ||
735 | void *response; | ||
736 | |||
737 | /* Only documented return value is CURLMSG_DONE */ | ||
738 | GNUNET_break (CURLMSG_DONE == cmsg->msg); | ||
739 | GNUNET_assert (CURLE_OK == | ||
740 | curl_easy_getinfo (cmsg->easy_handle, | ||
741 | CURLINFO_PRIVATE, | ||
742 | (char **) &job)); | ||
743 | GNUNET_assert (job->ctx == ctx); | ||
744 | response_code = 0; | ||
745 | if (NULL != job->jcc_raw) | ||
746 | { | ||
747 | /* RAW mode, no parsing */ | ||
748 | GNUNET_break (CURLE_OK == | ||
749 | curl_easy_getinfo (job->easy_handle, | ||
750 | CURLINFO_RESPONSE_CODE, | ||
751 | &response_code)); | ||
752 | job->jcc_raw (job->jcc_raw_cls, | ||
753 | response_code, | ||
754 | job->db.buf, | ||
755 | job->db.buf_size); | ||
756 | } | ||
757 | else | ||
758 | { | ||
759 | /* to be parsed via 'rp' */ | ||
760 | response = rp (&job->db, | ||
761 | job->easy_handle, | ||
762 | &response_code); | ||
763 | job->jcc (job->jcc_cls, | ||
764 | response_code, | ||
765 | response); | ||
766 | rc (response); | ||
767 | } | ||
768 | GNUNET_CURL_job_cancel (job); | ||
769 | } | ||
770 | } | ||
771 | |||
772 | |||
773 | void | ||
774 | GNUNET_CURL_perform (struct GNUNET_CURL_Context *ctx) | ||
775 | { | ||
776 | GNUNET_CURL_perform2 (ctx, | ||
777 | &GNUNET_CURL_download_get_result_, | ||
778 | (GNUNET_CURL_ResponseCleaner) & json_decref); | ||
779 | } | ||
780 | |||
781 | |||
782 | void | ||
783 | GNUNET_CURL_get_select_info (struct GNUNET_CURL_Context *ctx, | ||
784 | fd_set *read_fd_set, | ||
785 | fd_set *write_fd_set, | ||
786 | fd_set *except_fd_set, | ||
787 | int *max_fd, | ||
788 | long *timeout) | ||
789 | { | ||
790 | long to; | ||
791 | int m; | ||
792 | |||
793 | m = -1; | ||
794 | GNUNET_assert (CURLM_OK == | ||
795 | curl_multi_fdset (ctx->multi, | ||
796 | read_fd_set, | ||
797 | write_fd_set, | ||
798 | except_fd_set, | ||
799 | &m)); | ||
800 | to = *timeout; | ||
801 | *max_fd = GNUNET_MAX (m, *max_fd); | ||
802 | GNUNET_assert (CURLM_OK == | ||
803 | curl_multi_timeout (ctx->multi, | ||
804 | &to)); | ||
805 | |||
806 | /* Only if what we got back from curl is smaller than what we | ||
807 | already had (-1 == infinity!), then update timeout */ | ||
808 | if ((to < *timeout) && (-1 != to)) | ||
809 | *timeout = to; | ||
810 | if ((-1 == (*timeout)) && (NULL != ctx->jobs_head)) | ||
811 | *timeout = to; | ||
812 | } | ||
813 | |||
814 | |||
815 | void | ||
816 | GNUNET_CURL_fini (struct GNUNET_CURL_Context *ctx) | ||
817 | { | ||
818 | /* all jobs must have been cancelled at this time, assert this */ | ||
819 | GNUNET_assert (NULL == ctx->jobs_head); | ||
820 | curl_share_cleanup (ctx->share); | ||
821 | curl_multi_cleanup (ctx->multi); | ||
822 | curl_slist_free_all (ctx->common_headers); | ||
823 | GNUNET_free (ctx->userpass); | ||
824 | GNUNET_free (ctx->certtype); | ||
825 | GNUNET_free (ctx->certfile); | ||
826 | GNUNET_free (ctx->keyfile); | ||
827 | GNUNET_free (ctx->keypass); | ||
828 | GNUNET_free (ctx); | ||
829 | } | ||
830 | |||
831 | |||
832 | /** | ||
833 | * Initial global setup logic, specifically runs the Curl setup. | ||
834 | */ | ||
835 | __attribute__ ((constructor)) void | ||
836 | GNUNET_CURL_constructor__ (void) | ||
837 | { | ||
838 | CURLcode ret; | ||
839 | |||
840 | if (CURLE_OK != (ret = curl_global_init (CURL_GLOBAL_DEFAULT))) | ||
841 | { | ||
842 | CURL_STRERROR (GNUNET_ERROR_TYPE_ERROR, | ||
843 | "curl_global_init", | ||
844 | ret); | ||
845 | curl_fail = 1; | ||
846 | } | ||
847 | } | ||
848 | |||
849 | |||
850 | /** | ||
851 | * Cleans up after us, specifically runs the Curl cleanup. | ||
852 | */ | ||
853 | __attribute__ ((destructor)) void | ||
854 | GNUNET_CURL_destructor__ (void) | ||
855 | { | ||
856 | if (curl_fail) | ||
857 | return; | ||
858 | curl_global_cleanup (); | ||
859 | } | ||
860 | |||
861 | |||
862 | /* end of curl.c */ | ||
diff --git a/src/curl/curl_reschedule.c b/src/curl/curl_reschedule.c deleted file mode 100644 index 0c19bd171..000000000 --- a/src/curl/curl_reschedule.c +++ /dev/null | |||
@@ -1,181 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2015, 2016 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /** | ||
21 | * @file curl/curl_reschedule.c | ||
22 | * @brief API for event loop integration with GNUnet SCHEDULER. | ||
23 | * @author Christian Grothoff | ||
24 | */ | ||
25 | #include "platform.h" | ||
26 | #include <jansson.h> | ||
27 | #include "gnunet_curl_lib.h" | ||
28 | #include "gnunet_util_lib.h" | ||
29 | |||
30 | extern void * | ||
31 | GNUNET_CURL_download_get_result_ (struct GNUNET_CURL_DownloadBuffer *db, | ||
32 | CURL *eh, | ||
33 | long *response_code); | ||
34 | |||
35 | /** | ||
36 | * Closure for #GNUNET_CURL_gnunet_scheduler_reschedule(). | ||
37 | */ | ||
38 | struct GNUNET_CURL_RescheduleContext | ||
39 | { | ||
40 | /** | ||
41 | * Just the task. | ||
42 | */ | ||
43 | struct GNUNET_SCHEDULER_Task *task; | ||
44 | |||
45 | /** | ||
46 | * Context we manage. | ||
47 | */ | ||
48 | struct GNUNET_CURL_Context *ctx; | ||
49 | |||
50 | /** | ||
51 | * Parser of the raw response. | ||
52 | */ | ||
53 | GNUNET_CURL_RawParser parser; | ||
54 | |||
55 | /** | ||
56 | * Deallocate the response object. | ||
57 | */ | ||
58 | GNUNET_CURL_ResponseCleaner cleaner; | ||
59 | }; | ||
60 | |||
61 | |||
62 | struct GNUNET_CURL_RescheduleContext * | ||
63 | GNUNET_CURL_gnunet_rc_create_with_parser (struct GNUNET_CURL_Context *ctx, | ||
64 | GNUNET_CURL_RawParser rp, | ||
65 | GNUNET_CURL_ResponseCleaner rc) | ||
66 | { | ||
67 | struct GNUNET_CURL_RescheduleContext *rctx; | ||
68 | |||
69 | rctx = GNUNET_new (struct GNUNET_CURL_RescheduleContext); | ||
70 | rctx->ctx = ctx; | ||
71 | rctx->parser = rp; | ||
72 | rctx->cleaner = rc; | ||
73 | |||
74 | return rctx; | ||
75 | } | ||
76 | |||
77 | |||
78 | /** | ||
79 | * Just a wrapper to avoid casting of function pointers. | ||
80 | * | ||
81 | * @param response the (JSON) response to clean. | ||
82 | */ | ||
83 | static void | ||
84 | clean_result (void *response) | ||
85 | { | ||
86 | json_decref (response); | ||
87 | } | ||
88 | |||
89 | |||
90 | struct GNUNET_CURL_RescheduleContext * | ||
91 | GNUNET_CURL_gnunet_rc_create (struct GNUNET_CURL_Context *ctx) | ||
92 | { | ||
93 | struct GNUNET_CURL_RescheduleContext *rc; | ||
94 | |||
95 | rc = GNUNET_new (struct GNUNET_CURL_RescheduleContext); | ||
96 | rc->ctx = ctx; | ||
97 | rc->parser = &GNUNET_CURL_download_get_result_; | ||
98 | rc->cleaner = &clean_result; | ||
99 | return rc; | ||
100 | } | ||
101 | |||
102 | |||
103 | void | ||
104 | GNUNET_CURL_gnunet_rc_destroy (struct GNUNET_CURL_RescheduleContext *rc) | ||
105 | { | ||
106 | if (NULL != rc->task) | ||
107 | GNUNET_SCHEDULER_cancel (rc->task); | ||
108 | GNUNET_free (rc); | ||
109 | } | ||
110 | |||
111 | |||
112 | /** | ||
113 | * Task that runs the context's event loop with the GNUnet scheduler. | ||
114 | * | ||
115 | * @param cls a `struct GNUNET_CURL_RescheduleContext *` | ||
116 | */ | ||
117 | static void | ||
118 | context_task (void *cls) | ||
119 | { | ||
120 | struct GNUNET_CURL_RescheduleContext *rc = cls; | ||
121 | long timeout; | ||
122 | int max_fd; | ||
123 | fd_set read_fd_set; | ||
124 | fd_set write_fd_set; | ||
125 | fd_set except_fd_set; | ||
126 | struct GNUNET_NETWORK_FDSet *rs; | ||
127 | struct GNUNET_NETWORK_FDSet *ws; | ||
128 | struct GNUNET_TIME_Relative delay; | ||
129 | |||
130 | rc->task = NULL; | ||
131 | GNUNET_CURL_perform2 (rc->ctx, rc->parser, rc->cleaner); | ||
132 | max_fd = -1; | ||
133 | timeout = -1; | ||
134 | FD_ZERO (&read_fd_set); | ||
135 | FD_ZERO (&write_fd_set); | ||
136 | FD_ZERO (&except_fd_set); | ||
137 | GNUNET_CURL_get_select_info (rc->ctx, | ||
138 | &read_fd_set, | ||
139 | &write_fd_set, | ||
140 | &except_fd_set, | ||
141 | &max_fd, | ||
142 | &timeout); | ||
143 | if (timeout >= 0) | ||
144 | delay = | ||
145 | GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, | ||
146 | timeout); | ||
147 | else | ||
148 | delay = GNUNET_TIME_UNIT_FOREVER_REL; | ||
149 | rs = GNUNET_NETWORK_fdset_create (); | ||
150 | GNUNET_NETWORK_fdset_copy_native (rs, | ||
151 | &read_fd_set, | ||
152 | max_fd + 1); | ||
153 | ws = GNUNET_NETWORK_fdset_create (); | ||
154 | GNUNET_NETWORK_fdset_copy_native (ws, | ||
155 | &write_fd_set, | ||
156 | max_fd + 1); | ||
157 | if (NULL == rc->task) | ||
158 | rc->task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, | ||
159 | delay, | ||
160 | rs, | ||
161 | ws, | ||
162 | &context_task, | ||
163 | rc); | ||
164 | GNUNET_NETWORK_fdset_destroy (rs); | ||
165 | GNUNET_NETWORK_fdset_destroy (ws); | ||
166 | } | ||
167 | |||
168 | |||
169 | void | ||
170 | GNUNET_CURL_gnunet_scheduler_reschedule (void *cls) | ||
171 | { | ||
172 | struct GNUNET_CURL_RescheduleContext *rc = *(void **) cls; | ||
173 | |||
174 | if (NULL != rc->task) | ||
175 | GNUNET_SCHEDULER_cancel (rc->task); | ||
176 | rc->task = GNUNET_SCHEDULER_add_now (&context_task, | ||
177 | rc); | ||
178 | } | ||
179 | |||
180 | |||
181 | /* end of curl_reschedule.c */ | ||