GNUnet  0.19.3
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...
 
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...
 
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 309 of file curl.c.

313 {
314  struct GNUNET_CURL_DownloadBuffer *db = cls;
315  size_t msize;
316  void *buf;
317 
318  if (0 == size * nitems)
319  {
320  /* Nothing (left) to do */
321  return 0;
322  }
323  msize = size * nitems;
324  if ((msize + db->buf_size) >= GNUNET_MAX_MALLOC_CHECKED)
325  {
326  db->eno = ENOMEM;
327  return 0; /* signals an error to curl */
328  }
329  db->buf = GNUNET_realloc (db->buf,
330  db->buf_size + msize);
331  buf = db->buf + db->buf_size;
332  GNUNET_memcpy (buf, bufptr, msize);
333  db->buf_size += msize;
334  return msize;
335 }
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 346 of file curl.c.

348 {
349  struct curl_slist *all_headers = NULL;
350 
351  for (const struct curl_slist *curr = job_headers;
352  NULL != curr;
353  curr = curr->next)
354  {
355  GNUNET_assert (NULL !=
356  (all_headers = curl_slist_append (all_headers,
357  curr->data)));
358  }
359 
360  for (const struct curl_slist *curr = ctx->common_headers;
361  NULL != curr;
362  curr = curr->next)
363  {
364  GNUNET_assert (NULL !=
365  (all_headers = curl_slist_append (all_headers,
366  curr->data)));
367  }
368 
369  if (NULL != ctx->async_scope_id_header)
370  {
372 
374  if (GNUNET_YES == scope.have_scope)
375  {
376  char *aid_header;
377 
378  aid_header =
380  &scope.scope_id,
381  sizeof(struct GNUNET_AsyncScopeId));
382  GNUNET_assert (NULL != aid_header);
383  GNUNET_assert (NULL != curl_slist_append (all_headers,
384  aid_header));
385  GNUNET_free (aid_header);
386  }
387  }
388  return all_headers;
389 }
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 401 of file curl.c.

404 {
405  struct GNUNET_CURL_Job *job;
406 
407  if (CURLE_OK !=
408  curl_easy_setopt (eh,
409  CURLOPT_HTTPHEADER,
410  all_headers))
411  {
412  GNUNET_break (0);
413  curl_slist_free_all (all_headers);
414  curl_easy_cleanup (eh);
415  return NULL;
416  }
417  job = GNUNET_new (struct GNUNET_CURL_Job);
418  job->job_headers = all_headers;
419 
420  if ( (CURLE_OK !=
421  curl_easy_setopt (eh,
422  CURLOPT_PRIVATE,
423  job)) ||
424  (CURLE_OK !=
425  curl_easy_setopt (eh,
426  CURLOPT_WRITEFUNCTION,
427  &download_cb)) ||
428  (CURLE_OK !=
429  curl_easy_setopt (eh,
430  CURLOPT_WRITEDATA,
431  &job->db)) ||
432  (CURLE_OK !=
433  curl_easy_setopt (eh,
434  CURLOPT_SHARE,
435  ctx->share)) )
436  {
437  GNUNET_break (0);
438  GNUNET_free (job);
439  curl_easy_cleanup (eh);
440  return NULL;
441  }
442  if ( (CURLM_OK !=
443  curl_multi_add_handle (ctx->multi,
444  eh)) )
445  {
446  GNUNET_break (0);
447  GNUNET_free (job);
448  curl_easy_cleanup (eh);
449  return NULL;
450  }
451  job->easy_handle = eh;
452  job->ctx = ctx;
453  GNUNET_CONTAINER_DLL_insert (ctx->jobs_head,
454  ctx->jobs_tail,
455  job);
456  return job;
457 }
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:309
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.
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, 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 615 of file curl.c.

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

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

646 {
647  json_t *json;
648  json_error_t error;
649  char *ct;
650 
651 #if DEBUG
653  "Downloaded body: %.*s\n",
654  (int) db->buf_size,
655  (char *) db->buf);
656 #endif
657  if (CURLE_OK !=
658  curl_easy_getinfo (eh,
659  CURLINFO_RESPONSE_CODE,
660  response_code))
661  {
662  /* unexpected error... */
663  GNUNET_break (0);
664  *response_code = 0;
665  }
666  if ((CURLE_OK !=
667  curl_easy_getinfo (eh,
668  CURLINFO_CONTENT_TYPE,
669  &ct)) ||
670  (NULL == ct) ||
671  (! is_json (ct)))
672  {
673  /* No content type or explicitly not JSON, refuse to parse
674  (but keep response code) */
675  if (0 != db->buf_size)
677  "Did NOT detect response `%.*s' as JSON\n",
678  (int) db->buf_size,
679  (const char *) db->buf);
680  return NULL;
681  }
682  if (MHD_HTTP_NO_CONTENT == *response_code)
683  return NULL;
684  if (0 == *response_code)
685  {
686  char *url;
687 
688  if (CURLE_OK !=
689  curl_easy_getinfo (eh,
690  CURLINFO_EFFECTIVE_URL,
691  &url))
692  url = "<unknown URL>";
694  "Failed to download response from `%s': \n",
695  url);
696  return NULL;
697  }
698  json = NULL;
699  if (0 == db->eno)
700  {
701  json = json_loadb (db->buf,
702  db->buf_size,
703  JSON_REJECT_DUPLICATES | JSON_DISABLE_EOF_CHECK,
704  &error);
705  if (NULL == json)
706  {
707  JSON_WARN (error);
708  *response_code = 0;
709  }
710  }
711  GNUNET_free (db->buf);
712  db->buf = NULL;
713  db->buf_size = 0;
714  return json;
715 }
static bool is_json(const char *ct)
Test if the given content type ct is JSON.
Definition: curl.c:615
#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 847 of file curl.c.

849 {
850  CURLcode ret;
851 
852  if (CURLE_OK != (ret = curl_global_init (CURL_GLOBAL_DEFAULT)))
853  {
855  "curl_global_init",
856  ret);
857  curl_fail = 1;
858  }
859 }
#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 865 of file curl.c.

867 {
868  if (curl_fail)
869  return;
870  curl_global_cleanup ();
871 }

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().