GNUnet  0.20.0
curl.c File Reference

API for downloading JSON via CURL. More...

#include "platform.h"
#include <jansson.h>
#include <microhttpd.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 DEBUG   0
 Set to 1 for extra debug logging. More...
 
#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

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_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...
 
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. 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...
 
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...
 
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_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...
 
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...
 
void GNUNET_CURL_job_cancel (struct GNUNET_CURL_Job *job)
 Cancel a job. More...
 
static bool is_json (const char *ct)
 Test if the given content type ct is JSON. More...
 
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. 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 CURL 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

◆ DEBUG

#define DEBUG   0

Set to 1 for extra debug logging.

Definition at line 38 of file curl.c.

◆ 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 47 of file curl.c.

◆ JSON_WARN

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

Print JSON parsing related error information.

Definition at line 58 of file curl.c.

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 314 of file curl.c.

318 {
319  struct GNUNET_CURL_DownloadBuffer *db = cls;
320  size_t msize;
321  void *buf;
322 
323  if (0 == size * nitems)
324  {
325  /* Nothing (left) to do */
326  return 0;
327  }
328  msize = size * nitems;
329  if ((msize + db->buf_size) >= GNUNET_MAX_MALLOC_CHECKED)
330  {
331  db->eno = ENOMEM;
332  return 0; /* signals an error to curl */
333  }
334  db->buf = GNUNET_realloc (db->buf,
335  db->buf_size + msize);
336  buf = db->buf + db->buf_size;
337  GNUNET_memcpy (buf, bufptr, msize);
338  db->buf_size += msize;
339  return msize;
340 }
static struct GNUNET_FS_DirectoryBuilder * db
Definition: gnunet-search.c:97
static char buf[2048]
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_MAX_MALLOC_CHECKED
Maximum allocation with GNUNET_malloc macro.
#define GNUNET_realloc(ptr, size)
Wrapper around realloc.
static unsigned int size
Size of the "table".
Definition: peer.c:68
Buffer data structure we use to buffer the HTTP download before giving it to the JSON parser.

References buf, db, GNUNET_MAX_MALLOC_CHECKED, GNUNET_memcpy, GNUNET_realloc, and size.

Referenced by setup_job().

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 351 of file curl.c.

353 {
354  struct curl_slist *all_headers = NULL;
355 
356  for (const struct curl_slist *curr = job_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  for (const struct curl_slist *curr = ctx->common_headers;
366  NULL != curr;
367  curr = curr->next)
368  {
369  GNUNET_assert (NULL !=
370  (all_headers = curl_slist_append (all_headers,
371  curr->data)));
372  }
373 
374  if (NULL != ctx->async_scope_id_header)
375  {
377 
379  if (GNUNET_YES == scope.have_scope)
380  {
381  char *aid_header;
382 
383  aid_header =
385  &scope.scope_id,
386  sizeof(struct GNUNET_AsyncScopeId));
387  GNUNET_assert (NULL != aid_header);
388  GNUNET_assert (NULL != curl_slist_append (all_headers,
389  aid_header));
390  GNUNET_free (aid_header);
391  }
392  }
393  return all_headers;
394 }
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
void GNUNET_async_scope_get(struct GNUNET_AsyncScopeSave *scope_ret)
Get the current async scope.
@ GNUNET_YES
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_free(ptr)
Wrapper around free.
char * GNUNET_STRINGS_data_to_string_alloc(const void *buf, size_t size)
Return the base32crockford encoding of the given buffer.
Definition: strings.c:763
static enum GNUNET_NetworkType scope
Which network scope do we belong to?
Identifier for an asynchronous execution context.
Saved async scope identifier or root scope.

References ctx, GNUNET_assert, GNUNET_async_scope_get(), GNUNET_free, GNUNET_STRINGS_data_to_string_alloc(), GNUNET_YES, and scope.

Referenced by GNUNET_CURL_job_add2(), and GNUNET_CURL_job_add_raw().

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 406 of file curl.c.

409 {
410  struct GNUNET_CURL_Job *job;
411 
412  if (CURLE_OK !=
413  curl_easy_setopt (eh,
414  CURLOPT_HTTPHEADER,
415  all_headers))
416  {
417  GNUNET_break (0);
418  curl_slist_free_all (all_headers);
419  curl_easy_cleanup (eh);
420  return NULL;
421  }
422  job = GNUNET_new (struct GNUNET_CURL_Job);
423  job->start_time = GNUNET_TIME_absolute_get ();
424  job->job_headers = all_headers;
425 
426  if ( (CURLE_OK !=
427  curl_easy_setopt (eh,
428  CURLOPT_PRIVATE,
429  job)) ||
430  (CURLE_OK !=
431  curl_easy_setopt (eh,
432  CURLOPT_WRITEFUNCTION,
433  &download_cb)) ||
434  (CURLE_OK !=
435  curl_easy_setopt (eh,
436  CURLOPT_WRITEDATA,
437  &job->db)) ||
438  (CURLE_OK !=
439  curl_easy_setopt (eh,
440  CURLOPT_SHARE,
441  ctx->share)) )
442  {
443  GNUNET_break (0);
444  GNUNET_free (job);
445  curl_easy_cleanup (eh);
446  return NULL;
447  }
448  if ( (CURLM_OK !=
449  curl_multi_add_handle (ctx->multi,
450  eh)) )
451  {
452  GNUNET_break (0);
453  GNUNET_free (job);
454  curl_easy_cleanup (eh);
455  return NULL;
456  }
457  job->easy_handle = eh;
458  job->ctx = ctx;
459  GNUNET_CONTAINER_DLL_insert (ctx->jobs_head,
460  ctx->jobs_tail,
461  job);
462  return job;
463 }
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:314
static struct GNUNET_SCHEDULER_Task * job
Task for main job.
Definition: gnunet-cadet.c:137
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:111
Jobs are CURL requests running within a struct GNUNET_CURL_Context.
Definition: curl.c:77

