Expanding libgnunetcurl.
[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 /**
54  * Initialise this library.  This function should be called before using any of
55  * the following functions.
56  *
57  * @param cb function to call when rescheduling is required
58  * @param cb_cls closure for @a cb
59  * @return library context
60  */
61 struct GNUNET_CURL_Context *
62 GNUNET_CURL_init (GNUNET_CURL_RescheduleCallback cb,
63                   void *cb_cls);
64
65
66 /**
67  * Obtain the information for a select() call to wait until
68  * #GNUNET_CURL_perform() is ready again.
69  *
70  * Basically, a client should use this API to prepare for select(),
71  * then block on select(), then call #GNUNET_CURL_perform() and then
72  * start again until the work with the context is done.
73  *
74  * This function will NOT zero out the sets and assumes that @a max_fd
75  * and @a timeout are already set to minimal applicable values.  It is
76  * safe to give this API FD-sets and @a max_fd and @a timeout that are
77  * already initialized to some other descriptors that need to go into
78  * the select() call.
79  *
80  * @param ctx context to get the event loop information for
81  * @param read_fd_set will be set for any pending read operations
82  * @param write_fd_set will be set for any pending write operations
83  * @param except_fd_set is here because curl_multi_fdset() has this argument
84  * @param max_fd set to the highest FD included in any set;
85  *        if the existing sets have no FDs in it, the initial
86  *        value should be "-1". (Note that `max_fd + 1` will need
87  *        to be passed to select().)
88  * @param timeout set to the timeout in milliseconds (!); -1 means
89  *        no timeout (NULL, blocking forever is OK), 0 means to
90  *        proceed immediately with #GNUNET_CURL_perform().
91  */
92 void
93 GNUNET_CURL_get_select_info (struct GNUNET_CURL_Context *ctx,
94                              fd_set *read_fd_set,
95                              fd_set *write_fd_set,
96                              fd_set *except_fd_set,
97                              int *max_fd,
98                              long *timeout);
99
100
101 /**
102  * Add custom request header.
103  *
104  * @param ctx cURL context.
105  * @param header header string; will be given to the context AS IS.
106  * @return #GNUNET_OK if no errors occurred, #GNUNET_SYSERR otherwise.
107  */
108 int
109 GNUNET_CURL_append_header (struct GNUNET_CURL_Context *ctx,
110                            const char *header);
111
112 /**
113  * Run the main event loop for the CURL interaction.
114  *
115  * @param ctx the library context
116  */
117 void
118 GNUNET_CURL_perform (struct GNUNET_CURL_Context *ctx);
119
120
121 /**
122  * Cleanup library initialisation resources.  This function should be called
123  * after using this library to cleanup the resources occupied during library's
124  * initialisation.
125  *
126  * @param ctx the library context
127  */
128 void
129 GNUNET_CURL_fini (struct GNUNET_CURL_Context *ctx);
130
131
132 /**
133  * Entry in the context's job queue.
134  */
135 struct GNUNET_CURL_Job;
136
137 /**
138  * Function to call upon completion of a job.
139  *
140  * @param cls closure
141  * @param response_code HTTP response code from server, 0 on hard error
142  * @param json response, NULL if response was not in JSON format
143  */
144 typedef void
145 (*GNUNET_CURL_JobCompletionCallback)(void *cls,
146                                      long response_code,
147                                      const json_t *json);
148
149
150 /**
151  * Schedule a CURL request to be executed and call the given @a jcc
152  * upon its completion. Note that the context will make use of the
153  * CURLOPT_PRIVATE facility of the CURL @a eh.
154  *
155  * This function modifies the CURL handle to add the
156  * "Content-Type: application/json" header if @a add_json is set.
157  *
158  * @param ctx context to execute the job in
159  * @param eh curl easy handle for the request, will
160  *           be executed AND cleaned up
161  * @param add_json add "application/json" content type header
162  * @param jcc callback to invoke upon completion
163  * @param jcc_cls closure for @a jcc
164  * @return NULL on error (in this case, @eh is still released!)
165  */
166 struct GNUNET_CURL_Job *
167 GNUNET_CURL_job_add (struct GNUNET_CURL_Context *ctx,
168                      CURL *eh,
169                      int add_json,
170                      GNUNET_CURL_JobCompletionCallback jcc,
171                      void *jcc_cls);
172
173
174 /**
175  * Cancel a job.  Must only be called before the job completion
176  * callback is called for the respective job.
177  *
178  * @param job job to cancel
179  */
180 void
181 GNUNET_CURL_job_cancel (struct GNUNET_CURL_Job *job);
182
183
184 /* ******* GNUnet SCHEDULER integration ************ */
185
186
187 /**
188  * Closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
189  */
190 struct GNUNET_CURL_RescheduleContext;
191
192
193 /**
194  * Initialize reschedule context.
195  *
196  * @param ctx context to manage
197  * @return closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
198  */
199 struct GNUNET_CURL_RescheduleContext *
200 GNUNET_CURL_gnunet_rc_create (struct GNUNET_CURL_Context *ctx);
201
202 /**
203  * Destroy reschedule context.
204  *
205  * @param rc context to destroy
206  */
207 void
208 GNUNET_CURL_gnunet_rc_destroy (struct GNUNET_CURL_RescheduleContext *rc);
209
210
211 /**
212  * Implementation of the #GNUNET_CURL_RescheduleCallback for GNUnet's
213  * scheduler.  Will run the CURL context using GNUnet's scheduler.
214  * Note that you MUST immediately destroy the reschedule context after
215  * calling #GNUNET_CURL_fini().
216  *
217  * @param cls must point to a `struct GNUNET_CURL_RescheduleContext *`
218  *           (pointer to a pointer!)
219  */
220 void
221 GNUNET_CURL_gnunet_scheduler_reschedule (void *cls);
222
223
224 #endif
225 /** @} */  /* end of group */
226
227 /* end of gnunet_curl_lib.h */