2 This file is part of GNUnet
3 Copyright (C) 2014, 2015, 2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file src/include/gnunet_curl_lib.h
22 * @brief library to make it easy to download JSON replies over HTTP
23 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
24 * @author Christian Grothoff
26 * @defgroup curl CURL integration library
27 * Download JSON using libcurl.
30 #ifndef GNUNET_CURL_LIB_H
31 #define GNUNET_CURL_LIB_H
33 #include <curl/curl.h>
34 #elif HAVE_GNURL_CURL_H
35 #include <gnurl/curl.h>
37 #error "needs curl or gnurl"
40 #include "gnunet_util_lib.h"
44 * Function called by the context to ask for the event loop to be
45 * rescheduled, that is the application should call
46 * #GNUNET_CURL_get_select_info() as the set of sockets we care about
52 (*GNUNET_CURL_RescheduleCallback)(void *cls);
55 * @brief Buffer data structure we use to buffer the HTTP download
56 * before giving it to the JSON parser.
58 struct GNUNET_CURL_DownloadBuffer
67 * The size of the download buffer
72 * Error code (based on libc errno) if we failed to download
73 * (i.e. response too large).
81 * Parses the raw response we got from the Web server.
83 * @param db the raw data
85 * @param response_code HTTP response code
86 * @return the parsed object
89 (*GNUNET_CURL_RawParser) (struct GNUNET_CURL_DownloadBuffer *db,
94 * Deallocate the response.
96 * @param response object to clean
99 (*GNUNET_CURL_ResponseCleaner) (void *response);
102 * Initialise this library. This function should be called before using any of
103 * the following functions.
105 * @param cb function to call when rescheduling is required
106 * @param cb_cls closure for @a cb
107 * @return library context
109 struct GNUNET_CURL_Context *
110 GNUNET_CURL_init (GNUNET_CURL_RescheduleCallback cb,
115 * Obtain the information for a select() call to wait until
116 * #GNUNET_CURL_perform() is ready again.
118 * Basically, a client should use this API to prepare for select(),
119 * then block on select(), then call #GNUNET_CURL_perform() and then
120 * start again until the work with the context is done.
122 * This function will NOT zero out the sets and assumes that @a max_fd
123 * and @a timeout are already set to minimal applicable values. It is
124 * safe to give this API FD-sets and @a max_fd and @a timeout that are
125 * already initialized to some other descriptors that need to go into
128 * @param ctx context to get the event loop information for
129 * @param read_fd_set will be set for any pending read operations
130 * @param write_fd_set will be set for any pending write operations
131 * @param except_fd_set is here because curl_multi_fdset() has this argument
132 * @param max_fd set to the highest FD included in any set;
133 * if the existing sets have no FDs in it, the initial
134 * value should be "-1". (Note that `max_fd + 1` will need
135 * to be passed to select().)
136 * @param timeout set to the timeout in milliseconds (!); -1 means
137 * no timeout (NULL, blocking forever is OK), 0 means to
138 * proceed immediately with #GNUNET_CURL_perform().
141 GNUNET_CURL_get_select_info (struct GNUNET_CURL_Context *ctx,
143 fd_set *write_fd_set,
144 fd_set *except_fd_set,
150 * Add custom request header.
152 * @param ctx cURL context.
153 * @param header header string; will be given to the context AS IS.
154 * @return #GNUNET_OK if no errors occurred, #GNUNET_SYSERR otherwise.
157 GNUNET_CURL_append_header (struct GNUNET_CURL_Context *ctx,
161 * Run the main event loop for the CURL interaction.
163 * @param ctx the library context
166 GNUNET_CURL_perform (struct GNUNET_CURL_Context *ctx);
170 * Run the main event loop for the Taler interaction.
172 * @param ctx the library context
173 * @param rp parses the raw response returned from
175 * @param rc cleans/frees the response
178 GNUNET_CURL_perform2 (struct GNUNET_CURL_Context *ctx,
179 GNUNET_CURL_RawParser rp,
180 GNUNET_CURL_ResponseCleaner rc);
183 * Cleanup library initialisation resources. This function should be called
184 * after using this library to cleanup the resources occupied during library's
187 * @param ctx the library context
190 GNUNET_CURL_fini (struct GNUNET_CURL_Context *ctx);
194 * Entry in the context's job queue.
196 struct GNUNET_CURL_Job;
199 * Function to call upon completion of a job.
202 * @param response_code HTTP response code from server, 0 on hard error
203 * @param json response, NULL if response was not in JSON format
206 (*GNUNET_CURL_JobCompletionCallback)(void *cls,
208 const void *response);
212 * Schedule a CURL request to be executed and call the given @a jcc
213 * upon its completion. Note that the context will make use of the
214 * CURLOPT_PRIVATE facility of the CURL @a eh.
216 * This function modifies the CURL handle to add the
217 * "Content-Type: application/json" header if @a add_json is set.
219 * @param ctx context to execute the job in
220 * @param eh curl easy handle for the request, will
221 * be executed AND cleaned up
222 * @param add_json add "application/json" content type header
223 * @param jcc callback to invoke upon completion
224 * @param jcc_cls closure for @a jcc
225 * @return NULL on error (in this case, @eh is still released!)
227 struct GNUNET_CURL_Job *
228 GNUNET_CURL_job_add (struct GNUNET_CURL_Context *ctx,
231 GNUNET_CURL_JobCompletionCallback jcc,
236 * Cancel a job. Must only be called before the job completion
237 * callback is called for the respective job.
239 * @param job job to cancel
242 GNUNET_CURL_job_cancel (struct GNUNET_CURL_Job *job);
245 /* ******* GNUnet SCHEDULER integration ************ */
249 * Closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
251 struct GNUNET_CURL_RescheduleContext;
255 * Initialize reschedule context.
257 * @param ctx context to manage
258 * @return closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
260 struct GNUNET_CURL_RescheduleContext *
261 GNUNET_CURL_gnunet_rc_create (struct GNUNET_CURL_Context *ctx);
264 * Initialize reschedule context; with custom response parser
266 * @param ctx context to manage
267 * @return closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
269 struct GNUNET_CURL_RescheduleContext *
270 GNUNET_CURL_gnunet_rc_create_with_parser (struct GNUNET_CURL_Context *ctx,
271 GNUNET_CURL_RawParser rp,
272 GNUNET_CURL_ResponseCleaner rc);
276 * Destroy reschedule context.
278 * @param rc context to destroy
281 GNUNET_CURL_gnunet_rc_destroy (struct GNUNET_CURL_RescheduleContext *rc);
285 * Implementation of the #GNUNET_CURL_RescheduleCallback for GNUnet's
286 * scheduler. Will run the CURL context using GNUnet's scheduler.
287 * Note that you MUST immediately destroy the reschedule context after
288 * calling #GNUNET_CURL_fini().
290 * @param cls must point to a `struct GNUNET_CURL_RescheduleContext *`
291 * (pointer to a pointer!)
294 GNUNET_CURL_gnunet_scheduler_reschedule (void *cls);
298 /** @} */ /* end of group */
300 /* end of gnunet_curl_lib.h */