GNUnet  0.11.x
Data Structures | Macros | Functions | Variables
curl.c File Reference

API for downloading JSON via CURL. More...

#include "platform.h"
#include <jansson.h>
#include "gnunet_curl_lib.h"
Include dependency graph for curl.c:

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 CURL_STRERROR(type, function, code)
 Log error related to CURL operations. More...
 
#define JSON_WARN(error)
 Print JSON parsing related error information. More...
 

Functions

struct GNUNET_CURL_ContextGNUNET_CURL_init (GNUNET_CURL_RescheduleCallback cb, void *cb_cls)
 Initialise this library. 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...
 
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. More...
 
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. More...
 
static struct GNUNET_CURL_Jobsetup_job (CURL *eh, struct GNUNET_CURL_Context *ctx, struct curl_slist *all_headers)
 Create a job. 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...
 
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 (struct GNUNET_CURL_Context *ctx, CURL *eh, int add_json, 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_job_cancel (struct GNUNET_CURL_Job *job)
 Cancel a job. More...
 
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. More...
 
int GNUNET_CURL_append_header (struct GNUNET_CURL_Context *ctx, const char *header)
 Add custom request header. 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_perform (struct GNUNET_CURL_Context *ctx)
 Run the main event loop for the HTTP interaction. 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...
 
void GNUNET_CURL_fini (struct GNUNET_CURL_Context *ctx)
 Cleanup library initialisation resources. More...
 
 __attribute__ ((constructor))
 Initial global setup logic, specifically runs the Curl setup. More...
 
 __attribute__ ((destructor))
 Cleans up after us, specifically runs the Curl cleanup. More...
 

Variables

static int curl_fail
 Failsafe flag. More...
 

Detailed Description

API for downloading JSON via CURL.

Author
Sree Harsha Totakura sreeh.nosp@m.arsh.nosp@m.a@tot.nosp@m.akur.nosp@m.a.in
Christian Grothoff

Definition in file curl.c.

Macro Definition Documentation

◆ CURL_STRERROR

#define CURL_STRERROR (   type,
  function,
  code 
)
Value:
"Curl function `%s' has failed at `%s:%d' with error: %s\n", \
function, \
__FILE__, \
__LINE__, \
curl_easy_strerror (code));
#define GNUNET_log(kind,...)
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model

Log error related to CURL operations.

Parameters
typelog level
functionwhich function failed to run
codewhat was the curl error code

Definition at line 42 of file curl.c.

Referenced by __attribute__().

◆ JSON_WARN

#define JSON_WARN (   error)
Value:
"JSON parsing failed at %s:%u: %s (%s)\n", \
__FILE__, \
__LINE__, \
error.text, \
error.source)
#define GNUNET_log(kind,...)

Print JSON parsing related error information.

Definition at line 53 of file curl.c.

Referenced by GNUNET_CURL_download_get_result_().

Function Documentation

◆ download_cb()

static size_t download_cb ( char *  bufptr,
size_t  size,
size_t  nitems,
void *  cls 
)
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.

Parameters
bufptrdata downloaded via HTTP
sizesize of an item in bufptr
nitemsnumber of items in bufptr
clsthe struct DownloadBuffer
Returns
number of bytes processed from bufptr

Definition at line 266 of file curl.c.

References GNUNET_CURL_DownloadBuffer::buf, buf, GNUNET_CURL_DownloadBuffer::buf_size, GNUNET_CURL_Job::db, GNUNET_CURL_DownloadBuffer::eno, GNUNET_MAX_MALLOC_CHECKED, GNUNET_memcpy, and GNUNET_realloc.

Referenced by setup_job().

