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>
35 #include <gnurl/curl.h>
37 #error "needs curl or gnurl"
39 #include "gnunet_util_lib.h"
43 * Function called by the context to ask for the event loop to be
44 * rescheduled, that is the application should call
45 * #GNUNET_CURL_get_select_info() as the set of sockets we care about
51 (*GNUNET_CURL_RescheduleCallback)(void *cls);
54 * @brief Buffer data structure we use to buffer the HTTP download
55 * before giving it to the JSON parser.
57 struct GNUNET_CURL_DownloadBuffer
65 * The size of the download buffer
70 * Error code (based on libc errno) if we failed to download
71 * (i.e. response too large).
78 * Parses the raw response we got from the Web server.
80 * @param db the raw data
82 * @param response_code HTTP response code
83 * @return the parsed object
86 (*GNUNET_CURL_RawParser) (struct GNUNET_CURL_DownloadBuffer *db,
91 * Deallocate the response.
93 * @param response object to clean
96 (*GNUNET_CURL_ResponseCleaner) (void *response);
99 * Initialise this library. This function should be called before using any of
100 * the following functions.
102 * @param cb function to call when rescheduling is required
103 * @param cb_cls closure for @a cb
104 * @return library context
106 struct GNUNET_CURL_Context *
107 GNUNET_CURL_init (GNUNET_CURL_RescheduleCallback cb,
112 * Obtain the information for a select() call to wait until
113 * #GNUNET_CURL_perform() is ready again.
115 * Basically, a client should use this API to prepare for select(),
116 * then block on select(), then call #GNUNET_CURL_perform() and then
117 * start again until the work with the context is done.
119 * This function will NOT zero out the sets and assumes that @a max_fd
120 * and @a timeout are already set to minimal applicable values. It is
121 * safe to give this API FD-sets and @a max_fd and @a timeout that are
122 * already initialized to some other descriptors that need to go into
125 * @param ctx context to get the event loop information for
126 * @param read_fd_set will be set for any pending read operations
127 * @param write_fd_set will be set for any pending write operations
128 * @param except_fd_set is here because curl_multi_fdset() has this argument
129 * @param max_fd set to the highest FD included in any set;
130 * if the existing sets have no FDs in it, the initial
131 * value should be "-1". (Note that `max_fd + 1` will need
132 * to be passed to select().)
133 * @param timeout set to the timeout in milliseconds (!); -1 means
134 * no timeout (NULL, blocking forever is OK), 0 means to
135 * proceed immediately with #GNUNET_CURL_perform().
138 GNUNET_CURL_get_select_info (struct GNUNET_CURL_Context *ctx,
140 fd_set *write_fd_set,
141 fd_set *except_fd_set,
147 * Add custom request header.
149 * @param ctx cURL context.
150 * @param header header string; will be given to the context AS IS.
151 * @return #GNUNET_OK if no errors occurred, #GNUNET_SYSERR otherwise.
154 GNUNET_CURL_append_header (struct GNUNET_CURL_Context *ctx,
158 * Run the main event loop for the CURL interaction.
160 * @param ctx the library context
163 GNUNET_CURL_perform (struct GNUNET_CURL_Context *ctx);
167 * Run the main event loop for the HTTP interaction.
169 * @param ctx the library context
170 * @param rp parses the raw response returned from
172 * @param rc cleans/frees the response
175 GNUNET_CURL_perform2 (struct GNUNET_CURL_Context *ctx,
176 GNUNET_CURL_RawParser rp,
177 GNUNET_CURL_ResponseCleaner rc);
180 * Cleanup library initialisation resources. This function should be called
181 * after using this library to cleanup the resources occupied during library's
184 * @param ctx the library context
187 GNUNET_CURL_fini (struct GNUNET_CURL_Context *ctx);
191 * Entry in the context's job queue.
193 struct GNUNET_CURL_Job;
196 * Function to call upon completion of a job.
199 * @param response_code HTTP response code from server, 0 on hard error
200 * @param response in JSON, NULL if response was not in JSON format
203 (*GNUNET_CURL_JobCompletionCallback)(void *cls,
205 const void *response);
209 * Function to call upon completion of a raw job.
212 * @param response_code HTTP response code from server, 0 on hard error
213 * @param body http body of the response
214 * @param body_size number of bytes in @a body
217 (*GNUNET_CURL_RawJobCompletionCallback)(void *cls,
224 * Schedule a CURL request to be executed and call the given @a jcc
225 * upon its completion. Note that the context will make use of the
226 * CURLOPT_PRIVATE facility of the CURL @a eh.
228 * This function modifies the CURL handle to add the
229 * "Content-Type: application/json" header if @a add_json is set.
231 * @param ctx context to execute the job in
232 * @param eh curl easy handle for the request, will
233 * be executed AND cleaned up
234 * @param add_json add "application/json" content type header
235 * @param jcc callback to invoke upon completion
236 * @param jcc_cls closure for @a jcc
237 * @return NULL on error (in this case, @eh is still released!)
239 struct GNUNET_CURL_Job *
240 GNUNET_CURL_job_add (struct GNUNET_CURL_Context *ctx,
243 GNUNET_CURL_JobCompletionCallback jcc,
248 * Schedule a CURL request to be executed and call the given @a jcc
249 * upon its completion. Note that the context will make use of the
250 * CURLOPT_PRIVATE facility of the CURL @a eh.
252 * This function modifies the CURL handle to add the
253 * "Content-Type: application/json" header if @a add_json is set.
255 * @param ctx context to execute the job in
256 * @param eh curl easy handle for the request, will
257 * be executed AND cleaned up
258 * @param job_headers extra headers to add for this request
259 * @param jcc callback to invoke upon completion
260 * @param jcc_cls closure for @a jcc
261 * @return NULL on error (in this case, @eh is still released!)
263 struct GNUNET_CURL_Job *
264 GNUNET_CURL_job_add2 (struct GNUNET_CURL_Context *ctx,
266 const struct curl_slist *job_headers,
267 GNUNET_CURL_JobCompletionCallback jcc,
272 * Schedule a CURL request to be executed and call the given @a jcc
273 * upon its completion. Note that the context will make use of the
274 * CURLOPT_PRIVATE facility of the CURL @a eh. Used to download
275 * resources that are NOT in JSON. The raw body will be returned.
277 * @param ctx context to execute the job in
278 * @param eh curl easy handle for the request, will
279 * be executed AND cleaned up
280 * @param job_headers extra headers to add for this request
281 * @param max_reply_size largest acceptable response body
282 * @param jcc callback to invoke upon completion
283 * @param jcc_cls closure for @a jcc
284 * @return NULL on error (in this case, @eh is still released!)
286 struct GNUNET_CURL_Job *
287 GNUNET_CURL_job_add_raw (struct GNUNET_CURL_Context *ctx,
289 const struct curl_slist *job_headers,
290 GNUNET_CURL_RawJobCompletionCallback jcc,
295 * Cancel a job. Must only be called before the job completion
296 * callback is called for the respective job.
298 * @param job job to cancel
301 GNUNET_CURL_job_cancel (struct GNUNET_CURL_Job *job);
304 /* ******* GNUnet SCHEDULER integration ************ */
308 * Closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
310 struct GNUNET_CURL_RescheduleContext;
314 * Initialize reschedule context.
316 * @param ctx context to manage
317 * @return closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
319 struct GNUNET_CURL_RescheduleContext *
320 GNUNET_CURL_gnunet_rc_create (struct GNUNET_CURL_Context *ctx);
323 * Initialize reschedule context; with custom response parser
325 * @param ctx context to manage
326 * @return closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
328 struct GNUNET_CURL_RescheduleContext *
329 GNUNET_CURL_gnunet_rc_create_with_parser (struct GNUNET_CURL_Context *ctx,
330 GNUNET_CURL_RawParser rp,
331 GNUNET_CURL_ResponseCleaner rc);
335 * Destroy reschedule context.
337 * @param rc context to destroy
340 GNUNET_CURL_gnunet_rc_destroy (struct GNUNET_CURL_RescheduleContext *rc);
344 * Implementation of the #GNUNET_CURL_RescheduleCallback for GNUnet's
345 * scheduler. Will run the CURL context using GNUnet's scheduler.
346 * Note that you MUST immediately destroy the reschedule context after
347 * calling #GNUNET_CURL_fini().
349 * @param cls must point to a `struct GNUNET_CURL_RescheduleContext *`
350 * (pointer to a pointer!)
353 GNUNET_CURL_gnunet_scheduler_reschedule (void *cls);
357 * Enable sending the async scope ID as a header.
359 * @param ctx the context to enable this for
360 * @param header_name name of the header to send.
363 GNUNET_CURL_enable_async_scope_header (struct GNUNET_CURL_Context *ctx,
364 const char *header_name);
368 * Return #GNUNET_YES if given a valid scope ID and
369 * #GNUNET_NO otherwise. See
370 * #GNUNET_CURL_enable_async_scope_header() for the
371 * code that generates such a @a scope_id in an HTTP
374 * @returns #GNUNET_YES iff given a valid scope ID
377 GNUNET_CURL_is_valid_scope_id (const char *scope_id);
381 /** @} */ /* end of group */
383 /* end of gnunet_curl_lib.h */