glitch in the license text detected by hyazinthe, thank you!
[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 /**
16  * @file src/include/gnunet_curl_lib.h
17  * @brief library to make it easy to download JSON replies over HTTP
18  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
19  * @author Christian Grothoff
20  *
21  * @defgroup curl CURL integration library
22  * Download JSON using libcurl.
23  * @{
24  */
25 #ifndef GNUNET_CURL_LIB_H
26 #define GNUNET_CURL_LIB_H
27 #if HAVE_CURL_CURL_H
28 #include <curl/curl.h>
29 #elif HAVE_GNURL_CURL_H
30 #include <gnurl/curl.h>
31 #else
32 #error "needs curl or gnurl"
33 #endif
34 #include <jansson.h>
35 #include "gnunet_util_lib.h"
36
37
38 /**
39  * Function called by the context to ask for the event loop to be
40  * rescheduled, that is the application should call
41  * #GNUNET_CURL_get_select_info() as the set of sockets we care about
42  * just changed.
43  *
44  * @param cls closure
45  */
46 typedef void
47 (*GNUNET_CURL_RescheduleCallback)(void *cls);
48
49
50 /**
51  * Initialise this library.  This function should be called before using any of
52  * the following functions.
53  *
54  * @param cb function to call when rescheduling is required
55  * @param cb_cls closure for @a cb
56  * @return library context
57  */
58 struct GNUNET_CURL_Context *
59 GNUNET_CURL_init (GNUNET_CURL_RescheduleCallback cb,
60                   void *cb_cls);
61
62
63 /**
64  * Obtain the information for a select() call to wait until
65  * #GNUNET_CURL_perform() is ready again.
66  *
67  * Basically, a client should use this API to prepare for select(),
68  * then block on select(), then call #GNUNET_CURL_perform() and then
69  * start again until the work with the context is done.
70  *
71  * This function will NOT zero out the sets and assumes that @a max_fd
72  * and @a timeout are already set to minimal applicable values.  It is
73  * safe to give this API FD-sets and @a max_fd and @a timeout that are
74  * already initialized to some other descriptors that need to go into
75  * the select() call.
76  *
77  * @param ctx context to get the event loop information for
78  * @param read_fd_set will be set for any pending read operations
79  * @param write_fd_set will be set for any pending write operations
80  * @param except_fd_set is here because curl_multi_fdset() has this argument
81  * @param max_fd set to the highest FD included in any set;
82  *        if the existing sets have no FDs in it, the initial
83  *        value should be "-1". (Note that `max_fd + 1` will need
84  *        to be passed to select().)
85  * @param timeout set to the timeout in milliseconds (!); -1 means
86  *        no timeout (NULL, blocking forever is OK), 0 means to
87  *        proceed immediately with #GNUNET_CURL_perform().
88  */
89 void
90 GNUNET_CURL_get_select_info (struct GNUNET_CURL_Context *ctx,
91                              fd_set *read_fd_set,
92                              fd_set *write_fd_set,
93                              fd_set *except_fd_set,
94                              int *max_fd,
95                              long *timeout);
96
97
98 /**
99  * Run the main event loop for the CURL interaction.
100  *
101  * @param ctx the library context
102  */
103 void
104 GNUNET_CURL_perform (struct GNUNET_CURL_Context *ctx);
105
106
107 /**
108  * Cleanup library initialisation resources.  This function should be called
109  * after using this library to cleanup the resources occupied during library's
110  * initialisation.
111  *
112  * @param ctx the library context
113  */
114 void
115 GNUNET_CURL_fini (struct GNUNET_CURL_Context *ctx);
116
117
118 /**
119  * Entry in the context's job queue.
120  */
121 struct GNUNET_CURL_Job;
122
123 /**
124  * Function to call upon completion of a job.
125  *
126  * @param cls closure
127  * @param response_code HTTP response code from server, 0 on hard error
128  * @param json response, NULL if response was not in JSON format
129  */
130 typedef void
131 (*GNUNET_CURL_JobCompletionCallback)(void *cls,
132                                      long response_code,
133                                      const json_t *json);
134
135
136 /**
137  * Schedule a CURL request to be executed and call the given @a jcc
138  * upon its completion. Note that the context will make use of the
139  * CURLOPT_PRIVATE facility of the CURL @a eh.
140  *
141  * This function modifies the CURL handle to add the
142  * "Content-Type: application/json" header if @a add_json is set.
143  *
144  * @param ctx context to execute the job in
145  * @param eh curl easy handle for the request, will
146  *           be executed AND cleaned up
147  * @param add_json add "application/json" content type header
148  * @param jcc callback to invoke upon completion
149  * @param jcc_cls closure for @a jcc
150  * @return NULL on error (in this case, @eh is still released!)
151  */
152 struct GNUNET_CURL_Job *
153 GNUNET_CURL_job_add (struct GNUNET_CURL_Context *ctx,
154                      CURL *eh,
155                      int add_json,
156                      GNUNET_CURL_JobCompletionCallback jcc,
157                      void *jcc_cls);
158
159
160 /**
161  * Cancel a job.  Must only be called before the job completion
162  * callback is called for the respective job.
163  *
164  * @param job job to cancel
165  */
166 void
167 GNUNET_CURL_job_cancel (struct GNUNET_CURL_Job *job);
168
169
170 /* ******* GNUnet SCHEDULER integration ************ */
171
172
173 /**
174  * Closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
175  */
176 struct GNUNET_CURL_RescheduleContext;
177
178
179 /**
180  * Initialize reschedule context.
181  *
182  * @param ctx context to manage
183  * @return closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
184  */
185 struct GNUNET_CURL_RescheduleContext *
186 GNUNET_CURL_gnunet_rc_create (struct GNUNET_CURL_Context *ctx);
187
188 /**
189  * Destroy reschedule context.
190  *
191  * @param rc context to destroy
192  */
193 void
194 GNUNET_CURL_gnunet_rc_destroy (struct GNUNET_CURL_RescheduleContext *rc);
195
196
197 /**
198  * Implementation of the #GNUNET_CURL_RescheduleCallback for GNUnet's
199  * scheduler.  Will run the CURL context using GNUnet's scheduler.
200  * Note that you MUST immediately destroy the reschedule context after
201  * calling #GNUNET_CURL_fini().
202  *
203  * @param cls must point to a `struct GNUNET_CURL_RescheduleContext *`
204  *           (pointer to a pointer!)
205  */
206 void
207 GNUNET_CURL_gnunet_scheduler_reschedule (void *cls);
208
209
210 #endif
211 /** @} */  /* end of group */
212
213 /* end of gnunet_curl_lib.h */