GNUnet  0.11.x
Data Structures | Typedefs | Functions
CURL integration library

Download JSON using libcurl. More...

Data Structures

struct  GNUNET_CURL_DownloadBuffer
 Buffer data structure we use to buffer the HTTP download before giving it to the JSON parser. More...
 

Typedefs

typedef void(* GNUNET_CURL_RescheduleCallback) (void *cls)
 Function called by the context to ask for the event loop to be rescheduled, that is the application should call GNUNET_CURL_get_select_info() as the set of sockets we care about just changed. More...
 
typedef void *(* GNUNET_CURL_RawParser) (struct GNUNET_CURL_DownloadBuffer *db, CURL *eh, long *response_code)
 Parses the raw response we got from the Web server. More...
 
typedef void(* GNUNET_CURL_ResponseCleaner) (void *response)
 Deallocate the response. More...
 
typedef void(* GNUNET_CURL_JobCompletionCallback) (void *cls, long response_code, const void *response)
 Function to call upon completion of a job. More...
 
typedef void(* GNUNET_CURL_RawJobCompletionCallback) (void *cls, long response_code, const void *body, size_t body_size)
 Function to call upon completion of a raw job. More...
 

Functions

struct GNUNET_CURL_ContextGNUNET_CURL_init (GNUNET_CURL_RescheduleCallback cb, void *cb_cls)
 Initialise this library. More...
 
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. More...
 
int GNUNET_CURL_append_header (struct GNUNET_CURL_Context *ctx, const char *header)
 Add custom request header. More...
 
void GNUNET_CURL_perform (struct GNUNET_CURL_Context *ctx)
 Run the main event loop for the CURL interaction. More...
 
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. More...
 
void GNUNET_CURL_fini (struct GNUNET_CURL_Context *ctx)
 Cleanup library initialisation resources. More...
 
struct GNUNET_CURL_JobGNUNET_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. More...
 
struct GNUNET_CURL_JobGNUNET_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. More...
 
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. More...
 
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. More...
 
struct GNUNET_CURL_JobGNUNET_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. More...
 
struct GNUNET_CURL_JobGNUNET_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. More...
 
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. More...
 
void GNUNET_CURL_job_cancel (struct GNUNET_CURL_Job *job)
 Cancel a job. More...
 
struct GNUNET_CURL_RescheduleContextGNUNET_CURL_gnunet_rc_create (struct GNUNET_CURL_Context *ctx)
 Initialize reschedule context. More...
 
struct GNUNET_CURL_RescheduleContextGNUNET_CURL_gnunet_rc_create_with_parser (struct GNUNET_CURL_Context *ctx, GNUNET_CURL_RawParser rp, GNUNET_CURL_ResponseCleaner rc)
 Initialize reschedule context; with custom response parser. More...
 
void GNUNET_CURL_gnunet_rc_destroy (struct GNUNET_CURL_RescheduleContext *rc)
 Destroy reschedule context. More...
 
void GNUNET_CURL_gnunet_scheduler_reschedule (void *cls)
 Implementation of the GNUNET_CURL_RescheduleCallback for GNUnet's scheduler. More...
 
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. More...
 
int GNUNET_CURL_is_valid_scope_id (const char *scope_id)
 Return GNUNET_YES if given a valid scope ID and GNUNET_NO otherwise. More...
 

Detailed Description

Download JSON using libcurl.

Typedef Documentation

◆ GNUNET_CURL_RescheduleCallback

typedef void(* GNUNET_CURL_RescheduleCallback) (void *cls)

Function called by the context to ask for the event loop to be rescheduled, that is the application should call GNUNET_CURL_get_select_info() as the set of sockets we care about just changed.

Parameters
clsclosure

Definition at line 51 of file gnunet_curl_lib.h.

◆ GNUNET_CURL_RawParser

typedef void*(* GNUNET_CURL_RawParser) (struct GNUNET_CURL_DownloadBuffer *db, CURL *eh, long *response_code)

Parses the raw response we got from the Web server.

Parameters
dbthe raw data
ehhandle
response_codeHTTP response code
Returns
the parsed object

Definition at line 86 of file gnunet_curl_lib.h.

◆ GNUNET_CURL_ResponseCleaner

typedef void(* GNUNET_CURL_ResponseCleaner) (void *response)

Deallocate the response.

Parameters
responseobject to clean

Definition at line 96 of file gnunet_curl_lib.h.

◆ GNUNET_CURL_JobCompletionCallback

typedef void(* GNUNET_CURL_JobCompletionCallback) (void *cls, long response_code, const void *response)

Function to call upon completion of a job.

Parameters
clsclosure
response_codeHTTP response code from server, 0 on hard error
responsein JSON, NULL if response was not in JSON format

Definition at line 203 of file gnunet_curl_lib.h.

◆ GNUNET_CURL_RawJobCompletionCallback

