2 This file is part of GNUnet
3 Copyright (C) 2014, 2015, 2016, 2018 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @brief API for downloading JSON via CURL
23 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
24 * @author Christian Grothoff
28 #include "gnunet_curl_lib.h"
31 #include "../util/benchmark.h"
36 * Log error related to CURL operations.
38 * @param type log level
39 * @param function which function failed to run
40 * @param code what was the curl error code
42 #define CURL_STRERROR(type, function, code) \
44 "Curl function `%s' has failed at `%s:%d' with error: %s\n", \
45 function, __FILE__, __LINE__, curl_easy_strerror (code));
48 * Print JSON parsing related error information
50 #define JSON_WARN(error) \
51 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, \
52 "JSON parsing failed at %s:%u: %s (%s)\n", \
53 __FILE__, __LINE__, error.text, error.source)
57 * Failsafe flag. Raised if our constructor fails to initialize
63 * Jobs are CURL requests running within a `struct GNUNET_CURL_Context`.
65 struct GNUNET_CURL_Job
69 * We keep jobs in a DLL.
71 struct GNUNET_CURL_Job *next;
74 * We keep jobs in a DLL.
76 struct GNUNET_CURL_Job *prev;
79 * Easy handle of the job.
84 * Context this job runs in.
86 struct GNUNET_CURL_Context *ctx;
89 * Function to call upon completion.
91 GNUNET_CURL_JobCompletionCallback jcc;
99 * Buffer for response received from CURL.
101 struct GNUNET_CURL_DownloadBuffer db;
109 struct GNUNET_CURL_Context
122 * We keep jobs in a DLL.
124 struct GNUNET_CURL_Job *jobs_head;
127 * We keep jobs in a DLL.
129 struct GNUNET_CURL_Job *jobs_tail;
132 * HTTP header "application/json", created once and used
133 * for all requests that need it.
135 struct curl_slist *json_header;
138 * Function we need to call whenever the event loop's
139 * socket set changed.
141 GNUNET_CURL_RescheduleCallback cb;
151 * Initialise this library. This function should be called before using any of
152 * the following functions.
154 * @param cb function to call when rescheduling is required
155 * @param cb_cls closure for @a cb
156 * @return library context
158 struct GNUNET_CURL_Context *
159 GNUNET_CURL_init (GNUNET_CURL_RescheduleCallback cb,
162 struct GNUNET_CURL_Context *ctx;
168 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
169 "Curl was not initialised properly\n");
172 if (NULL == (multi = curl_multi_init ()))
174 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
175 "Failed to create a Curl multi handle\n");
178 if (NULL == (share = curl_share_init ()))
180 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
181 "Failed to create a Curl share handle\n");
184 ctx = GNUNET_new (struct GNUNET_CURL_Context);
186 ctx->cb_cls = cb_cls;
189 GNUNET_assert (NULL != (ctx->json_header =
190 curl_slist_append (NULL,
191 "Content-Type: application/json")));
197 * Callback used when downloading the reply to an HTTP request.
198 * Just appends all of the data to the `buf` in the
199 * `struct DownloadBuffer` for further processing. The size of
200 * the download is limited to #GNUNET_MAX_MALLOC_CHECKED, if
201 * the download exceeds this size, we abort with an error.
203 * @param bufptr data downloaded via HTTP
204 * @param size size of an item in @a bufptr
205 * @param nitems number of items in @a bufptr
206 * @param cls the `struct DownloadBuffer`
207 * @return number of bytes processed from @a bufptr
210 download_cb (char *bufptr,
215 struct GNUNET_CURL_DownloadBuffer *db = cls;
219 if (0 == size * nitems)
221 /* Nothing (left) to do */
224 msize = size * nitems;
225 if ( (msize + db->buf_size) >= GNUNET_MAX_MALLOC_CHECKED)
228 return 0; /* signals an error to curl */
230 db->buf = GNUNET_realloc (db->buf,
231 db->buf_size + msize);
232 buf = db->buf + db->buf_size;
233 GNUNET_memcpy (buf, bufptr, msize);
234 db->buf_size += msize;
240 * Schedule a CURL request to be executed and call the given @a jcc
241 * upon its completion. Note that the context will make use of the
242 * CURLOPT_PRIVATE facility of the CURL @a eh.
244 * This function modifies the CURL handle to add the
245 * "Content-Type: application/json" header if @a add_json is set.
247 * @param ctx context to execute the job in
248 * @param eh curl easy handle for the request, will
249 * be executed AND cleaned up
250 * @param add_json add "application/json" content type header
251 * @param jcc callback to invoke upon completion
252 * @param jcc_cls closure for @a jcc
253 * @return NULL on error (in this case, @eh is still released!)
255 struct GNUNET_CURL_Job *
256 GNUNET_CURL_job_add (struct GNUNET_CURL_Context *ctx,
259 GNUNET_CURL_JobCompletionCallback jcc,
262 struct GNUNET_CURL_Job *job;
264 if (GNUNET_YES == add_json)
266 curl_easy_setopt (eh,
271 curl_easy_cleanup (eh);
275 job = GNUNET_new (struct GNUNET_CURL_Job);
277 curl_easy_setopt (eh,
281 curl_easy_setopt (eh,
282 CURLOPT_WRITEFUNCTION,
285 curl_easy_setopt (eh,
289 curl_easy_setopt (eh,
293 curl_multi_add_handle (ctx->multi,
298 curl_easy_cleanup (eh);
302 job->easy_handle = eh;
305 job->jcc_cls = jcc_cls;
306 GNUNET_CONTAINER_DLL_insert (ctx->jobs_head,
309 ctx->cb (ctx->cb_cls);
315 * Cancel a job. Must only be called before the job completion
316 * callback is called for the respective job.
318 * @param job job to cancel
321 GNUNET_CURL_job_cancel (struct GNUNET_CURL_Job *job)
323 struct GNUNET_CURL_Context *ctx = job->ctx;
325 GNUNET_CONTAINER_DLL_remove (ctx->jobs_head,
328 GNUNET_break (CURLM_OK ==
329 curl_multi_remove_handle (ctx->multi,
331 curl_easy_cleanup (job->easy_handle);
332 GNUNET_free_non_null (job->db.buf);
338 * Obtain information about the final result about the
339 * HTTP download. If the download was successful, parses
340 * the JSON in the @a db and returns it. Also returns
341 * the HTTP @a response_code. If the download failed,
342 * the return value is NULL. The response code is set
343 * in any case, on download errors to zero.
345 * Calling this function also cleans up @a db.
347 * @param db download buffer
348 * @param eh CURL handle (to get the response code)
349 * @param[out] response_code set to the HTTP response code
350 * (or zero if we aborted the download, i.e.
351 * because the response was too big, or if
352 * the JSON we received was malformed).
353 * @return NULL if downloading a JSON reply failed.
356 download_get_result (struct GNUNET_CURL_DownloadBuffer *db,
364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
365 "Downloaded body: %.*s\n",
370 curl_easy_getinfo (eh,
371 CURLINFO_CONTENT_TYPE,
374 (0 != strcasecmp (ct,
375 "application/json")) )
377 /* No content type or explicitly not JSON, refuse to parse
378 (but keep response code) */
380 curl_easy_getinfo (eh,
381 CURLINFO_RESPONSE_CODE,
384 /* unexpected error... */
388 if (0 != db->buf_size)
389 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
390 "Did NOT detect response as JSON\n");
396 json = json_loadb (db->buf,
398 JSON_REJECT_DUPLICATES | JSON_DISABLE_EOF_CHECK,
406 GNUNET_free_non_null (db->buf);
412 curl_easy_getinfo (eh,
413 CURLINFO_RESPONSE_CODE,
416 /* unexpected error... */
426 * Add custom request header.
428 * @param ctx cURL context.
429 * @param header header string; will be given to the context AS IS.
430 * @return #GNUNET_OK if no errors occurred, #GNUNET_SYSERR otherwise.
433 GNUNET_CURL_append_header (struct GNUNET_CURL_Context *ctx,
436 ctx->json_header = curl_slist_append (ctx->json_header,
438 if (NULL == ctx->json_header)
439 return GNUNET_SYSERR;
446 * Run the main event loop for the Taler interaction.
448 * @param ctx the library context
449 * @param rp parses the raw response returned from
451 * @param rc cleans/frees the response
454 GNUNET_CURL_perform2 (struct GNUNET_CURL_Context *ctx,
455 GNUNET_CURL_RawParser rp,
456 GNUNET_CURL_ResponseCleaner rc)
459 struct GNUNET_CURL_Job *job;
465 (void) curl_multi_perform (ctx->multi,
467 while (NULL != (cmsg = curl_multi_info_read (ctx->multi,
470 /* Only documented return value is CURLMSG_DONE */
471 GNUNET_break (CURLMSG_DONE == cmsg->msg);
472 GNUNET_assert (CURLE_OK ==
473 curl_easy_getinfo (cmsg->easy_handle,
476 GNUNET_assert (job->ctx == ctx);
478 response = rp (&job->db,
484 double total_as_double = 0;
485 struct GNUNET_TIME_Relative total;
486 struct UrlRequestData *urd;
487 /* Some care required, as curl is using data types (long vs curl_off_t vs
488 * double) inconsistently to store byte count. */
489 curl_off_t size_curl = 0;
491 uint64_t bytes_sent = 0;
492 uint64_t bytes_received = 0;
494 GNUNET_break (CURLE_OK ==
495 curl_easy_getinfo (cmsg->easy_handle,
498 total.rel_value_us = total_as_double * 1000 * 1000;
500 GNUNET_break (CURLE_OK ==
501 curl_easy_getinfo (cmsg->easy_handle,
502 CURLINFO_EFFECTIVE_URL,
505 /* HEADER_SIZE + SIZE_DOWNLOAD_T is hopefully the total
506 number of bytes received, not clear from curl docs. */
508 GNUNET_break (CURLE_OK ==
509 curl_easy_getinfo (cmsg->easy_handle,
510 CURLINFO_HEADER_SIZE,
512 bytes_received += size_long;
514 GNUNET_break (CURLE_OK ==
515 curl_easy_getinfo (cmsg->easy_handle,
516 CURLINFO_SIZE_DOWNLOAD_T,
518 bytes_received += size_curl;
520 /* REQUEST_SIZE + SIZE_UPLOAD_T is hopefully the total number of bytes
521 sent, again docs are not completely clear. */
523 GNUNET_break (CURLE_OK ==
524 curl_easy_getinfo (cmsg->easy_handle,
525 CURLINFO_REQUEST_SIZE,
527 bytes_sent += size_long;
529 GNUNET_break (CURLE_OK ==
530 curl_easy_getinfo (cmsg->easy_handle,
531 CURLINFO_SIZE_UPLOAD_T,
533 bytes_sent += size_curl;
535 urd = get_url_benchmark_data (url, (unsigned int) response_code);
537 urd->time = GNUNET_TIME_relative_add (urd->time, total);
538 urd->time_max = GNUNET_TIME_relative_max (total, urd->time_max);
539 urd->bytes_sent += bytes_sent;
540 urd->bytes_received += bytes_received;
543 job->jcc (job->jcc_cls,
547 GNUNET_CURL_job_cancel (job);
553 * Run the main event loop for the Taler interaction.
555 * @param ctx the library context
558 GNUNET_CURL_perform (struct GNUNET_CURL_Context *ctx)
561 GNUNET_CURL_perform2 (ctx,
563 (GNUNET_CURL_ResponseCleaner) &json_decref);
568 * Obtain the information for a select() call to wait until
569 * #GNUNET_CURL_perform() is ready again. Note that calling
570 * any other GNUNET_CURL-API may also imply that the library
571 * is again ready for #GNUNET_CURL_perform().
573 * Basically, a client should use this API to prepare for select(),
574 * then block on select(), then call #GNUNET_CURL_perform() and then
575 * start again until the work with the context is done.
577 * This function will NOT zero out the sets and assumes that @a max_fd
578 * and @a timeout are already set to minimal applicable values. It is
579 * safe to give this API FD-sets and @a max_fd and @a timeout that are
580 * already initialized to some other descriptors that need to go into
583 * @param ctx context to get the event loop information for
584 * @param read_fd_set will be set for any pending read operations
585 * @param write_fd_set will be set for any pending write operations
586 * @param except_fd_set is here because curl_multi_fdset() has this argument
587 * @param max_fd set to the highest FD included in any set;
588 * if the existing sets have no FDs in it, the initial
589 * value should be "-1". (Note that `max_fd + 1` will need
590 * to be passed to select().)
591 * @param timeout set to the timeout in milliseconds (!); -1 means
592 * no timeout (NULL, blocking forever is OK), 0 means to
593 * proceed immediately with #GNUNET_CURL_perform().
596 GNUNET_CURL_get_select_info (struct GNUNET_CURL_Context *ctx,
598 fd_set *write_fd_set,
599 fd_set *except_fd_set,
607 GNUNET_assert (CURLM_OK ==
608 curl_multi_fdset (ctx->multi,
614 *max_fd = GNUNET_MAX (m, *max_fd);
615 GNUNET_assert (CURLM_OK ==
616 curl_multi_timeout (ctx->multi,
619 /* Only if what we got back from curl is smaller than what we
620 already had (-1 == infinity!), then update timeout */
621 if ( (to < *timeout) &&
624 if ( (-1 == (*timeout)) &&
625 (NULL != ctx->jobs_head) )
631 * Cleanup library initialisation resources. This function should be called
632 * after using this library to cleanup the resources occupied during library's
635 * @param ctx the library context
638 GNUNET_CURL_fini (struct GNUNET_CURL_Context *ctx)
640 /* all jobs must have been cancelled at this time, assert this */
641 GNUNET_assert (NULL == ctx->jobs_head);
642 curl_share_cleanup (ctx->share);
643 curl_multi_cleanup (ctx->multi);
644 curl_slist_free_all (ctx->json_header);
650 * Initial global setup logic, specifically runs the Curl setup.
652 __attribute__ ((constructor))
654 GNUNET_CURL_constructor__ (void)
658 if (CURLE_OK != (ret = curl_global_init (CURL_GLOBAL_DEFAULT)))
660 CURL_STRERROR (GNUNET_ERROR_TYPE_ERROR,
669 * Cleans up after us, specifically runs the Curl cleanup.
671 __attribute__ ((destructor))
673 GNUNET_CURL_destructor__ (void)
677 curl_global_cleanup ();