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