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 under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3, or (at your option) any later version.
9 GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License along with
14 GNUnet; see the file COPYING. If not, If not, see
15 <http://www.gnu.org/licenses/>
18 * @file src/include/gnunet_curl_lib.h
19 * @brief library to make it easy to download JSON replies over HTTP
20 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
21 * @author Christian Grothoff
23 * @defgroup curl CURL integration library
24 * Download JSON using libcurl.
27 #ifndef GNUNET_CURL_LIB_H
28 #define GNUNET_CURL_LIB_H
30 #include <curl/curl.h>
31 #elif HAVE_GNURL_CURL_H
32 #include <gnurl/curl.h>
34 #error "needs curl or gnurl"
37 #include "gnunet_util_lib.h"
41 * Function called by the context to ask for the event loop to be
42 * rescheduled, that is the application should call
43 * #GNUNET_CURL_get_select_info() as the set of sockets we care about
49 (*GNUNET_CURL_RescheduleCallback)(void *cls);
53 * Initialise this library. This function should be called before using any of
54 * the following functions.
56 * @param cb function to call when rescheduling is required
57 * @param cb_cls closure for @a cb
58 * @return library context
60 struct GNUNET_CURL_Context *
61 GNUNET_CURL_init (GNUNET_CURL_RescheduleCallback cb,
66 * Obtain the information for a select() call to wait until
67 * #GNUNET_CURL_perform() is ready again. Note that calling
68 * any other TALER_EXCHANGE-API may also imply that the library
69 * is again ready for #GNUNET_CURL_perform().
71 * Basically, a client should use this API to prepare for select(),
72 * then block on select(), then call #GNUNET_CURL_perform() and then
73 * start again until the work with the context is done.
75 * This function will NOT zero out the sets and assumes that @a max_fd
76 * and @a timeout are already set to minimal applicable values. It is
77 * safe to give this API FD-sets and @a max_fd and @a timeout that are
78 * already initialized to some other descriptors that need to go into
81 * @param ctx context to get the event loop information for
82 * @param read_fd_set will be set for any pending read operations
83 * @param write_fd_set will be set for any pending write operations
84 * @param except_fd_set is here because curl_multi_fdset() has this argument
85 * @param max_fd set to the highest FD included in any set;
86 * if the existing sets have no FDs in it, the initial
87 * value should be "-1". (Note that `max_fd + 1` will need
88 * to be passed to select().)
89 * @param timeout set to the timeout in milliseconds (!); -1 means
90 * no timeout (NULL, blocking forever is OK), 0 means to
91 * proceed immediately with #GNUNET_CURL_perform().
94 GNUNET_CURL_get_select_info (struct GNUNET_CURL_Context *ctx,
97 fd_set *except_fd_set,
103 * Run the main event loop for the Taler interaction.
105 * @param ctx the library context
108 GNUNET_CURL_perform (struct GNUNET_CURL_Context *ctx);
112 * Cleanup library initialisation resources. This function should be called
113 * after using this library to cleanup the resources occupied during library's
116 * @param ctx the library context
119 GNUNET_CURL_fini (struct GNUNET_CURL_Context *ctx);
123 * Entry in the context's job queue.
125 struct GNUNET_CURL_Job;
128 * Function to call upon completion of a job.
131 * @param response_code HTTP response code from server, 0 on hard error
132 * @param json response, NULL if response was not in JSON format
135 (*GNUNET_CURL_JobCompletionCallback)(void *cls,
141 * Schedule a CURL request to be executed and call the given @a jcc
142 * upon its completion. Note that the context will make use of the
143 * CURLOPT_PRIVATE facility of the CURL @a eh.
145 * This function modifies the CURL handle to add the
146 * "Content-Type: application/json" header if @a add_json is set.
148 * @param ctx context to execute the job in
149 * @param eh curl easy handle for the request, will
150 * be executed AND cleaned up
151 * @param add_json add "application/json" content type header
152 * @param jcc callback to invoke upon completion
153 * @param jcc_cls closure for @a jcc
154 * @return NULL on error (in this case, @eh is still released!)
156 struct GNUNET_CURL_Job *
157 GNUNET_CURL_job_add (struct GNUNET_CURL_Context *ctx,
160 GNUNET_CURL_JobCompletionCallback jcc,
165 * Cancel a job. Must only be called before the job completion
166 * callback is called for the respective job.
168 * @param job job to cancel
171 GNUNET_CURL_job_cancel (struct GNUNET_CURL_Job *job);
174 /* ******* GNUnet SCHEDULER integration ************ */
178 * Closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
180 struct GNUNET_CURL_RescheduleContext;
184 * Initialize reschedule context.
186 * @param ctx context to manage
187 * @return closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
189 struct GNUNET_CURL_RescheduleContext *
190 GNUNET_CURL_gnunet_rc_create (struct GNUNET_CURL_Context *ctx);
193 * Destroy reschedule context.
195 * @param rc context to destroy
198 GNUNET_CURL_gnunet_rc_destroy (struct GNUNET_CURL_RescheduleContext *rc);
202 * Implementation of the #GNUNET_CURL_RescheduleCallback for GNUnet's
203 * scheduler. Will run the CURL context using GNUnet's scheduler.
204 * Note that you MUST immediately destroy the reschedule context after
205 * calling #GNUNET_CURL_fini().
207 * @param cls must point to a `struct GNUNET_CURL_RescheduleContext *`
208 * (pointer to a pointer!)
211 GNUNET_CURL_gnunet_scheduler_reschedule (void *cls);
215 /** @} */ /* end of group */
217 /* end of gnunet_curl_lib.h */