typedef void(* GNUNET_CURL_RawJobCompletionCallback) (void *cls, long response_code, const void *body, size_t body_size)

Function to call upon completion of a raw job.

Parameters
clsclosure
response_codeHTTP response code from server, 0 on hard error
bodyhttp body of the response
body_sizenumber of bytes in body

Definition at line 217 of file gnunet_curl_lib.h.

Function Documentation

◆ GNUNET_CURL_init()

struct GNUNET_CURL_Context* GNUNET_CURL_init ( GNUNET_CURL_RescheduleCallback  cb,
void *  cb_cls 
)

Initialise this library.

This function should be called before using any of the following functions.

Parameters
cbfunction to call when rescheduling is required
cb_clsclosure for cb
Returns
library context

Definition at line 268 of file curl.c.

References GNUNET_CURL_Context::cb, GNUNET_CURL_Context::cb_cls, GNUNET_CURL_Job::ctx, curl_fail, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_new, GNUNET_CURL_Context::multi, multi, and GNUNET_CURL_Context::share.

270 {
271  struct GNUNET_CURL_Context *ctx;
272  CURLM *multi;
273  CURLSH *share;
274 
275  if (curl_fail)
276  {
278  "Curl was not initialised properly\n");
279  return NULL;
280  }
281  if (NULL == (multi = curl_multi_init ()))
282  {
284  "Failed to create a Curl multi handle\n");
285  return NULL;
286  }
287  if (NULL == (share = curl_share_init ()))
288  {
290  "Failed to create a Curl share handle\n");
291  return NULL;
292  }
293  ctx = GNUNET_new (struct GNUNET_CURL_Context);
294  ctx->cb = cb;
295  ctx->cb_cls = cb_cls;
296  ctx->multi = multi;
297  ctx->share = share;
298  return ctx;
299 }
#define GNUNET_new(type)
Allocate a struct or union of the given type.
GNUNET_CURL_RescheduleCallback cb
Function we need to call whenever the event loop's socket set changed.
Definition: curl.c:167
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
static CURLM * multi
Current multi-CURL handle.
CURLSH * share
Curl share handle.
Definition: curl.c:140
static int curl_fail
Failsafe flag.
Definition: curl.c:67
Context.
Definition: curl.c:130
CURLM * multi
Curl multi handle.
Definition: curl.c:135
void * cb_cls
Closure for cb.
Definition: curl.c:172
#define GNUNET_log(kind,...)

◆ GNUNET_CURL_get_select_info()

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.

Basically, a client should use this API to prepare for select(), then block on select(), then call GNUNET_CURL_perform() and then start again until the work with the context is done.

This function will NOT zero out the sets and assumes that max_fd and timeout are already set to minimal applicable values. It is safe to give this API FD-sets and max_fd and timeout that are already initialized to some other descriptors that need to go into the select() call.

Parameters
ctxcontext to get the event loop information for
read_fd_setwill be set for any pending read operations
write_fd_setwill be set for any pending write operations
except_fd_setis here because curl_multi_fdset() has this argument
max_fdset to the highest FD included in any set; if the existing sets have no FDs in it, the initial value should be "-1". (Note that max_fd + 1 will need to be passed to select().)
timeoutset to the timeout in milliseconds (!); -1 means no timeout (NULL, blocking forever is OK), 0 means to proceed immediately with GNUNET_CURL_perform().

Note that calling any other GNUNET_CURL-API may also imply that the library is again ready for GNUNET_CURL_perform().

Basically, a client should use this API to prepare for select(), then block on select(), then call GNUNET_CURL_perform() and then start again until the work with the context is done.

This function will NOT zero out the sets and assumes that max_fd and timeout are already set to minimal applicable values. It is safe to give this API FD-sets and max_fd and timeout that are already initialized to some other descriptors that need to go into the select() call.

Parameters
ctxcontext to get the event loop information for
read_fd_setwill be set for any pending read operations
write_fd_setwill be set for any pending write operations
except_fd_setis here because curl_multi_fdset() has this argument
max_fdset to the highest FD included in any set; if the existing sets have no FDs in it, the initial value should be "-1". (Note that max_fd + 1 will need to be passed to select().)
timeoutset to the timeout in milliseconds (!); -1 means no timeout (NULL, blocking forever is OK), 0 means to proceed immediately with GNUNET_CURL_perform().

Definition at line 1012 of file curl.c.

References GNUNET_assert, GNUNET_MAX, GNUNET_CURL_Context::jobs_head, m, GNUNET_CURL_Context::multi, and timeout.

Referenced by context_task().

