API for downloading JSON via CURL. More...
#include "platform.h"#include <jansson.h>#include <microhttpd.h>#include "gnunet_curl_lib.h"#include "curl_internal.h"Go to the source code of this file.
Data Structures | |
| struct | GNUNET_CURL_Job |
Jobs are CURL requests running within a struct GNUNET_CURL_Context. More... | |
| struct | GNUNET_CURL_Context |
| Context. More... | |
Macros | |
| #define | DEBUG 0 |
| Set to 1 for extra debug logging. | |
| #define | CURL_STRERROR(type, function, code) |
| Log error related to CURL operations. | |
| #define | JSON_WARN(error) |
| Print JSON parsing related error information. | |
Functions | |
| void | GNUNET_CURL_set_userpass (struct GNUNET_CURL_Context *ctx, const char *userpass) |
| Force use of the provided username and password for client authentication for all operations performed with ctx. | |
| void | GNUNET_CURL_set_tlscert (struct GNUNET_CURL_Context *ctx, const char *certtype, const char *certfile, const char *keyfile, const char *keypass) |
| Force use of the provided TLS client certificate for client authentication for all operations performed with ctx. | |
| struct GNUNET_CURL_Context * | GNUNET_CURL_init (GNUNET_CURL_RescheduleCallback cb, void *cb_cls) |
| Initialise this library. | |
| void | GNUNET_CURL_enable_async_scope_header (struct GNUNET_CURL_Context *ctx, const char *header_name) |
| Enable sending the async scope ID as a header. | |
| enum GNUNET_GenericReturnValue | GNUNET_CURL_is_valid_scope_id (const char *scope_id) |
| Return GNUNET_YES if given a valid scope ID and GNUNET_NO otherwise. | |
| static size_t | download_cb (char *bufptr, size_t size, size_t nitems, void *cls) |
| Callback used when downloading the reply to an HTTP request. | |
| static struct curl_slist * | setup_job_headers (struct GNUNET_CURL_Context *ctx, const struct curl_slist *job_headers) |
| Create the HTTP headers for the request. | |
| static size_t | stream_write_cb (char *bufptr, size_t size, size_t nitems, void *cls) |
| Callback used when the caller wants the response body as it arrives, instead of buffered. | |
| static size_t | stream_header_cb (char *bufptr, size_t size, size_t nitems, void *cls) |
| Callback used to pass the response's header lines on to a streaming caller. | |
| static bool | setup_job_io (CURL *eh, struct GNUNET_CURL_Job *job) |
| Point eh's body and header handling at job, in whichever of the two modes job was set up for. | |
| static struct GNUNET_CURL_Job * | setup_job (CURL *eh, struct GNUNET_CURL_Context *ctx, struct curl_slist *all_headers, const struct GNUNET_CURL_StreamHandlers *sh) |
| Create a job. | |
| void | GNUNET_CURL_extend_headers (struct GNUNET_CURL_Job *job, const struct curl_slist *extra_headers) |
| Add extra_headers to the HTTP headers for job. | |
| struct GNUNET_CURL_Job * | GNUNET_CURL_job_add_raw (struct GNUNET_CURL_Context *ctx, CURL *eh, const struct curl_slist *job_headers, GNUNET_CURL_RawJobCompletionCallback jcc, void *jcc_cls) |
| Schedule a CURL request to be executed and call the given jcc upon its completion. | |
| struct GNUNET_CURL_Job * | GNUNET_CURL_job_add_stream (struct GNUNET_CURL_Context *ctx, CURL *eh, const struct curl_slist *job_headers, const struct GNUNET_CURL_StreamHandlers *sh) |
| Schedule a CURL request whose response is delivered incrementally, rather than buffered in full and handed over at the end. | |
| void | GNUNET_CURL_job_set_paused (struct GNUNET_CURL_Job *job, bool recv_paused, bool send_paused) |
| Say which directions of job are to be paused from now on. | |
| struct GNUNET_CURL_Job * | GNUNET_CURL_job_add2 (struct GNUNET_CURL_Context *ctx, CURL *eh, const struct curl_slist *job_headers, GNUNET_CURL_JobCompletionCallback jcc, void *jcc_cls) |
| Schedule a CURL request to be executed and call the given jcc upon its completion. | |
| struct GNUNET_CURL_Job * | GNUNET_CURL_job_add_with_ct_json (struct GNUNET_CURL_Context *ctx, CURL *eh, GNUNET_CURL_JobCompletionCallback jcc, void *jcc_cls) |
| Schedule a CURL request to be executed and call the given jcc upon its completion. | |
| struct GNUNET_CURL_Job * | GNUNET_CURL_job_add (struct GNUNET_CURL_Context *ctx, CURL *eh, GNUNET_CURL_JobCompletionCallback jcc, void *jcc_cls) |
| Schedule a CURL request to be executed and call the given jcc upon its completion. | |
| void | GNUNET_CURL_job_cancel (struct GNUNET_CURL_Job *job) |
| Cancel a job. | |
| static bool | is_json (const char *ct) |
| Test if the given content type ct is JSON. | |
| void * | GNUNET_CURL_download_get_result_ (struct GNUNET_CURL_DownloadBuffer *db, CURL *eh, long *response_code) |
| enum GNUNET_GenericReturnValue | GNUNET_CURL_append_header (struct GNUNET_CURL_Context *ctx, const char *header) |
| Add custom request header. | |
| void | GNUNET_CURL_perform2 (struct GNUNET_CURL_Context *ctx, GNUNET_CURL_RawParser rp, GNUNET_CURL_ResponseCleaner rc) |
| Run the main event loop for the HTTP interaction. | |
| void | GNUNET_CURL_perform (struct GNUNET_CURL_Context *ctx) |
| Run the main event loop for the CURL interaction. | |
| void | GNUNET_CURL_get_select_info (struct GNUNET_CURL_Context *ctx, fd_set *read_fd_set, fd_set *write_fd_set, fd_set *except_fd_set, int *max_fd, long *timeout) |
| Obtain the information for a select() call to wait until GNUNET_CURL_perform() is ready again. | |
| void | GNUNET_CURL_fini (struct GNUNET_CURL_Context *ctx) |
| Cleanup library initialisation resources. | |
| void | GNUNET_CURL_constructor__ (void) |
| void | __attribute__ ((constructor)) |
| Initial global setup logic, specifically runs the Curl setup. | |
| void | GNUNET_CURL_destructor__ (void) |
| void | __attribute__ ((destructor)) |
| Cleans up after us, specifically runs the Curl cleanup. | |
Variables | |
| static int | curl_fail |
| Failsafe flag. | |
API for downloading JSON via CURL.
Definition in file curl.c.
| #define CURL_STRERROR | ( | type, | |
| function, | |||
| code | |||
| ) |
Log error related to CURL operations.
| type | log level |
| function | which function failed to run |
| code | what was the curl error code |
Definition at line 48 of file curl.c.
| #define JSON_WARN | ( | error | ) |
|
static |
Callback used when downloading the reply to an HTTP request.
Just appends all of the data to the buf in the struct DownloadBuffer for further processing. The size of the download is limited to GNUNET_MAX_MALLOC_CHECKED, if the download exceeds this size, we abort with an error.
| bufptr | data downloaded via HTTP |
| size | size of an item in bufptr |
| nitems | number of items in bufptr |
| cls | the struct DownloadBuffer |
Definition at line 327 of file curl.c.
References GNUNET_CURL_DownloadBuffer::buf, db, GNUNET_MAX_MALLOC_CHECKED, GNUNET_memcpy, GNUNET_realloc, and size.
Referenced by setup_job_io().
|
static |
Create the HTTP headers for the request.
| ctx | context we run in |
| job_headers | job-specific headers |
Definition at line 364 of file curl.c.
References ctx, GNUNET_assert, GNUNET_async_scope_get(), GNUNET_free, GNUNET_STRINGS_data_to_string_alloc(), GNUNET_YES, GNUNET_AsyncScopeSave::have_scope, and GNUNET_AsyncScopeSave::scope_id.
Referenced by GNUNET_CURL_job_add2(), GNUNET_CURL_job_add_raw(), and GNUNET_CURL_job_add_stream().
|
static |
Callback used when the caller wants the response body as it arrives, instead of buffered.
Hands the chunk straight to the caller's GNUNET_CURL_StreamCallback, whose return value libcurl understands directly: msize accepts, GNUNET_CURL_STREAM_PAUSE suspends delivery, anything else fails the transfer.
| bufptr | data downloaded via HTTP |
| size | size of an item in bufptr |
| nitems | number of items in bufptr |
| cls | the struct GNUNET_CURL_Job |
Definition at line 424 of file curl.c.
Referenced by setup_job_io().
|
static |
Callback used to pass the response's header lines on to a streaming caller.
Note that libcurl routes everything through here that it reads in a header section, including status lines, the blank line ending each section, interim (1xx) responses and trailers; sorting those out is the caller's job, and GNUNET_CURL_StreamHeaderCallback says so.
| bufptr | one header line, not 0-terminated |
| size | size of an item in bufptr |
| nitems | number of items in bufptr |
| cls | the struct GNUNET_CURL_Job |
Definition at line 456 of file curl.c.
References GNUNET_OK, job, and size.
Referenced by setup_job_io().
|
static |
Point eh's body and header handling at job, in whichever of the two modes job was set up for.
| eh | easy handle to configure | |
| [in,out] | job | the job eh belongs to |
Definition at line 484 of file curl.c.
References download_cb(), job, stream_header_cb(), and stream_write_cb().
Referenced by setup_job().
|
static |
Create a job.
| eh | easy handle to use |
| ctx | context to run the job in |
| all_headers | HTTP client headers to use (free'd) |
| sh | handlers for a streaming job, or NULL for a buffering one |
Definition at line 525 of file curl.c.
References ctx, GNUNET_break, GNUNET_CONTAINER_DLL_insert, GNUNET_free, GNUNET_new, GNUNET_TIME_absolute_get(), job, setup_job_io(), and sh.
Referenced by GNUNET_CURL_job_add2(), GNUNET_CURL_job_add_raw(), and GNUNET_CURL_job_add_stream().
|
static |
Test if the given content type ct is JSON.
| ct | a content type, e.g. "application/json; charset=UTF-8" |
Definition at line 792 of file curl.c.
Referenced by GNUNET_CURL_download_get_result_().
| void * GNUNET_CURL_download_get_result_ | ( | struct GNUNET_CURL_DownloadBuffer * | db, |
| CURL * | eh, | ||
| long * | response_code | ||
| ) |
Definition at line 820 of file curl.c.
References db, GNUNET_break, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_log, is_json(), and JSON_WARN.
Referenced by GNUNET_CURL_gnunet_rc_create(), and GNUNET_CURL_perform().
| void GNUNET_CURL_constructor__ | ( | void | ) |
| void __attribute__ | ( | (constructor) | ) |
Initial global setup logic, specifically runs the Curl setup.
Definition at line 1095 of file curl.c.
References curl_fail, CURL_STRERROR, GNUNET_ERROR_TYPE_ERROR, and ret.
| void GNUNET_CURL_destructor__ | ( | void | ) |
| void __attribute__ | ( | (destructor) | ) |
|
static |
Failsafe flag.
Raised if our constructor fails to initialize the Curl library.
Definition at line 72 of file curl.c.
Referenced by __attribute__(), __attribute__(), and GNUNET_CURL_init().