Merge branch 'master' of gnunet.org:gnunet
[oweals/gnunet.git] / src / include / gnunet_curl_lib.h
1 /*
2   This file is part of GNUnet
3   Copyright (C) 2014, 2015, 2016 GNUnet e.V.
4
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.
9
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.
14  
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/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19 */
20 /**
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
25  *
26  * @defgroup curl CURL integration library
27  * Download JSON using libcurl.
28  * @{
29  */
30 #ifndef GNUNET_CURL_LIB_H
31 #define GNUNET_CURL_LIB_H
32 #if HAVE_CURL_CURL_H
33 #include <curl/curl.h>
34 #elif HAVE_GNURL_CURL_H
35 #include <gnurl/curl.h>
36 #else
37 #error "needs curl or gnurl"
38 #endif
39 #include <jansson.h>
40 #include "gnunet_util_lib.h"
41
42
43 /**
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
47  * just changed.
48  *
49  * @param cls closure
50  */
51 typedef void
52 (*GNUNET_CURL_RescheduleCallback)(void *cls);
53
54 /**
55  * @brief Buffer data structure we use to buffer the HTTP download
56  * before giving it to the JSON parser.
57  */
58 struct GNUNET_CURL_DownloadBuffer
59 {
60
61   /**
62    * Download buffer
63    */
64   void *buf;
65
66   /**
67    * The size of the download buffer
68    */
69   size_t buf_size;
70
71   /**
72    * Error code (based on libc errno) if we failed to download
73    * (i.e. response too large).
74    */
75   int eno;
76
77 };
78
79
80 /**
81  * Parses the raw response we got from the Web server.
82  *
83  * @param db the raw data
84  * @param eh handle
85  * @param response_code HTTP response code
86  * @return the parsed object
87  */
88 typedef void *
89 (*GNUNET_CURL_RawParser) (struct GNUNET_CURL_DownloadBuffer *db,
90                           CURL *eh,
91                           long *response_code);
92
93 /**
94  * Deallocate the response.
95  * 
96  * @param response object to clean
97  */
98 typedef void
99 (*GNUNET_CURL_ResponseCleaner) (void *response);
100
101 /**
102  * Initialise this library.  This function should be called before using any of
103  * the following functions.
104  *
105  * @param cb function to call when rescheduling is required
106  * @param cb_cls closure for @a cb
107  * @return library context
108  */
109 struct GNUNET_CURL_Context *
110 GNUNET_CURL_init (GNUNET_CURL_RescheduleCallback cb,
111                   void *cb_cls);
112
113
114 /**
115  * Obtain the information for a select() call to wait until
116  * #GNUNET_CURL_perform() is ready again.
117  *
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.
121  *
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
126  * the select() call.
127  *
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().
139  */
140 void
141 GNUNET_CURL_get_select_info (struct GNUNET_CURL_Context *ctx,
142                              fd_set *read_fd_set,
143                              fd_set *write_fd_set,
144                              fd_set *except_fd_set,
145                              int *max_fd,
146                              long *timeout);
147
148
149 /**
150  * Add custom request header.
151  *
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.
155  */
156 int
157 GNUNET_CURL_append_header (struct GNUNET_CURL_Context *ctx,
158                            const char *header);
159
160 /**
161  * Run the main event loop for the CURL interaction.
162  *
163  * @param ctx the library context
164  */
165 void
166 GNUNET_CURL_perform (struct GNUNET_CURL_Context *ctx);
167
168
169 /**
170  * Run the main event loop for the Taler interaction.
171  *
172  * @param ctx the library context
173  * @param rp parses the raw response returned from
174  *        the Web server.
175  * @param rc cleans/frees the response
176  */
177 void
178 GNUNET_CURL_perform2 (struct GNUNET_CURL_Context *ctx,
179                       GNUNET_CURL_RawParser rp,
180                       GNUNET_CURL_ResponseCleaner rc);
181
182 /**
183  * Cleanup library initialisation resources.  This function should be called
184  * after using this library to cleanup the resources occupied during library's
185  * initialisation.
186  *
187  * @param ctx the library context
188  */
189 void
190 GNUNET_CURL_fini (struct GNUNET_CURL_Context *ctx);
191
192
193 /**
194  * Entry in the context's job queue.
195  */
196 struct GNUNET_CURL_Job;
197
198 /**
199  * Function to call upon completion of a job.
200  *
201  * @param cls closure
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
204  */
205 typedef void
206 (*GNUNET_CURL_JobCompletionCallback)(void *cls,
207                                      long response_code,
208                                      const void *response);
209
210
211 /**
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.
215  *
216  * This function modifies the CURL handle to add the
217  * "Content-Type: application/json" header if @a add_json is set.
218  *
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!)
226  */
227 struct GNUNET_CURL_Job *
228 GNUNET_CURL_job_add (struct GNUNET_CURL_Context *ctx,
229                      CURL *eh,
230                      int add_json,
231                      GNUNET_CURL_JobCompletionCallback jcc,
232                      void *jcc_cls);
233
234
235 /**
236  * Cancel a job.  Must only be called before the job completion
237  * callback is called for the respective job.
238  *
239  * @param job job to cancel
240  */
241 void
242 GNUNET_CURL_job_cancel (struct GNUNET_CURL_Job *job);
243
244
245 /* ******* GNUnet SCHEDULER integration ************ */
246
247
248 /**
249  * Closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
250  */
251 struct GNUNET_CURL_RescheduleContext;
252
253
254 /**
255  * Initialize reschedule context.
256  *
257  * @param ctx context to manage
258  * @return closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
259  */
260 struct GNUNET_CURL_RescheduleContext *
261 GNUNET_CURL_gnunet_rc_create (struct GNUNET_CURL_Context *ctx);
262
263 /**
264  * Initialize reschedule context; with custom response parser
265  *
266  * @param ctx context to manage
267  * @return closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
268  */
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);
273
274
275 /**
276  * Destroy reschedule context.
277  *
278  * @param rc context to destroy
279  */
280 void
281 GNUNET_CURL_gnunet_rc_destroy (struct GNUNET_CURL_RescheduleContext *rc);
282
283
284 /**
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().
289  *
290  * @param cls must point to a `struct GNUNET_CURL_RescheduleContext *`
291  *           (pointer to a pointer!)
292  */
293 void
294 GNUNET_CURL_gnunet_scheduler_reschedule (void *cls);
295
296
297 #endif
298 /** @} */  /* end of group */
299
300 /* end of gnunet_curl_lib.h */