Merge branch 'master' of ssh://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 /**
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
23  *
24  * @defgroup curl CURL integration library
25  * Download JSON using libcurl.
26  * @{
27  */
28 #ifndef GNUNET_CURL_LIB_H
29 #define GNUNET_CURL_LIB_H
30 #if HAVE_CURL_CURL_H
31 #include <curl/curl.h>
32 #elif HAVE_GNURL_CURL_H
33 #include <gnurl/curl.h>
34 #else
35 #error "needs curl or gnurl"
36 #endif
37 #include <jansson.h>
38 #include "gnunet_util_lib.h"
39
40
41 /**
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
45  * just changed.
46  *
47  * @param cls closure
48  */
49 typedef void
50 (*GNUNET_CURL_RescheduleCallback)(void *cls);
51
52 /**
53  * @brief Buffer data structure we use to buffer the HTTP download
54  * before giving it to the JSON parser.
55  */
56 struct GNUNET_CURL_DownloadBuffer
57 {
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 /**
79  * Parses the raw response we got from the Web server.
80  *
81  * @param db the raw data
82  * @param eh handle
83  * @param response_code HTTP response code
84  * @return the parsed object
85  */
86 typedef void *
87 (*GNUNET_CURL_RawParser) (struct GNUNET_CURL_DownloadBuffer *db,
88                           CURL *eh,
89                           long *response_code);
90
91 /**
92  * Deallocate the response.
93  * 
94  * @param response object to clean
95  */
96 typedef void
97 (*GNUNET_CURL_ResponseCleaner) (void *response);
98
99 /**
100  * Initialise this library.  This function should be called before using any of
101  * the following functions.
102  *
103  * @param cb function to call when rescheduling is required
104  * @param cb_cls closure for @a cb
105  * @return library context
106  */
107 struct GNUNET_CURL_Context *
108 GNUNET_CURL_init (GNUNET_CURL_RescheduleCallback cb,
109                   void *cb_cls);
110
111
112 /**
113  * Obtain the information for a select() call to wait until
114  * #GNUNET_CURL_perform() is ready again.
115  *
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.
119  *
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
124  * the select() call.
125  *
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().
137  */
138 void
139 GNUNET_CURL_get_select_info (struct GNUNET_CURL_Context *ctx,
140                              fd_set *read_fd_set,
141                              fd_set *write_fd_set,
142                              fd_set *except_fd_set,
143                              int *max_fd,
144                              long *timeout);
145
146
147 /**
148  * Add custom request header.
149  *
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.
153  */
154 int
155 GNUNET_CURL_append_header (struct GNUNET_CURL_Context *ctx,
156                            const char *header);
157
158 /**
159  * Run the main event loop for the CURL interaction.
160  *
161  * @param ctx the library context
162  */
163 void
164 GNUNET_CURL_perform (struct GNUNET_CURL_Context *ctx);
165
166
167 /**
168  * Run the main event loop for the Taler interaction.
169  *
170  * @param ctx the library context
171  * @param rp parses the raw response returned from
172  *        the Web server.
173  * @param rc cleans/frees the response
174  */
175 void
176 GNUNET_CURL_perform2 (struct GNUNET_CURL_Context *ctx,
177                       GNUNET_CURL_RawParser rp,
178                       GNUNET_CURL_ResponseCleaner rc);
179
180 /**
181  * Cleanup library initialisation resources.  This function should be called
182  * after using this library to cleanup the resources occupied during library's
183  * initialisation.
184  *
185  * @param ctx the library context
186  */
187 void
188 GNUNET_CURL_fini (struct GNUNET_CURL_Context *ctx);
189
190
191 /**
192  * Entry in the context's job queue.
193  */
194 struct GNUNET_CURL_Job;
195
196 /**
197  * Function to call upon completion of a job.
198  *
199  * @param cls closure
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
202  */
203 typedef void
204 (*GNUNET_CURL_JobCompletionCallback)(void *cls,
205                                      long response_code,
206                                      const void *response);
207
208
209 /**
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.
213  *
214  * This function modifies the CURL handle to add the
215  * "Content-Type: application/json" header if @a add_json is set.
216  *
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!)
224  */
225 struct GNUNET_CURL_Job *
226 GNUNET_CURL_job_add (struct GNUNET_CURL_Context *ctx,
227                      CURL *eh,
228                      int add_json,
229                      GNUNET_CURL_JobCompletionCallback jcc,
230                      void *jcc_cls);
231
232
233 /**
234  * Cancel a job.  Must only be called before the job completion
235  * callback is called for the respective job.
236  *
237  * @param job job to cancel
238  */
239 void
240 GNUNET_CURL_job_cancel (struct GNUNET_CURL_Job *job);
241
242
243 /* ******* GNUnet SCHEDULER integration ************ */
244
245
246 /**
247  * Closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
248  */
249 struct GNUNET_CURL_RescheduleContext;
250
251
252 /**
253  * Initialize reschedule context.
254  *
255  * @param ctx context to manage
256  * @return closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
257  */
258 struct GNUNET_CURL_RescheduleContext *
259 GNUNET_CURL_gnunet_rc_create (struct GNUNET_CURL_Context *ctx);
260
261 /**
262  * Initialize reschedule context; with custom response parser
263  *
264  * @param ctx context to manage
265  * @return closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
266  */
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);
271
272
273 /**
274  * Destroy reschedule context.
275  *
276  * @param rc context to destroy
277  */
278 void
279 GNUNET_CURL_gnunet_rc_destroy (struct GNUNET_CURL_RescheduleContext *rc);
280
281
282 /**
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().
287  *
288  * @param cls must point to a `struct GNUNET_CURL_RescheduleContext *`
289  *           (pointer to a pointer!)
290  */
291 void
292 GNUNET_CURL_gnunet_scheduler_reschedule (void *cls);
293
294
295 #endif
296 /** @} */  /* end of group */
297
298 /* end of gnunet_curl_lib.h */