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