References ctx, download_cb(), GNUNET_break, GNUNET_CONTAINER_DLL_insert, GNUNET_free, GNUNET_new, GNUNET_TIME_absolute_get(), and job.

Referenced by GNUNET_CURL_job_add2(), and GNUNET_CURL_job_add_raw().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_json()

static bool is_json ( const char *  ct)
static

Test if the given content type ct is JSON.

Parameters
cta content type, e.g. "application/json; charset=UTF-8"
Returns
true if ct denotes JSON

Definition at line 621 of file curl.c.

622 {
623  const char *semi;
624 
625  /* check for "application/json" exact match */
626  if (0 == strcasecmp (ct,
627  "application/json"))
628  return true;
629  /* check for "application/json;[ANYTHING]" */
630  semi = strchr (ct,
631  ';');
632  /* also allow "application/json [ANYTHING]" (note the space!) */
633  if (NULL == semi)
634  semi = strchr (ct,
635  ' ');
636  if (NULL == semi)
637  return false; /* no delimiter we accept, forget it */
638  if (semi - ct != strlen ("application/json"))
639  return false; /* delimiter past desired length, forget it */
640  if (0 == strncasecmp (ct,
641  "application/json",
642  strlen ("application/json")))
643  return true; /* OK */
644  return false;
645 }

Referenced by GNUNET_CURL_append_header().

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 
)

Definition at line 649 of file curl.c.

652 {
653  json_t *json;
654  json_error_t error;
655  char *ct;
656 
657 #if DEBUG
659  "Downloaded body: %.*s\n",
660  (int) db->buf_size,
661  (char *) db->buf);
662 #endif
663  if (CURLE_OK !=
664  curl_easy_getinfo (eh,
665  CURLINFO_RESPONSE_CODE,
666  response_code))
667  {
668  /* unexpected error... */
669  GNUNET_break (0);
670  *response_code = 0;
671  }
672  if (MHD_HTTP_NO_CONTENT == *response_code)
673  return NULL;
674  if ((CURLE_OK !=
675  curl_easy_getinfo (eh,
676  CURLINFO_CONTENT_TYPE,
677  &ct)) ||
678  (NULL == ct) ||
679  (! is_json (ct)))
680  {
681  /* No content type or explicitly not JSON, refuse to parse
682  (but keep response code) */
683  if (0 != db->buf_size)
684  {
685  char *url;
686 
687  if (CURLE_OK !=
688  curl_easy_getinfo (eh,
689  CURLINFO_EFFECTIVE_URL,
690  &url))
691  url = "<unknown URL>";
693  "Request to `%s' was expected to return a body of type `application/json', got `%s'\n",
694  url,
695  ct);
696  }
697  return NULL;
698  }
699  if (0 == *response_code)
700  {
701  char *url;
702 
703  if (CURLE_OK !=
704  curl_easy_getinfo (eh,
705  CURLINFO_EFFECTIVE_URL,
706  &url))
707  url = "<unknown URL>";
709  "Failed to download response from `%s': \n",
710  url);
711  return NULL;
712  }
713  json = NULL;
714  if (0 == db->eno)
715  {
716  json = json_loadb (db->buf,
717  db->buf_size,
718  JSON_REJECT_DUPLICATES | JSON_DISABLE_EOF_CHECK,
719  &error);
720  if (NULL == json)
721  {
722  JSON_WARN (error);
723  *response_code = 0;
724  }
725  }
726  GNUNET_free (db->buf);
727  db->buf = NULL;
728  db->buf_size = 0;
729  return json;
730 }
static bool is_json(const char *ct)
Test if the given content type ct is JSON.
Definition: curl.c:621
#define JSON_WARN(error)
Print JSON parsing related error information.
Definition: curl.c:58
@ GNUNET_ERROR_TYPE_DEBUG
@ MHD_HTTP_NO_CONTENT
No Content [RFC7231, Section 6.3.5].

Referenced by GNUNET_CURL_gnunet_rc_create(), and GNUNET_CURL_perform().

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 880 of file curl.c.

882 {
883  CURLcode ret;
884 
885  if (CURLE_OK != (ret = curl_global_init (CURL_GLOBAL_DEFAULT)))
886  {
888  "curl_global_init",
889  ret);
890  curl_fail = 1;
891  }
892 }
#define CURL_STRERROR(type, function, code)
Log error related to CURL operations.
Definition: curl.c:47
static int curl_fail
Failsafe flag.
Definition: curl.c:71
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
@ GNUNET_ERROR_TYPE_ERROR

References curl_fail, CURL_STRERROR, GNUNET_ERROR_TYPE_ERROR, and ret.

◆ __attribute__() [2/2]

__attribute__ ( (destructor)  )

Cleans up after us, specifically runs the Curl cleanup.

Definition at line 898 of file curl.c.

900 {
901  if (curl_fail)
902  return;
903  curl_global_cleanup ();
904 }

References curl_fail.

Variable Documentation

◆ curl_fail

int curl_fail
static

Failsafe flag.

Raised if our constructor fails to initialize the Curl library.

Definition at line 71 of file curl.c.

Referenced by __attribute__(), and GNUNET_CURL_init().