curl_lib: fix building with gnurl/curl.h
[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 #endif
34 #include <jansson.h>
35 #include <gnunet/gnunet_util_lib.h>
36
37
38 /**
39  * Initialise this library.  This function should be called before using any of
40  * the following functions.
41  *
42  * @return library context
43  */
44 struct GNUNET_CURL_Context *
45 GNUNET_CURL_init (void);
46
47
48 /**
49  * Obtain the information for a select() call to wait until
50  * #GNUNET_CURL_perform() is ready again.  Note that calling
51  * any other TALER_EXCHANGE-API may also imply that the library
52  * is again ready for #GNUNET_CURL_perform().
53  *
54  * Basically, a client should use this API to prepare for select(),
55  * then block on select(), then call #GNUNET_CURL_perform() and then
56  * start again until the work with the context is done.
57  *
58  * This function will NOT zero out the sets and assumes that @a max_fd
59  * and @a timeout are already set to minimal applicable values.  It is
60  * safe to give this API FD-sets and @a max_fd and @a timeout that are
61  * already initialized to some other descriptors that need to go into
62  * the select() call.
63  *
64  * @param ctx context to get the event loop information for
65  * @param read_fd_set will be set for any pending read operations
66  * @param write_fd_set will be set for any pending write operations
67  * @param except_fd_set is here because curl_multi_fdset() has this argument
68  * @param max_fd set to the highest FD included in any set;
69  *        if the existing sets have no FDs in it, the initial
70  *        value should be "-1". (Note that `max_fd + 1` will need
71  *        to be passed to select().)
72  * @param timeout set to the timeout in milliseconds (!); -1 means
73  *        no timeout (NULL, blocking forever is OK), 0 means to
74  *        proceed immediately with #GNUNET_CURL_perform().
75  */
76 void
77 GNUNET_CURL_get_select_info (struct GNUNET_CURL_Context *ctx,
78                              fd_set *read_fd_set,
79                              fd_set *write_fd_set,
80                              fd_set *except_fd_set,
81                              int *max_fd,
82                              long *timeout);
83
84
85 /**
86  * Run the main event loop for the Taler interaction.
87  *
88  * @param ctx the library context
89  */
90 void
91 GNUNET_CURL_perform (struct GNUNET_CURL_Context *ctx);
92
93
94 /**
95  * Cleanup library initialisation resources.  This function should be called
96  * after using this library to cleanup the resources occupied during library's
97  * initialisation.
98  *
99  * @param ctx the library context
100  */
101 void
102 GNUNET_CURL_fini (struct GNUNET_CURL_Context *ctx);
103
104
105 /**
106  * Entry in the context's job queue.
107  */
108 struct GNUNET_CURL_Job;
109
110 /**
111  * Function to call upon completion of a job.
112  *
113  * @param cls closure
114  * @param response_code HTTP response code from server, 0 on hard error
115  * @param json response, NULL if response was not in JSON format
116  */
117 typedef void
118 (*GNUNET_CURL_JobCompletionCallback)(void *cls,
119                                      long response_code,
120                                      const json_t *json);
121
122
123 /**
124  * Schedule a CURL request to be executed and call the given @a jcc
125  * upon its completion. Note that the context will make use of the
126  * CURLOPT_PRIVATE facility of the CURL @a eh.
127  *
128  * This function modifies the CURL handle to add the
129  * "Content-Type: application/json" header if @a add_json is set.
130  *
131  * @param ctx context to execute the job in
132  * @param eh curl easy handle for the request, will
133  *           be executed AND cleaned up
134  * @param add_json add "application/json" content type header
135  * @param jcc callback to invoke upon completion
136  * @param jcc_cls closure for @a jcc
137  * @return NULL on error (in this case, @eh is still released!)
138  */
139 struct GNUNET_CURL_Job *
140 GNUNET_CURL_job_add (struct GNUNET_CURL_Context *ctx,
141                      CURL *eh,
142                      int add_json,
143                      GNUNET_CURL_JobCompletionCallback jcc,
144                      void *jcc_cls);
145
146
147 /**
148  * Cancel a job.  Must only be called before the job completion
149  * callback is called for the respective job.
150  *
151  * @param job job to cancel
152  */
153 void
154 GNUNET_CURL_job_cancel (struct GNUNET_CURL_Job *job);
155
156 #endif
157 /** @} */  /* end of group */
158
159 /* end of gnunet_curl_lib.h */