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...
 
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 Taler interaction. More...
 
void GNUNET_CURL_perform (struct GNUNET_CURL_Context *ctx)
 Run the main event loop for the Taler 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 234 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 GNUNET_CURL_job_add2().

235 {
236  struct GNUNET_CURL_DownloadBuffer *db = cls;
237  size_t msize;
238  void *buf;
239 
240  if (0 == size * nitems)
241  {
242  /* Nothing (left) to do */
243  return 0;
244  }
245  msize = size * nitems;
246  if ((msize + db->buf_size) >= GNUNET_MAX_MALLOC_CHECKED)
247  {
248  db->eno = ENOMEM;
249  return 0; /* signals an error to curl */
250  }
251  db->buf = GNUNET_realloc (db->buf, db->buf_size + msize);
252  buf = db->buf + db->buf_size;
253  GNUNET_memcpy (buf, bufptr, msize);
254  db->buf_size += msize;
255  return msize;
256 }
#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:

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

437 {
438  json_t *json;
439  json_error_t error;
440  char *ct;
441 
443  "Downloaded body: %.*s\n",
444  (int) db->buf_size,
445  (char *) db->buf);
446 
447  if ((CURLE_OK != curl_easy_getinfo (eh, CURLINFO_CONTENT_TYPE, &ct)) ||
448  (NULL == ct) || (0 != strcasecmp (ct, "application/json")))
449  {
450  /* No content type or explicitly not JSON, refuse to parse
451  (but keep response code) */
452  if (CURLE_OK !=
453  curl_easy_getinfo (eh, CURLINFO_RESPONSE_CODE, response_code))
454  {
455  /* unexpected error... */
456  GNUNET_break (0);
457  *response_code = 0;
458  }
459  if (0 != db->buf_size)
461  "Did NOT detect response as JSON\n");
462  return NULL;
463  }
464  json = NULL;
465  if (0 == db->eno)
466  {
467  json = json_loadb (db->buf,
468  db->buf_size,
469  JSON_REJECT_DUPLICATES | JSON_DISABLE_EOF_CHECK,
470  &error);
471  if (NULL == json)
472  {
473  JSON_WARN (error);
474  *response_code = 0;
475  }
476  }
478  db->buf = NULL;
479  db->buf_size = 0;
480  if (NULL != json)
481  {
482  if (CURLE_OK !=
483  curl_easy_getinfo (eh, CURLINFO_RESPONSE_CODE, response_code))
484  {
485  /* unexpected error... */
486  GNUNET_break (0);
487  *response_code = 0;
488  }
489  }
490  return json;
491 }
#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 709 of file curl.c.

References curl_fail, CURL_STRERROR, GNUNET_ERROR_TYPE_ERROR, and ret.

711 {
712  CURLcode ret;
713 
714  if (CURLE_OK != (ret = curl_global_init (CURL_GLOBAL_DEFAULT)))
715  {
716  CURL_STRERROR (GNUNET_ERROR_TYPE_ERROR, "curl_global_init", ret);
717  curl_fail = 1;
718  }
719 }
#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 725 of file curl.c.

References curl_fail.

727 {
728  if (curl_fail)
729  return;
730  curl_global_cleanup ();
731 }
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().