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

245 {
246  struct GNUNET_CURL_DownloadBuffer *db = cls;
247  size_t msize;
248  void *buf;
249 
250  if (0 == size * nitems)
251  {
252  /* Nothing (left) to do */
253  return 0;
254  }
255  msize = size * nitems;
256  if ((msize + db->buf_size) >= GNUNET_MAX_MALLOC_CHECKED)
257  {
258  db->eno = ENOMEM;
259  return 0; /* signals an error to curl */
260  }
261  db->buf = GNUNET_realloc (db->buf, db->buf_size + msize);
262  buf = db->buf + db->buf_size;
263  GNUNET_memcpy (buf, bufptr, msize);
264  db->buf_size += msize;
265  return msize;
266 }
#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 277 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().

279 {
280  struct curl_slist *all_headers = NULL;
281 
282  for (const struct curl_slist *curr = job_headers; curr != NULL;
283  curr = curr->next)
284  {
285  GNUNET_assert (NULL !=
286  (all_headers = curl_slist_append (all_headers, curr->data)));
287  }
288 
289  for (const struct curl_slist *curr = ctx->common_headers; curr != NULL;
290  curr = curr->next)
291  {
292  GNUNET_assert (NULL !=
293  (all_headers = curl_slist_append (all_headers, curr->data)));
294  }
295 
296  if (NULL != ctx->async_scope_id_header)
297  {
299 
301  if (GNUNET_YES == scope.have_scope)
302  {
303  char *aid_header = NULL;
304  aid_header =
306  sizeof(
307  struct GNUNET_AsyncScopeId));
308  GNUNET_assert (NULL != aid_header);
309  GNUNET_assert (NULL != curl_slist_append (all_headers, aid_header));
310  GNUNET_free (aid_header);
311  }
312  }
313  return all_headers;
314 }
#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:920
#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 326 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().

329 {
330  struct GNUNET_CURL_Job *job;
331 
332  if (CURLE_OK !=
333  curl_easy_setopt (eh, CURLOPT_HTTPHEADER, all_headers))
334  {
335  GNUNET_break (0);
336  curl_slist_free_all (all_headers);
337  curl_easy_cleanup (eh);
338  return NULL;
339  }
340  job = GNUNET_new (struct GNUNET_CURL_Job);
341  job->job_headers = all_headers;
342 
343  if ((CURLE_OK != curl_easy_setopt (eh, CURLOPT_PRIVATE, job)) ||
344  (CURLE_OK !=
345  curl_easy_setopt (eh, CURLOPT_WRITEFUNCTION, &download_cb)) ||
346  (CURLE_OK != curl_easy_setopt (eh, CURLOPT_WRITEDATA, &job->db)) ||
347  (CURLE_OK != curl_easy_setopt (eh, CURLOPT_SHARE, ctx->share)) ||
348  (CURLM_OK != curl_multi_add_handle (ctx->multi, eh)))
349  {
350  GNUNET_break (0);
351  GNUNET_free (job);
352  curl_easy_cleanup (eh);
353  return NULL;
354  }
355  job->easy_handle = eh;
356  job->ctx = ctx;
358  return job;
359 }
#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:244
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 525 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().

528 {
529  json_t *json;
530  json_error_t error;
531  char *ct;
532 
534  "Downloaded body: %.*s\n",
535  (int) db->buf_size,
536  (char *) db->buf);
537 
538  if ((CURLE_OK !=
539  curl_easy_getinfo (eh,
540  CURLINFO_CONTENT_TYPE,
541  &ct)) ||
542  (NULL == ct) ||
543  (0 != strcasecmp (ct,
544  "application/json")))
545  {
546  /* No content type or explicitly not JSON, refuse to parse
547  (but keep response code) */
548  if (CURLE_OK !=
549  curl_easy_getinfo (eh,
550  CURLINFO_RESPONSE_CODE,
551  response_code))
552  {
553  /* unexpected error... */
554  GNUNET_break (0);
555  *response_code = 0;
556  }
557  if (0 != db->buf_size)
559  "Did NOT detect response `%.*s' as JSON\n",
560  (int) db->buf_size,
561  (const char *) db->buf);
562  return NULL;
563  }
564  json = NULL;
565  if (0 == db->eno)
566  {
567  json = json_loadb (db->buf,
568  db->buf_size,
569  JSON_REJECT_DUPLICATES | JSON_DISABLE_EOF_CHECK,
570  &error);
571  if (NULL == json)
572  {
573  JSON_WARN (error);
574  *response_code = 0;
575  }
576  }
578  db->buf = NULL;
579  db->buf_size = 0;
580  if (NULL != json)
581  {
582  if (CURLE_OK !=
583  curl_easy_getinfo (eh,
584  CURLINFO_RESPONSE_CODE,
585  response_code))
586  {
587  /* unexpected error... */
588  GNUNET_break (0);
589  *response_code = 0;
590  }
591  }
592  return json;
593 }
#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 841 of file curl.c.

References curl_fail, CURL_STRERROR, GNUNET_ERROR_TYPE_ERROR, and ret.

843 {
844  CURLcode ret;
845 
846  if (CURLE_OK != (ret = curl_global_init (CURL_GLOBAL_DEFAULT)))
847  {
848  CURL_STRERROR (GNUNET_ERROR_TYPE_ERROR, "curl_global_init", ret);
849  curl_fail = 1;
850  }
851 }
#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 857 of file curl.c.

References curl_fail.

859 {
860  if (curl_fail)
861  return;
862  curl_global_cleanup ();
863 }
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().