GNUnet  0.10.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 232 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().

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

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

References curl_fail, CURL_STRERROR, GNUNET_ERROR_TYPE_ERROR, and ret.

709 {
710  CURLcode ret;
711 
712  if (CURLE_OK != (ret = curl_global_init(CURL_GLOBAL_DEFAULT)))
713  {
714  CURL_STRERROR(GNUNET_ERROR_TYPE_ERROR, "curl_global_init", ret);
715  curl_fail = 1;
716  }
717 }
#define CURL_STRERROR(type, function, code)
Log error related to CURL operations.
Definition: curl.c:42
static int ret
Final status code.
Definition: gnunet-arm.c:89
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 723 of file curl.c.

References curl_fail.

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