donau-httpd.h (6222B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2024 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify it under the 6 terms of the GNU Affero General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 TALER 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 Affero General Public License for more details. 12 13 You should have received a copy of the GNU Affero General Public License along with 14 TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file donau-httpd.h 18 * @brief Global declarations for the donau 19 * @author Florian Dold 20 * @author Benedikt Mueller 21 * @author Christian Grothoff 22 */ 23 #ifndef DONAU_HTTPD_H 24 #define DONAU_HTTPD_H 25 26 #include <microhttpd.h> 27 #include <taler/taler_json_lib.h> 28 #include <taler/taler_util.h> 29 #include <gnunet/gnunet_mhd_compat.h> 30 #include "donaudb_lib.h" 31 32 33 /** 34 * How long is caching /keys allowed at most? 35 */ 36 // extern struct GNUNET_TIME_Relative DH_max_keys_caching; 37 38 /** 39 * The donau's configuration. 40 */ 41 extern const struct GNUNET_CONFIGURATION_Handle *DH_cfg; 42 43 /** 44 * Main directory with donau data. 45 */ 46 extern char *DH_donau_directory; 47 48 /** 49 * Legal domain for this donau, to be shown to the user. 50 */ 51 extern char *DH_legal_domain; 52 53 /** 54 * True if we should commit suicide once all active 55 * connections are finished. Also forces /keys requests 56 * to terminate if they are long-polling. 57 */ 58 extern bool DH_suicide; 59 60 /** 61 * Value to return from main() 62 */ 63 extern int DH_global_ret; 64 65 /** 66 * Our DB context. 67 */ 68 extern struct DONAUDB_PostgresContext *DH_context; 69 70 /** 71 * Our currency. 72 */ 73 extern char *DH_currency; 74 75 /** 76 * Protocol version. 77 */ 78 extern char *DONAU_PROTOCOL_VERSION; 79 80 /** 81 * Our (externally visible) base URL. 82 */ 83 extern char *DH_base_url; 84 85 /** 86 * Are we shutting down? 87 */ 88 extern volatile bool MHD_terminating; 89 90 /** 91 * Context for all CURL operations (useful to the event loop) 92 */ 93 extern struct GNUNET_CURL_Context *DH_curl_ctx; 94 95 /** 96 * @brief Struct describing an URL and the handler for it. 97 */ 98 struct DH_RequestHandler; 99 100 101 /** 102 * @brief Context in which the donau is processing 103 * all requests 104 */ 105 struct DH_RequestContext 106 { 107 108 /** 109 * Async Scope ID associated with this request. 110 */ 111 struct GNUNET_AsyncScopeId async_scope_id; 112 113 /** 114 * When was this request started? 115 */ 116 struct GNUNET_TIME_Absolute start_time; 117 118 /** 119 * Opaque parsing context. 120 */ 121 void *opaque_post_parsing_context; 122 123 /** 124 * Request handler responsible for this request. 125 */ 126 const struct DH_RequestHandler *rh; 127 128 /** 129 * Request URL (for logging). 130 */ 131 const char *url; 132 133 /** 134 * Connection we are processing. 135 */ 136 struct MHD_Connection *connection; 137 138 /** 139 * @e rh-specific cleanup routine. Function called 140 * upon completion of the request that should 141 * clean up @a rh_ctx. Can be NULL. 142 */ 143 void 144 (*rh_cleaner)(struct DH_RequestContext *rc); 145 146 /** 147 * @e rh-specific context. Place where the request 148 * handler can associate state with this request. 149 * Can be NULL. 150 */ 151 void *rh_ctx; 152 }; 153 154 155 /** 156 * @brief Struct describing an URL and the handler for it. 157 */ 158 struct DH_RequestHandler 159 { 160 161 /** 162 * URL the handler is for (first part only). 163 */ 164 const char *url; 165 166 /** 167 * Method the handler is for. 168 */ 169 const char *method; 170 171 /** 172 * True if HTTP authorization via Bearer token is required. 173 */ 174 bool needs_authorization; 175 176 /** 177 * Callbacks for handling of the request. Which one is used 178 * depends on @e method. 179 */ 180 union 181 { 182 /** 183 * Function to call to handle GET requests (and those 184 * with @e method NULL). 185 * 186 * @param rc context for the request 187 * @param args array of arguments, needs to be of length @e args_expected 188 * @return MHD result code 189 */ 190 enum MHD_Result 191 (*get)(struct DH_RequestContext *rc, 192 const char *const args[]); 193 194 195 /** 196 * Function to call to handle POST requests. 197 * 198 * @param rc context for the request 199 * @param json uploaded JSON data 200 * @param args array of arguments, needs to be of length @e nargs 201 * @return MHD result code 202 */ 203 enum MHD_Result 204 (*post)(struct DH_RequestContext *rc, 205 const json_t *root, 206 const char *const args[]); 207 208 /** 209 * Function to call to handle PATCH requests. 210 * 211 * @param rc context for the request 212 * @param json uploaded JSON data 213 * @param args array of arguments, needs to be of length @e nargs 214 * @return MHD result code 215 */ 216 enum MHD_Result 217 (*patch)(struct DH_RequestContext *rc, 218 const json_t *root, 219 const char *const args[]); 220 221 /** 222 * Function to call to handle DELETE requests. 223 * 224 * @param rc context for the request 225 * @param args array of arguments, needs to be of length @e nargs 226 * @return MHD result code 227 */ 228 enum MHD_Result 229 (*delete)(struct DH_RequestContext *rc, 230 const char *const args[]); 231 232 } handler; 233 234 /** 235 * Mime type to use in reply (hint, can be NULL). 236 */ 237 const char *mime_type; 238 239 /** 240 * Raw data for the @e handler, can be NULL for none provided. 241 */ 242 const void *data; 243 244 /** 245 * Number of bytes in @e data, 0 for data is 0-terminated (!). 246 */ 247 size_t data_size; 248 249 /** 250 * Default response code. 0 for none provided. 251 */ 252 unsigned int response_code; 253 254 /** 255 * Number of arguments this handler expects in the @a args array. 256 */ 257 unsigned int nargs; 258 259 /** 260 * Is the number of arguments given in @e nargs only an upper bound, 261 * and calling with fewer arguments could be OK? 262 */ 263 bool nargs_is_upper_bound; 264 }; 265 266 /** 267 * Function to call to handle requests to "/keys" by sending 268 * back our current key material. 269 * 270 * @param rc request context 271 * @param args array of additional options (must be empty for this function) 272 * @return MHD result code 273 */ 274 // enum MHD_Result 275 // DH_keys_get_handler (struct DH_RequestContext *rc, 276 // const char *const args[]); 277 278 279 #endif