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/>.
19 * @file src/include/gnunet_curl_lib.h
20 * @brief library to make it easy to download JSON replies over HTTP
21 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
22 * @author Christian Grothoff
24 * @defgroup curl CURL integration library
25 * Download JSON using libcurl.
28 #ifndef GNUNET_CURL_LIB_H
29 #define GNUNET_CURL_LIB_H
31 #include <curl/curl.h>
32 #elif HAVE_GNURL_CURL_H
33 #include <gnurl/curl.h>
35 #error "needs curl or gnurl"
38 #include "gnunet_util_lib.h"
42 * Function called by the context to ask for the event loop to be
43 * rescheduled, that is the application should call
44 * #GNUNET_CURL_get_select_info() as the set of sockets we care about
50 (*GNUNET_CURL_RescheduleCallback)(void *cls);
53 * @brief Buffer data structure we use to buffer the HTTP download
54 * before giving it to the JSON parser.
56 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).
79 * Parses the raw response we got from the Web server.
81 * @param db the raw data
83 * @param response_code HTTP response code
84 * @return the parsed object
87 (*GNUNET_CURL_RawParser) (struct GNUNET_CURL_DownloadBuffer *db,
92 * Deallocate the response.
94 * @param response object to clean
97 (*GNUNET_CURL_ResponseCleaner) (void *response);
100 * Initialise this library. This function should be called before using any of
101 * the following functions.
103 * @param cb function to call when rescheduling is required
104 * @param cb_cls closure for @a cb
105 * @return library context
107 struct GNUNET_CURL_Context *
108 GNUNET_CURL_init (GNUNET_CURL_RescheduleCallback cb,
113 * Obtain the information for a select() call to wait until
114 * #GNUNET_CURL_perform() is ready again.
116 * Basically, a client should use this API to prepare for select(),
117 * then block on select(), then call #GNUNET_CURL_perform() and then
118 * start again until the work with the context is done.
120 * This function will NOT zero out the sets and assumes that @a max_fd
121 * and @a timeout are already set to minimal applicable values. It is
122 * safe to give this API FD-sets and @a max_fd and @a timeout that are
123 * already initialized to some other descriptors that need to go into
126 * @param ctx context to get the event loop information for
127 * @param read_fd_set will be set for any pending read operations
128 * @param write_fd_set will be set for any pending write operations
129 * @param except_fd_set is here because curl_multi_fdset() has this argument
130 * @param max_fd set to the highest FD included in any set;
131 * if the existing sets have no FDs in it, the initial
132 * value should be "-1". (Note that `max_fd + 1` will need
133 * to be passed to select().)
134 * @param timeout set to the timeout in milliseconds (!); -1 means
135 * no timeout (NULL, blocking forever is OK), 0 means to
136 * proceed immediately with #GNUNET_CURL_perform().
139 GNUNET_CURL_get_select_info (struct GNUNET_CURL_Context *ctx,
141 fd_set *write_fd_set,
142 fd_set *except_fd_set,
148 * Add custom request header.
150 * @param ctx cURL context.
151 * @param header header string; will be given to the context AS IS.
152 * @return #GNUNET_OK if no errors occurred, #GNUNET_SYSERR otherwise.
155 GNUNET_CURL_append_header (struct GNUNET_CURL_Context *ctx,
159 * Run the main event loop for the CURL interaction.
161 * @param ctx the library context
164 GNUNET_CURL_perform (struct GNUNET_CURL_Context *ctx);
168 * Run the main event loop for the Taler interaction.
170 * @param ctx the library context
171 * @param rp parses the raw response returned from
173 * @param rc cleans/frees the response
176 GNUNET_CURL_perform2 (struct GNUNET_CURL_Context *ctx,
177 GNUNET_CURL_RawParser rp,
178 GNUNET_CURL_ResponseCleaner rc);
181 * Cleanup library initialisation resources. This function should be called
182 * after using this library to cleanup the resources occupied during library's
185 * @param ctx the library context
188 GNUNET_CURL_fini (struct GNUNET_CURL_Context *ctx);
192 * Entry in the context's job queue.
194 struct GNUNET_CURL_Job;
197 * Function to call upon completion of a job.
200 * @param response_code HTTP response code from server, 0 on hard error
201 * @param json response, NULL if response was not in JSON format
204 (*GNUNET_CURL_JobCompletionCallback)(void *cls,
206 const void *response);
210 * Schedule a CURL request to be executed and call the given @a jcc
211 * upon its completion. Note that the context will make use of the
212 * CURLOPT_PRIVATE facility of the CURL @a eh.
214 * This function modifies the CURL handle to add the
215 * "Content-Type: application/json" header if @a add_json is set.
217 * @param ctx context to execute the job in
218 * @param eh curl easy handle for the request, will
219 * be executed AND cleaned up
220 * @param add_json add "application/json" content type header
221 * @param jcc callback to invoke upon completion
222 * @param jcc_cls closure for @a jcc
223 * @return NULL on error (in this case, @eh is still released!)
225 struct GNUNET_CURL_Job *
226 GNUNET_CURL_job_add (struct GNUNET_CURL_Context *ctx,
229 GNUNET_CURL_JobCompletionCallback jcc,
234 * Cancel a job. Must only be called before the job completion
235 * callback is called for the respective job.
237 * @param job job to cancel
240 GNUNET_CURL_job_cancel (struct GNUNET_CURL_Job *job);
243 /* ******* GNUnet SCHEDULER integration ************ */
247 * Closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
249 struct GNUNET_CURL_RescheduleContext;
253 * Initialize reschedule context.
255 * @param ctx context to manage
256 * @return closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
258 struct GNUNET_CURL_RescheduleContext *
259 GNUNET_CURL_gnunet_rc_create (struct GNUNET_CURL_Context *ctx);
262 * Initialize reschedule context; with custom response parser
264 * @param ctx context to manage
265 * @return closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
267 struct GNUNET_CURL_RescheduleContext *
268 GNUNET_CURL_gnunet_rc_create_with_parser (struct GNUNET_CURL_Context *ctx,
269 GNUNET_CURL_RawParser rp,
270 GNUNET_CURL_ResponseCleaner rc);
274 * Destroy reschedule context.
276 * @param rc context to destroy
279 GNUNET_CURL_gnunet_rc_destroy (struct GNUNET_CURL_RescheduleContext *rc);
283 * Implementation of the #GNUNET_CURL_RescheduleCallback for GNUnet's
284 * scheduler. Will run the CURL context using GNUnet's scheduler.
285 * Note that you MUST immediately destroy the reschedule context after
286 * calling #GNUNET_CURL_fini().
288 * @param cls must point to a `struct GNUNET_CURL_RescheduleContext *`
289 * (pointer to a pointer!)
292 GNUNET_CURL_gnunet_scheduler_reschedule (void *cls);
296 /** @} */ /* end of group */
298 /* end of gnunet_curl_lib.h */