1018 {
1019  long to;
1020  int m;
1021 
1022  m = -1;
1023  GNUNET_assert (CURLM_OK == curl_multi_fdset (ctx->multi,
1024  read_fd_set,
1025  write_fd_set,
1026  except_fd_set,
1027  &m));
1028  to = *timeout;
1029  *max_fd = GNUNET_MAX (m, *max_fd);
1030  GNUNET_assert (CURLM_OK == curl_multi_timeout (ctx->multi, &to));
1031 
1032  /* Only if what we got back from curl is smaller than what we
1033  already had (-1 == infinity!), then update timeout */
1034  if ((to < *timeout) && (-1 != to))
1035  *timeout = to;
1036  if ((-1 == (*timeout)) && (NULL != ctx->jobs_head))
1037  *timeout = to;
1038 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static struct GNUNET_ARM_MonitorHandle * m
Monitor connection with ARM.
Definition: gnunet-arm.c:104
struct GNUNET_CURL_Job * jobs_head
We keep jobs in a DLL.
Definition: curl.c:145
static struct GNUNET_TIME_Relative timeout
Desired timeout for the lookup (default is no timeout).
Definition: gnunet-abd.c:61
#define GNUNET_MAX(a, b)
Definition: gnunet_common.h:95
CURLM * multi
Curl multi handle.
Definition: curl.c:135
Here is the caller graph for this function:

◆ GNUNET_CURL_append_header()

int GNUNET_CURL_append_header ( struct GNUNET_CURL_Context ctx,
const char *  header 
)

Add custom request header.

Parameters
ctxcURL context.
headerheader string; will be given to the context AS IS.
Returns
GNUNET_OK if no errors occurred, GNUNET_SYSERR otherwise.

Definition at line 825 of file curl.c.

References UrlRequestData::bytes_received, UrlRequestData::bytes_sent, GNUNET_CURL_Context::common_headers, UrlRequestData::count, get_url_benchmark_data(), GNUNET_break, GNUNET_OK, GNUNET_SYSERR, GNUNET_TIME_relative_add(), GNUNET_TIME_relative_max(), GNUNET_TIME_Relative::rel_value_us, UrlRequestData::time, and UrlRequestData::time_max.

826 {
827  ctx->common_headers = curl_slist_append (ctx->common_headers, header);
828  if (NULL == ctx->common_headers)
829  return GNUNET_SYSERR;
830 
831  return GNUNET_OK;
832 }
struct curl_slist * common_headers
Headers common for all requests in the context.
Definition: curl.c:155
Here is the call graph for this function:

◆ GNUNET_CURL_perform()

void GNUNET_CURL_perform ( struct GNUNET_CURL_Context ctx)

Run the main event loop for the CURL interaction.

Parameters
ctxthe library context

Run the main event loop for the CURL interaction.

Parameters
ctxthe library context

Definition at line 975 of file curl.c.

References GNUNET_CURL_download_get_result_(), and GNUNET_CURL_perform2().

976 {
979  (GNUNET_CURL_ResponseCleaner) & json_decref);
980 }
void(* GNUNET_CURL_ResponseCleaner)(void *response)
Deallocate the response.
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.
Definition: curl.c:915
void * GNUNET_CURL_download_get_result_(struct GNUNET_CURL_DownloadBuffer *db, CURL *eh, long *response_code)
Obtain information about the final result about the HTTP download.
Definition: curl.c:745
Here is the call graph for this function:

◆ GNUNET_CURL_perform2()

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.

Parameters
ctxthe library context
rpparses the raw response returned from the Web server.
rccleans/frees the response

Definition at line 915 of file curl.c.

References GNUNET_assert, GNUNET_break, GNUNET_CURL_job_cancel(), job, GNUNET_CURL_Context::multi, response, and rp.

Referenced by context_task(), and GNUNET_CURL_perform().

