use NULL value in load_path_suffix to NOT load any files
[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_LIBCURL
33 #include <curl/curl.h>
34 #elif HAVE_LIBGNURL
35 #include <gnurl/curl.h>
36 #else
37 #error "needs curl or gnurl"
38 #endif
39 #include "gnunet_util_lib.h"
40
41
42 /**
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
46  * just changed.
47  *
48  * @param cls closure
49  */
50 typedef void
51 (*GNUNET_CURL_RescheduleCallback)(void *cls);
52
53 /**
54  * @brief Buffer data structure we use to buffer the HTTP download
55  * before giving it to the JSON parser.
56  */
57 struct GNUNET_CURL_DownloadBuffer
58 {
59   /**
60    * Download buffer
61    */
62   void *buf;
63
64   /**
65    * The size of the download buffer
66    */
67   size_t buf_size;
68
69   /**
70    * Error code (based on libc errno) if we failed to download
71    * (i.e. response too large).
72    */
73   int eno;
74 };
75
76
77 /**
78  * Parses the raw response we got from the Web server.
79  *
80  * @param db the raw data
81  * @param eh handle
82  * @param response_code HTTP response code
83  * @return the parsed object
84  */
85 typedef void *
86 (*GNUNET_CURL_RawParser) (struct GNUNET_CURL_DownloadBuffer *db,
87                           CURL *eh,
88                           long *response_code);
89
90 /**
91  * Deallocate the response.
92  *
93  * @param response object to clean
94  */
95 typedef void
96 (*GNUNET_CURL_ResponseCleaner) (void *response);
97
98 /**
99  * Initialise this library.  This function should be called before using any of
100  * the following functions.
101  *
102  * @param cb function to call when rescheduling is required
103  * @param cb_cls closure for @a cb
104  * @return library context
105  */
106 struct GNUNET_CURL_Context *
107 GNUNET_CURL_init (GNUNET_CURL_RescheduleCallback cb,
108                   void *cb_cls);
109
110
111 /**
112  * Obtain the information for a select() call to wait until
113  * #GNUNET_CURL_perform() is ready again.
114  *
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.
118  *
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
123  * the select() call.
124  *
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().
136  */
137 void
138 GNUNET_CURL_get_select_info (struct GNUNET_CURL_Context *ctx,
139                              fd_set *read_fd_set,
140                              fd_set *write_fd_set,
141                              fd_set *except_fd_set,
142                              int *max_fd,
143                              long *timeout);
144
145
146 /**
147  * Add custom request header.
148  *
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.
152  */
153 int
154 GNUNET_CURL_append_header (struct GNUNET_CURL_Context *ctx,
155                            const char *header);
156
157 /**
158  * Run the main event loop for the CURL interaction.
159  *
160  * @param ctx the library context
161  */
162 void
163 GNUNET_CURL_perform (struct GNUNET_CURL_Context *ctx);
164
165
166 /**
167  * Run the main event loop for the HTTP interaction.
168  *
169  * @param ctx the library context
170  * @param rp parses the raw response returned from
171  *        the Web server.
172  * @param rc cleans/frees the response
173  */
174 void
175 GNUNET_CURL_perform2 (struct GNUNET_CURL_Context *ctx,
176                       GNUNET_CURL_RawParser rp,
177                       GNUNET_CURL_ResponseCleaner rc);
178
179 /**
180  * Cleanup library initialisation resources.  This function should be called
181  * after using this library to cleanup the resources occupied during library's
182  * initialisation.
183  *
184  * @param ctx the library context
185  */
186 void
187 GNUNET_CURL_fini (struct GNUNET_CURL_Context *ctx);
188
189
190 /**
191  * Entry in the context's job queue.
192  */
193 struct GNUNET_CURL_Job;
194
195 /**
196  * Function to call upon completion of a job.
197  *
198  * @param cls closure
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
201  */
202 typedef void
203 (*GNUNET_CURL_JobCompletionCallback)(void *cls,
204                                      long response_code,
205                                      const void *response);
206
207
208 /**
209  * Function to call upon completion of a raw job.
210  *
211  * @param cls closure
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
215  */
216 typedef void
217 (*GNUNET_CURL_RawJobCompletionCallback)(void *cls,
218                                         long response_code,
219                                         const void *body,
220                                         size_t body_size);
221
222
223 /**
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.
227  *
228  * This function modifies the CURL handle to add the
229  * "Content-Type: application/json" header if @a add_json is set.
230  *
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!)
238  */
239 struct GNUNET_CURL_Job *
240 GNUNET_CURL_job_add (struct GNUNET_CURL_Context *ctx,
241                      CURL *eh,
242                      int add_json,
243                      GNUNET_CURL_JobCompletionCallback jcc,
244                      void *jcc_cls);
245
246
247 /**
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.
251  *
252  * This function modifies the CURL handle to add the
253  * "Content-Type: application/json" header if @a add_json is set.
254  *
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!)
262  */
263 struct GNUNET_CURL_Job *
264 GNUNET_CURL_job_add2 (struct GNUNET_CURL_Context *ctx,
265                       CURL *eh,
266                       const struct curl_slist *job_headers,
267                       GNUNET_CURL_JobCompletionCallback jcc,
268                       void *jcc_cls);
269
270
271 /**
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.
276  *
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!)
285  */
286 struct GNUNET_CURL_Job *
287 GNUNET_CURL_job_add_raw (struct GNUNET_CURL_Context *ctx,
288                          CURL *eh,
289                          const struct curl_slist *job_headers,
290                          GNUNET_CURL_RawJobCompletionCallback jcc,
291                          void *jcc_cls);
292
293
294 /**
295  * Cancel a job.  Must only be called before the job completion
296  * callback is called for the respective job.
297  *
298  * @param job job to cancel
299  */
300 void
301 GNUNET_CURL_job_cancel (struct GNUNET_CURL_Job *job);
302
303
304 /* ******* GNUnet SCHEDULER integration ************ */
305
306
307 /**
308  * Closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
309  */
310 struct GNUNET_CURL_RescheduleContext;
311
312
313 /**
314  * Initialize reschedule context.
315  *
316  * @param ctx context to manage
317  * @return closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
318  */
319 struct GNUNET_CURL_RescheduleContext *
320 GNUNET_CURL_gnunet_rc_create (struct GNUNET_CURL_Context *ctx);
321
322 /**
323  * Initialize reschedule context; with custom response parser
324  *
325  * @param ctx context to manage
326  * @return closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
327  */
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);
332
333
334 /**
335  * Destroy reschedule context.
336  *
337  * @param rc context to destroy
338  */
339 void
340 GNUNET_CURL_gnunet_rc_destroy (struct GNUNET_CURL_RescheduleContext *rc);
341
342
343 /**
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().
348  *
349  * @param cls must point to a `struct GNUNET_CURL_RescheduleContext *`
350  *           (pointer to a pointer!)
351  */
352 void
353 GNUNET_CURL_gnunet_scheduler_reschedule (void *cls);
354
355
356 /**
357  * Enable sending the async scope ID as a header.
358  *
359  * @param ctx the context to enable this for
360  * @param header_name name of the header to send.
361  */
362 void
363 GNUNET_CURL_enable_async_scope_header (struct GNUNET_CURL_Context *ctx, const
364                                        char *header_name);
365
366
367 #endif
368 /** @} */  /* end of group */
369
370 /* end of gnunet_curl_lib.h */