diff options
author | Marcello Stanisci <stanisci.m@gmail.com> | 2018-10-17 12:33:50 +0200 |
---|---|---|
committer | Marcello Stanisci <stanisci.m@gmail.com> | 2018-10-17 12:33:50 +0200 |
commit | 25b9d3dfa6fb538627e6cd8ed4b5741e18b752d9 (patch) | |
tree | 6a68f3bbac5ae7b438d6cfd99997e9aa180a408c /src/curl | |
parent | 564df39557f1a69280f36ff5173807856ff41067 (diff) | |
download | gnunet-25b9d3dfa6fb538627e6cd8ed4b5741e18b752d9.tar.gz gnunet-25b9d3dfa6fb538627e6cd8ed4b5741e18b752d9.zip |
curl: abstracting response's parser & cleaner.
Diffstat (limited to 'src/curl')
-rw-r--r-- | src/curl/curl.c | 75 |
1 files changed, 34 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 | */ |
63 | static int curl_fail; | 63 | static 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 | */ | ||
70 | struct 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 | */ |
385 | static json_t * | 358 | static void * |
386 | download_get_result (struct DownloadBuffer *db, | 359 | download_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 | */ |
479 | void | 454 | void |
480 | GNUNET_CURL_perform (struct GNUNET_CURL_Context *ctx) | 455 | GNUNET_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 | */ | ||
514 | void | ||
515 | GNUNET_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 |