918 {
919  CURLMsg *cmsg;
920  int n_running;
921  int n_completed;
922 
923  (void) curl_multi_perform (ctx->multi,
924  &n_running);
925  while (NULL != (cmsg = curl_multi_info_read (ctx->multi, &n_completed)))
926  {
927  struct GNUNET_CURL_Job *job;
928  long response_code;
929  void *response;
930 
931  /* Only documented return value is CURLMSG_DONE */
932  GNUNET_break (CURLMSG_DONE == cmsg->msg);
933  GNUNET_assert (CURLE_OK == curl_easy_getinfo (cmsg->easy_handle,
934  CURLINFO_PRIVATE,
935  (char **) &job));
936  GNUNET_assert (job->ctx == ctx);
937  response_code = 0;
938  if (NULL != job->jcc_raw)
939  {
940  /* RAW mode, no parsing */
941  GNUNET_break (CURLE_OK ==
942  curl_easy_getinfo (job->easy_handle,
943  CURLINFO_RESPONSE_CODE,
944  &response_code));
945  job->jcc_raw (job->jcc_raw_cls,
946  response_code,
947  job->db.buf,
948  job->db.buf_size);
949  }
950  else
951  {
952  /* to be parsed via 'rp' */
953  response = rp (&job->db,
954  job->easy_handle,
955  &response_code);
956  job->jcc (job->jcc_cls,
957  response_code,
958  response);
959  rc (response);
960  }
961 #if ENABLE_BENCHMARK
962  do_benchmark (cmsg);
963 #endif
965  }
966 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
void GNUNET_CURL_job_cancel(struct GNUNET_CURL_Job *job)
Cancel a job.
Definition: curl.c:677
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
void * jcc_cls
Closure for jcc.
Definition: curl.c:102
GNUNET_CURL_JobCompletionCallback jcc
Function to call upon completion.
Definition: curl.c:97
GNUNET_CURL_RawJobCompletionCallback jcc_raw
Function to call upon completion.
Definition: curl.c:107
CURLM * multi
Curl multi handle.
Definition: curl.c:135
CURL * easy_handle
Easy handle of the job.
Definition: curl.c:87
static struct GNUNET_SCHEDULER_Task * job
Task for main job.
Definition: gnunet-cadet.c:137
void * buf
Download buffer.
Jobs are CURL requests running within a struct GNUNET_CURL_Context.
Definition: curl.c:72
static char * rp
Relying party.
static struct MHD_Response * response
Our canonical response.
struct GNUNET_CURL_DownloadBuffer db
Buffer for response received from CURL.
Definition: curl.c:117
size_t buf_size
The size of the download buffer.
void * jcc_raw_cls
Closure for jcc_raw.
Definition: curl.c:112
struct GNUNET_CURL_Context * ctx
Context this job runs in.
Definition: curl.c:92
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_CURL_fini()

void GNUNET_CURL_fini ( struct GNUNET_CURL_Context ctx)

Cleanup library initialisation resources.

This function should be called after using this library to cleanup the resources occupied during library's initialisation.

Parameters
ctxthe library context

Definition at line 1049 of file curl.c.

References GNUNET_CURL_Context::certfile, GNUNET_CURL_Context::certtype, GNUNET_CURL_Context::common_headers, GNUNET_assert, GNUNET_free, GNUNET_CURL_Context::jobs_head, GNUNET_CURL_Context::keyfile, GNUNET_CURL_Context::keypass, GNUNET_CURL_Context::multi, GNUNET_CURL_Context::share, and GNUNET_CURL_Context::userpass.

1050 {
1051  /* all jobs must have been cancelled at this time, assert this */
1052  GNUNET_assert (NULL == ctx->jobs_head);
1053  curl_share_cleanup (ctx->share);
1054  curl_multi_cleanup (ctx->multi);
1055  curl_slist_free_all (ctx->common_headers);
1056  GNUNET_free (ctx->userpass);
1057  GNUNET_free (ctx->certtype);
1058  GNUNET_free (ctx->certfile);
1059  GNUNET_free (ctx->keyfile);
1060  GNUNET_free (ctx->keypass);
1061  GNUNET_free (ctx);
1062 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct GNUNET_CURL_Job * jobs_head
We keep jobs in a DLL.
Definition: curl.c:145
CURLSH * share
Curl share handle.
Definition: curl.c:140
char * keyfile
File with the private key to authenticate the TLS client, or NULL.
Definition: curl.c:194
CURLM * multi
Curl multi handle.
Definition: curl.c:135
char * keypass
Passphrase to decrypt keyfile, or NULL.
Definition: curl.c:199
char * certtype
Type of the TLS client certificate used, or NULL.
Definition: curl.c:183
char * certfile
File with the TLS client certificate, or NULL.
Definition: curl.c:188
char * userpass
USERNAME:PASSWORD to use for client-authentication with all requests of this context, or NULL.
Definition: curl.c:178
#define GNUNET_free(ptr)
Wrapper around free.
struct curl_slist * common_headers
Headers common for all requests in the context.
Definition: curl.c:155

◆ GNUNET_CURL_job_add()

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.

Note that the context will make use of the CURLOPT_PRIVATE facility of the CURL eh.

Parameters
ctxcontext to execute the job in
ehcurl easy handle for the request, will be executed AND cleaned up
jcccallback to invoke upon completion
jcc_clsclosure for jcc
Returns
NULL on error (in this case, is still released!)

Definition at line 657 of file curl.c.

References GNUNET_CURL_job_add2().

661 {
662  return GNUNET_CURL_job_add2 (ctx,
663  eh,
664  NULL,
665  jcc,
666  jcc_cls);
667 }
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.
Definition: curl.c:557
void * jcc_cls
Closure for jcc.
Definition: curl.c:102
GNUNET_CURL_JobCompletionCallback jcc
Function to call upon completion.
Definition: curl.c:97
Here is the call graph for this function:

◆ GNUNET_CURL_job_add_with_ct_json()

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.

Note that the context will make use of the CURLOPT_PRIVATE facility of the CURL eh.

This function modifies the CURL handle to add the "Content-Type: application/json" header.

Parameters
ctxcontext to execute the job in
ehcurl easy handle for the request, will be executed AND cleaned up
jcccallback to invoke upon completion
jcc_clsclosure for jcc
Returns
NULL on error (in this case, is still released!)

Definition at line 623 of file curl.c.

References GNUNET_assert, GNUNET_CURL_job_add2(), job, and GNUNET_CURL_Job::job_headers.

627 {
628  struct GNUNET_CURL_Job *job;
629  struct curl_slist *job_headers = NULL;
630 
631  GNUNET_assert (NULL != (job_headers =
632  curl_slist_append (NULL,
633  "Content-Type: application/json")));
634  job = GNUNET_CURL_job_add2 (ctx,
635  eh,
636  job_headers,
637  jcc,
638  jcc_cls);
639  curl_slist_free_all (job_headers);
640  return job;
641 }
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.
Definition: curl.c:557
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static struct GNUNET_SCHEDULER_Task * job
Task for main job.
Definition: gnunet-cadet.c:137
Jobs are CURL requests running within a struct GNUNET_CURL_Context.
Definition: curl.c:72
Here is the call graph for this function:

◆ GNUNET_CURL_set_userpass()

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.

Parameters
ctxcontext to set authentication data for
userpassstring with "$USERNAME:$PASSWORD"

Definition at line 213 of file curl.c.

References GNUNET_free, GNUNET_strdup, and GNUNET_CURL_Context::userpass.

215 {
216  GNUNET_free (ctx->userpass);
217  if (NULL != userpass)
218  ctx->userpass = GNUNET_strdup (userpass);
219 }
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
char * userpass
USERNAME:PASSWORD to use for client-authentication with all requests of this context, or NULL.
Definition: curl.c:178
#define GNUNET_free(ptr)
Wrapper around free.

◆ GNUNET_CURL_set_tlscert()

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.

Note that if the provided information is incorrect, the earliest operation that could fail is GNUNET_CURL_job_add() or GNUNET_CURL_job_add2()!

Parameters
ctxcontext to set authentication data for
certtypetype of the certificate
certfilefile with the certificate
keyfilefile with the private key
keypasspassphrase to decrypt keyfile (or NULL)

Definition at line 238 of file curl.c.

References GNUNET_CURL_Context::certfile, GNUNET_CURL_Context::certtype, GNUNET_free, GNUNET_strdup, GNUNET_CURL_Context::keyfile, and GNUNET_CURL_Context::keypass.

243 {
244  GNUNET_free (ctx->certtype);
245  GNUNET_free (ctx->certfile);
246  GNUNET_free (ctx->keyfile);
247  GNUNET_free (ctx->keypass);
248  if (NULL != certtype)
249  ctx->certtype = GNUNET_strdup (certtype);
250  if (NULL != certfile)
251  ctx->certfile = GNUNET_strdup (certfile);
252  if (NULL != keyfile)
253  ctx->certtype = GNUNET_strdup (keyfile);
254  if (NULL != keypass)
255  ctx->certtype = GNUNET_strdup (keypass);
256 }
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
char * keyfile
File with the private key to authenticate the TLS client, or NULL.
Definition: curl.c:194
char * keypass
Passphrase to decrypt keyfile, or NULL.
Definition: curl.c:199
char * certtype
Type of the TLS client certificate used, or NULL.
Definition: curl.c:183
char * certfile
File with the TLS client certificate, or NULL.
Definition: curl.c:188
#define GNUNET_free(ptr)
Wrapper around free.

◆ GNUNET_CURL_job_add2()

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.

Note that the context will make use of the CURLOPT_PRIVATE facility of the CURL eh.

This function modifies the CURL handle to add the "Content-Type: application/json" header if add_json is set.

Parameters
ctxcontext to execute the job in
ehcurl easy handle for the request, will be executed AND cleaned up
job_headersextra headers to add for this request
jcccallback to invoke upon completion
jcc_clsclosure for jcc
Returns
NULL on error (in this case, is still released!)

Note that the context will make use of the CURLOPT_PRIVATE facility of the CURL eh.

This function modifies the CURL handle to add the "Content-Type: application/json" header if add_json is set.

Parameters
ctxcontext to execute the job in
ehcurl easy handle for the request, will be executed AND cleaned up. NOTE: the handle should never have gotten any headers list, as that would then be overridden by jcc. Therefore, always pass custom headers as the job_headers parameter.
job_headersextra headers to add for this request
jcccallback to invoke upon completion
jcc_clsclosure for jcc
Returns
NULL on error (in this case, is still released!)

Definition at line 557 of file curl.c.

References GNUNET_CURL_Context::cb, GNUNET_CURL_Context::cb_cls, GNUNET_CURL_Context::certfile, GNUNET_CURL_Context::certtype, GNUNET_assert, GNUNET_CURL_Job::jcc, GNUNET_CURL_Job::jcc_cls, job, GNUNET_CURL_Context::keyfile, GNUNET_CURL_Context::keypass, setup_job(), setup_job_headers(), and GNUNET_CURL_Context::userpass.

Referenced by GNUNET_CURL_job_add(), and GNUNET_CURL_job_add_with_ct_json().

562 {
563  struct GNUNET_CURL_Job *job;
564  struct curl_slist *all_headers;
565 
566  GNUNET_assert (NULL != jcc);
567  if ( (NULL != ctx->userpass) &&
568  (0 != curl_easy_setopt (eh,
569  CURLOPT_USERPWD,
570  ctx->userpass)) )
571  return NULL;
572  if ( (NULL != ctx->certfile) &&
573  (0 != curl_easy_setopt (eh,
574  CURLOPT_SSLCERT,
575  ctx->certfile)) )
576  return NULL;
577  if ( (NULL != ctx->certtype) &&
578  (0 != curl_easy_setopt (eh,
579  CURLOPT_SSLCERTTYPE,
580  ctx->certtype)) )
581  return NULL;
582  if ( (NULL != ctx->keyfile) &&
583  (0 != curl_easy_setopt (eh,
584  CURLOPT_SSLKEY,
585  ctx->keyfile)) )
586  return NULL;
587  if ( (NULL != ctx->keypass) &&
588  (0 != curl_easy_setopt (eh,
589  CURLOPT_KEYPASSWD,
590  ctx->keypass)) )
591  return NULL;
592 
593  all_headers = setup_job_headers (ctx,
594  job_headers);
595  if (NULL == (job = setup_job (eh,
596  ctx,
597  all_headers)))
598  return NULL;
599 
600  job->jcc = jcc;
601  job->jcc_cls = jcc_cls;
602  ctx->cb (ctx->cb_cls);
603  return job;
604 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
GNUNET_CURL_RescheduleCallback cb
Function we need to call whenever the event loop&#39;s socket set changed.
Definition: curl.c:167
void * jcc_cls
Closure for jcc.
Definition: curl.c:102
GNUNET_CURL_JobCompletionCallback jcc
Function to call upon completion.
Definition: curl.c:97
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.
Definition: curl.c:387
char * keyfile
File with the private key to authenticate the TLS client, or NULL.
Definition: curl.c:194
static struct GNUNET_SCHEDULER_Task * job
Task for main job.
Definition: gnunet-cadet.c:137
char * keypass
Passphrase to decrypt keyfile, or NULL.
Definition: curl.c:199
char * certtype
Type of the TLS client certificate used, or NULL.
Definition: curl.c:183
void * cb_cls
Closure for cb.
Definition: curl.c:172
Jobs are CURL requests running within a struct GNUNET_CURL_Context.
Definition: curl.c:72
char * certfile
File with the TLS client certificate, or NULL.
Definition: curl.c:188
char * userpass
USERNAME:PASSWORD to use for client-authentication with all requests of this context, or NULL.
Definition: curl.c:178
static struct GNUNET_CURL_Job * setup_job(CURL *eh, struct GNUNET_CURL_Context *ctx, struct curl_slist *all_headers)
Create a job.
Definition: curl.c:436
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_CURL_job_add_raw()

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.

Note that the context will make use of the CURLOPT_PRIVATE facility of the CURL eh. Used to download resources that are NOT in JSON. The raw body will be returned.

Parameters
ctxcontext to execute the job in
ehcurl easy handle for the request, will be executed AND cleaned up
job_headersextra headers to add for this request
max_reply_sizelargest acceptable response body
jcccallback to invoke upon completion
jcc_clsclosure for jcc
Returns
NULL on error (in this case, is still released!)

Definition at line 514 of file curl.c.

References GNUNET_CURL_Context::cb, GNUNET_CURL_Context::cb_cls, GNUNET_assert, GNUNET_CURL_Job::jcc, GNUNET_CURL_Job::jcc_cls, GNUNET_CURL_Job::jcc_raw, GNUNET_CURL_Job::jcc_raw_cls, job, setup_job(), and setup_job_headers().

519 {
520  struct GNUNET_CURL_Job *job;
521  struct curl_slist *all_headers;
522 
523  GNUNET_assert (NULL != jcc);
524  all_headers = setup_job_headers (ctx,
525  job_headers);
526  if (NULL == (job = setup_job (eh,
527  ctx,
528  all_headers)))
529  return NULL;
530  job->jcc_raw = jcc;
531  job->jcc_raw_cls = jcc_cls;
532  ctx->cb (ctx->cb_cls);
533  return job;
534 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
GNUNET_CURL_RescheduleCallback cb
Function we need to call whenever the event loop&#39;s socket set changed.
Definition: curl.c:167
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.
Definition: curl.c:387
GNUNET_CURL_RawJobCompletionCallback jcc_raw
Function to call upon completion.
Definition: curl.c:107
static struct GNUNET_SCHEDULER_Task * job
Task for main job.
Definition: gnunet-cadet.c:137
void * cb_cls
Closure for cb.
Definition: curl.c:172
Jobs are CURL requests running within a struct GNUNET_CURL_Context.
Definition: curl.c:72
void * jcc_raw_cls
Closure for jcc_raw.
Definition: curl.c:112
static struct GNUNET_CURL_Job * setup_job(CURL *eh, struct GNUNET_CURL_Context *ctx, struct curl_slist *all_headers)
Create a job.
Definition: curl.c:436
Here is the call graph for this function:

◆ GNUNET_CURL_extend_headers()

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.

Parameters
[in,out]jobthe job to modify
extra_headersheaders to append

Definition at line 481 of file curl.c.

References GNUNET_assert, and GNUNET_CURL_Job::job_headers.

483 {
484  struct curl_slist *all_headers = job->job_headers;
485 
486  for (const struct curl_slist *curr = extra_headers;
487  NULL != curr;
488  curr = curr->next)
489  {
490  GNUNET_assert (NULL !=
491  (all_headers = curl_slist_append (all_headers,
492  curr->data)));
493  }
494  job->job_headers = all_headers;
495 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct curl_slist * job_headers
Headers used for this job, the list needs to be freed after the job has finished. ...
Definition: curl.c:123

◆ GNUNET_CURL_job_cancel()

void GNUNET_CURL_job_cancel ( struct GNUNET_CURL_Job job)

Cancel a job.

Must only be called before the job completion callback is called for the respective job.

Parameters
jobjob to cancel

Definition at line 677 of file curl.c.

References GNUNET_CURL_DownloadBuffer::buf, GNUNET_CURL_Context::cb, GNUNET_CURL_Context::cb_cls, GNUNET_CURL_Job::ctx, GNUNET_CURL_Job::db, GNUNET_CURL_Job::easy_handle, GNUNET_break, GNUNET_CONTAINER_DLL_remove, GNUNET_free, GNUNET_CURL_Job::job_headers, GNUNET_CURL_Context::jobs_head, GNUNET_CURL_Context::jobs_tail, and GNUNET_CURL_Context::multi.

Referenced by GNUNET_CURL_perform2().

678 {
679  struct GNUNET_CURL_Context *ctx = job->ctx;
680 
682  GNUNET_break (CURLM_OK ==
683  curl_multi_remove_handle (ctx->multi, job->easy_handle));
684  curl_easy_cleanup (job->easy_handle);
685  GNUNET_free (job->db.buf);
686  curl_slist_free_all (job->job_headers);
687  ctx->cb (ctx->cb_cls);
688  GNUNET_free (job);
689 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
GNUNET_CURL_RescheduleCallback cb
Function we need to call whenever the event loop&#39;s socket set changed.
Definition: curl.c:167
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
struct GNUNET_CURL_Job * jobs_head
We keep jobs in a DLL.
Definition: curl.c:145
Context.
Definition: curl.c:130
CURLM * multi
Curl multi handle.
Definition: curl.c:135
CURL * easy_handle
Easy handle of the job.
Definition: curl.c:87
void * buf
Download buffer.
void * cb_cls
Closure for cb.
Definition: curl.c:172
struct GNUNET_CURL_Job * jobs_tail
We keep jobs in a DLL.
Definition: curl.c:150
struct GNUNET_CURL_DownloadBuffer db
Buffer for response received from CURL.
Definition: curl.c:117
struct curl_slist * job_headers
Headers used for this job, the list needs to be freed after the job has finished. ...
Definition: curl.c:123
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_CURL_Context * ctx
Context this job runs in.
Definition: curl.c:92
Here is the caller graph for this function:

◆ GNUNET_CURL_gnunet_rc_create()

struct GNUNET_CURL_RescheduleContext* GNUNET_CURL_gnunet_rc_create ( struct GNUNET_CURL_Context ctx)

Initialize reschedule context.

Parameters
ctxcontext to manage
Returns
closure for GNUNET_CURL_gnunet_scheduler_reschedule().

Definition at line 103 of file curl_reschedule.c.

References clean_result(), GNUNET_CURL_RescheduleContext::cleaner, GNUNET_CURL_RescheduleContext::ctx, GNUNET_CURL_download_get_result_(), GNUNET_new, and GNUNET_CURL_RescheduleContext::parser.

104 {
106 
108  rc->ctx = ctx;
110  rc->cleaner = &clean_result;
111  return rc;
112 }
struct GNUNET_CURL_Context * ctx
Context we manage.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
GNUNET_CURL_ResponseCleaner cleaner
Deallocate the response object.
GNUNET_CURL_RawParser parser
Parser of the raw response.
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
Closure for GNUNET_CURL_gnunet_scheduler_reschedule().
static void clean_result(void *response)
Just a wrapper to avoid casting of function pointers.
void * GNUNET_CURL_download_get_result_(struct GNUNET_CURL_DownloadBuffer *db, CURL *eh, long *response_code)
Obtain information about the final result about the HTTP download.
Definition: curl.c:745
Here is the call graph for this function:

◆ GNUNET_CURL_gnunet_rc_create_with_parser()

struct GNUNET_CURL_RescheduleContext* GNUNET_CURL_gnunet_rc_create_with_parser ( struct GNUNET_CURL_Context ctx,
GNUNET_CURL_RawParser  rp,
GNUNET_CURL_ResponseCleaner  rc 
)

Initialize reschedule context; with custom response parser.

Parameters
ctxcontext to manage
Returns
closure for GNUNET_CURL_gnunet_scheduler_reschedule().

Definition at line 69 of file curl_reschedule.c.

References GNUNET_CURL_RescheduleContext::cleaner, GNUNET_CURL_RescheduleContext::ctx, GNUNET_new, GNUNET_CURL_RescheduleContext::parser, and rp.

72 {
73  struct GNUNET_CURL_RescheduleContext *rctx;
74 
76  rctx->ctx = ctx;
77  rctx->parser = rp;
78  rctx->cleaner = rc;
79 
80  return rctx;
81 }
struct GNUNET_CURL_Context * ctx
Context we manage.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
GNUNET_CURL_ResponseCleaner cleaner
Deallocate the response object.
GNUNET_CURL_RawParser parser
Parser of the raw response.
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
Closure for GNUNET_CURL_gnunet_scheduler_reschedule().
static char * rp
Relying party.

◆ GNUNET_CURL_gnunet_rc_destroy()

void GNUNET_CURL_gnunet_rc_destroy ( struct GNUNET_CURL_RescheduleContext rc)

Destroy reschedule context.

Parameters
rccontext to destroy

Definition at line 121 of file curl_reschedule.c.

References GNUNET_free, GNUNET_SCHEDULER_cancel(), and GNUNET_CURL_RescheduleContext::task.

122 {
123  if (NULL != rc->task)
125  GNUNET_free (rc);
126 }
struct GNUNET_SCHEDULER_Task * task
Just the task.
#define GNUNET_free(ptr)
Wrapper around free.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:972
Here is the call graph for this function:

◆ GNUNET_CURL_gnunet_scheduler_reschedule()

void GNUNET_CURL_gnunet_scheduler_reschedule ( void *  cls)

Implementation of the GNUNET_CURL_RescheduleCallback for GNUnet's scheduler.

Will run the CURL context using GNUnet's scheduler. Note that you MUST immediately destroy the reschedule context after calling GNUNET_CURL_fini().

Parameters
clsmust point to a struct GNUNET_CURL_RescheduleContext * (pointer to a pointer!)

Definition at line 191 of file curl_reschedule.c.

References context_task(), GNUNET_SCHEDULER_add_now(), GNUNET_SCHEDULER_cancel(), and GNUNET_CURL_RescheduleContext::task.

192 {
193  struct GNUNET_CURL_RescheduleContext *rc = *(void **) cls;
194 
195  if (NULL != rc->task)
198 }
Closure for GNUNET_CURL_gnunet_scheduler_reschedule().
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition: scheduler.c:1296
struct GNUNET_SCHEDULER_Task * task
Just the task.
static void context_task(void *cls)
Task that runs the context&#39;s event loop with the GNUnet scheduler.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:972
Here is the call graph for this function:

◆ GNUNET_CURL_enable_async_scope_header()

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.

Parameters
ctxthe context to enable this for
header_namename of the header to send.

Definition at line 309 of file curl.c.

References GNUNET_CURL_Context::async_scope_id_header.

311 {
312  ctx->async_scope_id_header = header_name;
313 }
const char * async_scope_id_header
If non-NULL, the async scope ID is sent in a request header of this name.
Definition: curl.c:161

◆ GNUNET_CURL_is_valid_scope_id()

int GNUNET_CURL_is_valid_scope_id ( const char *  scope_id)

Return GNUNET_YES if given a valid scope ID and GNUNET_NO otherwise.

See GNUNET_CURL_enable_async_scope_header() for the code that generates such a scope_id in an HTTP header.

Returns
GNUNET_YES iff given a valid scope ID

See setup_job_headers, logic related to GNUNET_CURL_enable_async_scope_header() for the code that generates such a scope_id.

Returns
GNUNET_YES iff given a valid scope ID

Definition at line 326 of file curl.c.

References GNUNET_NO, and GNUNET_YES.

327 {
328  if (strlen (scope_id) >= 64)
329  return GNUNET_NO;
330  for (size_t i = 0; i < strlen (scope_id); i++)
331  if (! (isalnum (scope_id[i]) || (scope_id[i] == '-')))
332  return GNUNET_NO;
333  return GNUNET_YES;
334 }