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.
69 * Basically, a client should use this API to prepare for select(),
70 * then block on select(), then call #GNUNET_CURL_perform() and then
71 * start again until the work with the context is done.
73 * This function will NOT zero out the sets and assumes that @a max_fd
74 * and @a timeout are already set to minimal applicable values. It is
75 * safe to give this API FD-sets and @a max_fd and @a timeout that are
76 * already initialized to some other descriptors that need to go into
79 * @param ctx context to get the event loop information for
80 * @param read_fd_set will be set for any pending read operations
81 * @param write_fd_set will be set for any pending write operations
82 * @param except_fd_set is here because curl_multi_fdset() has this argument
83 * @param max_fd set to the highest FD included in any set;
84 * if the existing sets have no FDs in it, the initial
85 * value should be "-1". (Note that `max_fd + 1` will need
86 * to be passed to select().)
87 * @param timeout set to the timeout in milliseconds (!); -1 means
88 * no timeout (NULL, blocking forever is OK), 0 means to
89 * proceed immediately with #GNUNET_CURL_perform().
92 GNUNET_CURL_get_select_info (struct GNUNET_CURL_Context *ctx,
95 fd_set *except_fd_set,
101 * Run the main event loop for the CURL interaction.
103 * @param ctx the library context
106 GNUNET_CURL_perform (struct GNUNET_CURL_Context *ctx);
110 * Cleanup library initialisation resources. This function should be called
111 * after using this library to cleanup the resources occupied during library's
114 * @param ctx the library context
117 GNUNET_CURL_fini (struct GNUNET_CURL_Context *ctx);
121 * Entry in the context's job queue.
123 struct GNUNET_CURL_Job;
126 * Function to call upon completion of a job.
129 * @param response_code HTTP response code from server, 0 on hard error
130 * @param json response, NULL if response was not in JSON format
133 (*GNUNET_CURL_JobCompletionCallback)(void *cls,
139 * Schedule a CURL request to be executed and call the given @a jcc
140 * upon its completion. Note that the context will make use of the
141 * CURLOPT_PRIVATE facility of the CURL @a eh.
143 * This function modifies the CURL handle to add the
144 * "Content-Type: application/json" header if @a add_json is set.
146 * @param ctx context to execute the job in
147 * @param eh curl easy handle for the request, will
148 * be executed AND cleaned up
149 * @param add_json add "application/json" content type header
150 * @param jcc callback to invoke upon completion
151 * @param jcc_cls closure for @a jcc
152 * @return NULL on error (in this case, @eh is still released!)
154 struct GNUNET_CURL_Job *
155 GNUNET_CURL_job_add (struct GNUNET_CURL_Context *ctx,
158 GNUNET_CURL_JobCompletionCallback jcc,
163 * Cancel a job. Must only be called before the job completion
164 * callback is called for the respective job.
166 * @param job job to cancel
169 GNUNET_CURL_job_cancel (struct GNUNET_CURL_Job *job);
172 /* ******* GNUnet SCHEDULER integration ************ */
176 * Closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
178 struct GNUNET_CURL_RescheduleContext;
182 * Initialize reschedule context.
184 * @param ctx context to manage
185 * @return closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
187 struct GNUNET_CURL_RescheduleContext *
188 GNUNET_CURL_gnunet_rc_create (struct GNUNET_CURL_Context *ctx);
191 * Destroy reschedule context.
193 * @param rc context to destroy
196 GNUNET_CURL_gnunet_rc_destroy (struct GNUNET_CURL_RescheduleContext *rc);
200 * Implementation of the #GNUNET_CURL_RescheduleCallback for GNUnet's
201 * scheduler. Will run the CURL context using GNUnet's scheduler.
202 * Note that you MUST immediately destroy the reschedule context after
203 * calling #GNUNET_CURL_fini().
205 * @param cls must point to a `struct GNUNET_CURL_RescheduleContext *`
206 * (pointer to a pointer!)
209 GNUNET_CURL_gnunet_scheduler_reschedule (void *cls);
213 /** @} */ /* end of group */
215 /* end of gnunet_curl_lib.h */