aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMarcello Stanisci <stanisci.m@gmail.com>2018-10-17 12:33:50 +0200
committerMarcello Stanisci <stanisci.m@gmail.com>2018-10-17 12:33:50 +0200
commit25b9d3dfa6fb538627e6cd8ed4b5741e18b752d9 (patch)
tree6a68f3bbac5ae7b438d6cfd99997e9aa180a408c /src
parent564df39557f1a69280f36ff5173807856ff41067 (diff)
downloadgnunet-25b9d3dfa6fb538627e6cd8ed4b5741e18b752d9.tar.gz
gnunet-25b9d3dfa6fb538627e6cd8ed4b5741e18b752d9.zip
curl: abstracting response's parser & cleaner.
Diffstat (limited to 'src')
-rw-r--r--src/curl/curl.c75
-rw-r--r--src/include/gnunet_curl_lib.h59
2 files changed, 93 insertions, 41 deletions
diff --git a/src/curl/curl.c b/src/curl/curl.c
index ec65986cb..eabe0e787 100644
--- a/src/curl/curl.c
+++ b/src/curl/curl.c
@@ -62,33 +62,6 @@
62 */ 62 */
63static int curl_fail; 63static int curl_fail;
64 64
65
66/**
67 * @brief Buffer data structure we use to buffer the HTTP download
68 * before giving it to the JSON parser.
69 */
70struct DownloadBuffer
71{
72
73 /**
74 * Download buffer
75 */
76 void *buf;
77
78 /**
79 * The size of the download buffer
80 */
81 size_t buf_size;
82
83 /**
84 * Error code (based on libc errno) if we failed to download
85 * (i.e. response too large).
86 */
87 int eno;
88
89};
90
91
92/** 65/**
93 * Jobs are CURL requests running within a `struct GNUNET_CURL_Context`. 66 * Jobs are CURL requests running within a `struct GNUNET_CURL_Context`.
94 */ 67 */
@@ -128,7 +101,7 @@ struct GNUNET_CURL_Job
128 /** 101 /**
129 * Buffer for response received from CURL. 102 * Buffer for response received from CURL.
130 */ 103 */
131 struct DownloadBuffer db; 104 struct GNUNET_CURL_DownloadBuffer db;
132 105
133}; 106};
134 107
@@ -242,7 +215,7 @@ download_cb (char *bufptr,
242 size_t nitems, 215 size_t nitems,
243 void *cls) 216 void *cls)
244{ 217{
245 struct DownloadBuffer *db = cls; 218 struct GNUNET_CURL_DownloadBuffer *db = cls;
246 size_t msize; 219 size_t msize;
247 void *buf; 220 void *buf;
248 221
@@ -380,10 +353,10 @@ GNUNET_CURL_job_cancel (struct GNUNET_CURL_Job *job)
380 * (or zero if we aborted the download, i.e. 353 * (or zero if we aborted the download, i.e.
381 * because the response was too big, or if 354 * because the response was too big, or if
382 * the JSON we received was malformed). 355 * the JSON we received was malformed).
383 * @return NULL if downloading a JSON reply failed 356 * @return NULL if downloading a JSON reply failed.
384 */ 357 */
385static json_t * 358static void *
386download_get_result (struct DownloadBuffer *db, 359download_get_result (struct GNUNET_CURL_DownloadBuffer *db,
387 CURL *eh, 360 CURL *eh,
388 long *response_code) 361 long *response_code)
389{ 362{
@@ -451,7 +424,6 @@ download_get_result (struct DownloadBuffer *db,
451 return json; 424 return json;
452} 425}
453 426
454
455/** 427/**
456 * Add custom request header. 428 * Add custom request header.
457 * 429 *
@@ -475,16 +447,21 @@ GNUNET_CURL_append_header (struct GNUNET_CURL_Context *ctx,
475 * Run the main event loop for the Taler interaction. 447 * Run the main event loop for the Taler interaction.
476 * 448 *
477 * @param ctx the library context 449 * @param ctx the library context
450 * @param rp parses the raw response returned from
451 * the Web server.
452 * @param rc cleans/frees the response
478 */ 453 */
479void 454void
480GNUNET_CURL_perform (struct GNUNET_CURL_Context *ctx) 455GNUNET_CURL_perform2 (struct GNUNET_CURL_Context *ctx,
456 GNUNET_CURL_RawParser rp,
457 GNUNET_CURL_ResponseCleaner rc)
481{ 458{
482 CURLMsg *cmsg; 459 CURLMsg *cmsg;
483 struct GNUNET_CURL_Job *job; 460 struct GNUNET_CURL_Job *job;
484 int n_running; 461 int n_running;
485 int n_completed; 462 int n_completed;
486 long response_code; 463 long response_code;
487 json_t *j; 464 void *response;
488 465
489 (void) curl_multi_perform (ctx->multi, 466 (void) curl_multi_perform (ctx->multi,
490 &n_running); 467 &n_running);
@@ -498,10 +475,10 @@ GNUNET_CURL_perform (struct GNUNET_CURL_Context *ctx)
498 CURLINFO_PRIVATE, 475 CURLINFO_PRIVATE,
499 (char **) &job)); 476 (char **) &job));
500 GNUNET_assert (job->ctx == ctx); 477 GNUNET_assert (job->ctx == ctx);
501 response_code = 0; 478 response_code = 0 ;
502 j = download_get_result (&job->db, 479 response = rp (&job->db,
503 job->easy_handle, 480 job->easy_handle,
504 &response_code); 481 &response_code);
505#if ENABLE_BENCHMARK 482#if ENABLE_BENCHMARK
506 { 483 {
507 char *url = NULL; 484 char *url = NULL;
@@ -521,14 +498,30 @@ GNUNET_CURL_perform (struct GNUNET_CURL_Context *ctx)
521#endif 498#endif
522 job->jcc (job->jcc_cls, 499 job->jcc (job->jcc_cls,
523 response_code, 500 response_code,
524 j); 501 /* NOTE: jcc is now in charge of decref-ing */
525 json_decref (j); 502 response);
503 rc (response);
526 GNUNET_CURL_job_cancel (job); 504 GNUNET_CURL_job_cancel (job);
527 } 505 }
528} 506}
529 507
530 508
531/** 509/**
510 * Run the main event loop for the Taler interaction.
511 *
512 * @param ctx the library context
513 */
514void
515GNUNET_CURL_perform (struct GNUNET_CURL_Context *ctx)
516{
517
518 GNUNET_CURL_perform2 (ctx,
519 download_get_result,
520 (GNUNET_CURL_ResponseCleaner) &json_decref);
521}
522
523
524/**
532 * Obtain the information for a select() call to wait until 525 * Obtain the information for a select() call to wait until
533 * #GNUNET_CURL_perform() is ready again. Note that calling 526 * #GNUNET_CURL_perform() is ready again. Note that calling
534 * any other GNUNET_CURL-API may also imply that the library 527 * any other GNUNET_CURL-API may also imply that the library
diff --git a/src/include/gnunet_curl_lib.h b/src/include/gnunet_curl_lib.h
index 2b2442a52..9bf798bca 100644
--- a/src/include/gnunet_curl_lib.h
+++ b/src/include/gnunet_curl_lib.h
@@ -49,6 +49,52 @@
49typedef void 49typedef void
50(*GNUNET_CURL_RescheduleCallback)(void *cls); 50(*GNUNET_CURL_RescheduleCallback)(void *cls);
51 51
52/**
53 * @brief Buffer data structure we use to buffer the HTTP download
54 * before giving it to the JSON parser.
55 */
56struct GNUNET_CURL_DownloadBuffer
57{
58
59 /**
60 * Download buffer
61 */
62 void *buf;
63
64 /**
65 * The size of the download buffer
66 */
67 size_t buf_size;
68
69 /**
70 * Error code (based on libc errno) if we failed to download
71 * (i.e. response too large).
72 */
73 int eno;
74
75};
76
77
78/**
79 * Parses the raw response we got from the Web server.
80 *
81 * @param db the raw data
82 * @param eh handle
83 * @param response_code HTTP response code
84 * @return the parsed object
85 */
86typedef void *
87(*GNUNET_CURL_RawParser) (struct GNUNET_CURL_DownloadBuffer *db,
88 CURL *eh,
89 long *response_code);
90
91/**
92 * Deallocate the response.
93 *
94 * @param response object to clean
95 */
96typedef void
97(*GNUNET_CURL_ResponseCleaner) (void *response);
52 98
53/** 99/**
54 * Initialise this library. This function should be called before using any of 100 * Initialise this library. This function should be called before using any of
@@ -119,6 +165,19 @@ GNUNET_CURL_perform (struct GNUNET_CURL_Context *ctx);
119 165
120 166
121/** 167/**
168 * Run the main event loop for the Taler interaction.
169 *
170 * @param ctx the library context
171 * @param rp parses the raw response returned from
172 * the Web server.
173 * @param rc cleans/frees the response
174 */
175void
176GNUNET_CURL_perform2 (struct GNUNET_CURL_Context *ctx,
177 GNUNET_CURL_RawParser rp,
178 GNUNET_CURL_ResponseCleaner rc);
179
180/**
122 * Cleanup library initialisation resources. This function should be called 181 * Cleanup library initialisation resources. This function should be called
123 * after using this library to cleanup the resources occupied during library's 182 * after using this library to cleanup the resources occupied during library's
124 * initialisation. 183 * initialisation.