267 {
268  struct GNUNET_CURL_DownloadBuffer *db = cls;
269  size_t msize;
270  void *buf;
271 
272  if (0 == size * nitems)
273  {
274  /* Nothing (left) to do */
275  return 0;
276  }
277  msize = size * nitems;
278  if ((msize + db->buf_size) >= GNUNET_MAX_MALLOC_CHECKED)
279  {
280  db->eno = ENOMEM;
281  return 0; /* signals an error to curl */
282  }
283  db->buf = GNUNET_realloc (db->buf, db->buf_size + msize);
284  buf = db->buf + db->buf_size;
285  GNUNET_memcpy (buf, bufptr, msize);
286  db->buf_size += msize;
287  return msize;
288 }
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_realloc(ptr, size)
Wrapper around realloc.
static char buf[2048]
static unsigned int size
Size of the "table".
Definition: peer.c:67
#define GNUNET_MAX_MALLOC_CHECKED
Maximum allocation with GNUNET_malloc macro.
Buffer data structure we use to buffer the HTTP download before giving it to the JSON parser...
static struct GNUNET_FS_DirectoryBuilder * db
Definition: gnunet-search.c:41
void * buf
Download buffer.
size_t buf_size
The size of the download buffer.
int eno
Error code (based on libc errno) if we failed to download (i.e.
Here is the caller graph for this function:

◆ setup_job_headers()

static struct curl_slist* setup_job_headers ( struct GNUNET_CURL_Context ctx,
const struct curl_slist *  job_headers 
)
static

Create the HTTP headers for the request.

Parameters
ctxcontext we run in
job_headersjob-specific headers
Returns
all headers to use

Definition at line 299 of file curl.c.

References GNUNET_CURL_Context::async_scope_id_header, GNUNET_CURL_Context::common_headers, 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(), and GNUNET_CURL_job_add_raw().

301 {
302  struct curl_slist *all_headers = NULL;
303 
304  for (const struct curl_slist *curr = job_headers; curr != NULL;
305  curr = curr->next)
306  {
307  GNUNET_assert (NULL !=
308  (all_headers = curl_slist_append (all_headers, curr->data)));
309  }
310 
311  for (const struct curl_slist *curr = ctx->common_headers; curr != NULL;
312  curr = curr->next)
313  {
314  GNUNET_assert (NULL !=
315  (all_headers = curl_slist_append (all_headers, curr->data)));
316  }
317 
318  if (NULL != ctx->async_scope_id_header)
319  {
321 
323  if (GNUNET_YES == scope.have_scope)
324  {
325  char *aid_header = NULL;
326  aid_header =
328  sizeof(
329  struct GNUNET_AsyncScopeId));
330  GNUNET_assert (NULL != aid_header);
331  GNUNET_assert (NULL != curl_slist_append (all_headers, aid_header));
332  GNUNET_free (aid_header);
333  }
334  }
335  return all_headers;
336 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
Identifier for an asynchronous execution context.
static enum GNUNET_NetworkType scope
Which network scope do we belong to?
void GNUNET_async_scope_get(struct GNUNET_AsyncScopeSave *scope_ret)
Get the current async scope.
Saved async scope identifier or root scope.
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:160
#define GNUNET_YES
Definition: gnunet_common.h:85
char * GNUNET_STRINGS_data_to_string_alloc(const void *buf, size_t size)
Return the base32crockford encoding of the given buffer.
Definition: strings.c:935
#define GNUNET_free(ptr)
Wrapper around free.
struct curl_slist * common_headers
Headers common for all requests in the context.
Definition: curl.c:154
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setup_job()

static struct GNUNET_CURL_Job* setup_job ( CURL *  eh,
struct GNUNET_CURL_Context ctx,
struct curl_slist *  all_headers 
)
static

Create a job.

Parameters
eheasy handle to use
ctxcontext to run the job in
all_headersHTTP client headers to use (free'd)
Returns
NULL on error

Definition at line 348 of file curl.c.

References GNUNET_CURL_Job::ctx, GNUNET_CURL_Job::db, download_cb(), GNUNET_CURL_Job::easy_handle, GNUNET_break, GNUNET_CONTAINER_DLL_insert, GNUNET_free, GNUNET_new, job, GNUNET_CURL_Job::job_headers, GNUNET_CURL_Context::jobs_head, GNUNET_CURL_Context::jobs_tail, GNUNET_CURL_Context::multi, and GNUNET_CURL_Context::share.

Referenced by GNUNET_CURL_job_add2(), and GNUNET_CURL_job_add_raw().

351 {
352  struct GNUNET_CURL_Job *job;
353 
354  if (CURLE_OK !=
355  curl_easy_setopt (eh, CURLOPT_HTTPHEADER, all_headers))
356  {
357  GNUNET_break (0);
358  curl_slist_free_all (all_headers);
359  curl_easy_cleanup (eh);
360  return NULL;
361  }
362  job = GNUNET_new (struct GNUNET_CURL_Job);
363  job->job_headers = all_headers;
364 
365  if ((CURLE_OK != curl_easy_setopt (eh, CURLOPT_PRIVATE, job)) ||
366  (CURLE_OK !=
367  curl_easy_setopt (eh, CURLOPT_WRITEFUNCTION, &download_cb)) ||
368  (CURLE_OK != curl_easy_setopt (eh, CURLOPT_WRITEDATA, &job->db)) ||
369  (CURLE_OK != curl_easy_setopt (eh, CURLOPT_SHARE, ctx->share)) ||
370  (CURLM_OK != curl_multi_add_handle (ctx->multi, eh)))
371  {
372  GNUNET_break (0);
373  GNUNET_free (job);
374  curl_easy_cleanup (eh);
375  return NULL;
376  }
377  job->easy_handle = eh;
378  job->ctx = ctx;
380  return job;
381 }
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#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:144
CURLSH * share
Curl share handle.
Definition: curl.c:139
CURLM * multi
Curl multi handle.
Definition: curl.c:134
CURL * easy_handle
Easy handle of the job.
Definition: curl.c:86
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:71
struct GNUNET_CURL_Job * jobs_tail
We keep jobs in a DLL.
Definition: curl.c:149
struct GNUNET_CURL_DownloadBuffer db
Buffer for response received from CURL.
Definition: curl.c:116
struct curl_slist * job_headers
Headers used for this job, the list needs to be freed after the job has finished. ...
Definition: curl.c:122
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_CURL_Context * ctx
Context this job runs in.
Definition: curl.c:91
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.
Definition: curl.c:266
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_CURL_download_get_result_()

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.

If the download was successful, parses the JSON in the db and returns it. Also returns the HTTP response_code. If the download failed, the return value is NULL. The response code is set in any case, on download errors to zero.

Calling this function also cleans up db.

Parameters
dbdownload buffer
ehCURL handle (to get the response code)
[out]response_codeset to the HTTP response code (or zero if we aborted the download, i.e. because the response was too big, or if the JSON we received was malformed).
Returns
NULL if downloading a JSON reply failed.

Definition at line 547 of file curl.c.

References GNUNET_CURL_DownloadBuffer::buf, GNUNET_CURL_DownloadBuffer::buf_size, GNUNET_CURL_DownloadBuffer::eno, GNUNET_break, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_WARNING, GNUNET_free_non_null, GNUNET_log, and JSON_WARN.

Referenced by GNUNET_CURL_gnunet_rc_create(), and GNUNET_CURL_perform().

550 {
551  json_t *json;
552  json_error_t error;
553  char *ct;
554 
556  "Downloaded body: %.*s\n",
557  (int) db->buf_size,
558  (char *) db->buf);
559 
560  if ((CURLE_OK !=
561  curl_easy_getinfo (eh,
562  CURLINFO_CONTENT_TYPE,
563  &ct)) ||
564  (NULL == ct) ||
565  (0 != strcasecmp (ct,
566  "application/json")))
567  {
568  /* No content type or explicitly not JSON, refuse to parse
569  (but keep response code) */
570  if (CURLE_OK !=
571  curl_easy_getinfo (eh,
572  CURLINFO_RESPONSE_CODE,
573  response_code))
574  {
575  /* unexpected error... */
576  GNUNET_break (0);
577  *response_code = 0;
578  }
579  if (0 != db->buf_size)
581  "Did NOT detect response `%.*s' as JSON\n",
582  (int) db->buf_size,
583  (const char *) db->buf);
584  return NULL;
585  }
586  json = NULL;
587  if (0 == db->eno)
588  {
589  json = json_loadb (db->buf,
590  db->buf_size,
591  JSON_REJECT_DUPLICATES | JSON_DISABLE_EOF_CHECK,
592  &error);
593  if (NULL == json)
594  {
595  JSON_WARN (error);
596  *response_code = 0;
597  }
598  }
600  db->buf = NULL;
601  db->buf_size = 0;
602  if (NULL != json)
603  {
604  if (CURLE_OK !=
605  curl_easy_getinfo (eh,
606  CURLINFO_RESPONSE_CODE,
607  response_code))
608  {
609  /* unexpected error... */
610  GNUNET_break (0);
611  *response_code = 0;
612  }
613  }
614  return json;
615 }
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define JSON_WARN(error)
Print JSON parsing related error information.
Definition: curl.c:53
void * buf
Download buffer.
#define GNUNET_log(kind,...)
size_t buf_size
The size of the download buffer.
int eno
Error code (based on libc errno) if we failed to download (i.e.
Here is the caller graph for this function:

◆ __attribute__() [1/2]

__attribute__ ( (constructor)  )

Initial global setup logic, specifically runs the Curl setup.

Definition at line 863 of file curl.c.

References curl_fail, CURL_STRERROR, GNUNET_ERROR_TYPE_ERROR, and ret.

865 {
866  CURLcode ret;
867 
868  if (CURLE_OK != (ret = curl_global_init (CURL_GLOBAL_DEFAULT)))
869  {
870  CURL_STRERROR (GNUNET_ERROR_TYPE_ERROR, "curl_global_init", ret);
871  curl_fail = 1;
872  }
873 }
#define CURL_STRERROR(type, function, code)
Log error related to CURL operations.
Definition: curl.c:42
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static int curl_fail
Failsafe flag.
Definition: curl.c:66

◆ __attribute__() [2/2]

__attribute__ ( (destructor)  )

Cleans up after us, specifically runs the Curl cleanup.

Definition at line 879 of file curl.c.

References curl_fail.

881 {
882  if (curl_fail)
883  return;
884  curl_global_cleanup ();
885 }
static int curl_fail
Failsafe flag.
Definition: curl.c:66

Variable Documentation

◆ curl_fail

int curl_fail
static

Failsafe flag.

Raised if our constructor fails to initialize the Curl library.

Definition at line 66 of file curl.c.

Referenced by __attribute__(), and GNUNET_CURL_init().