quickjs-http.h (4789B)
1 /* 2 This file is part of GNU Taler 3 Copyright (C) 2024 Taler Systems SA 4 5 GNU 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 GNU 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 GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 17 18 // ## Native HTTP client library support. 19 20 // Considerations: 21 // - the API is designed for the HTTP client implementation 22 // to run in its own thread and *not* be integrated with the 23 // application's main event loop. 24 // - focus on small API 25 // - not a generic HTTP client, only supposed to serve the needs 26 // of a JS runtime 27 // - only very tiny subset of HTTP supported 28 // - no request/response streaming 29 // - should be appropriate to implement a JS HTTP fetch function 30 // in the style of WHATWG fetch 31 // - no focus on ABI compatibility whatsoever 32 33 34 #ifndef _QUICKJS_HTTP_H 35 #define _QUICKJS_HTTP_H 36 37 #include <stdint.h> 38 #include <limits.h> 39 #include <stddef.h> 40 41 // Forward declaration; 42 struct JSHttpResponseInfo; 43 44 /** 45 * Callback called when an HTTP response has arrived. 46 * 47 * IMPORTANT: May be called from an arbitrary thread. 48 */ 49 typedef void (*JSHttpResponseCb)(void *cls, struct JSHttpResponseInfo *resp); 50 51 enum JSHttpRedirectFlag { 52 /** 53 * Handle redirects transparently. 54 */ 55 JS_HTTP_REDIRECT_TRANSPARENT = 0, 56 /** 57 * Redirect status codes are returned to the client. 58 * The client can choose to follow them manually (or not). 59 */ 60 JS_HTTP_REDIRECT_MANUAL = 1, 61 /** 62 * All redirect status codes result in an error. 63 */ 64 JS_HTTP_REDIRECT_ERROR = 2, 65 }; 66 67 /** 68 * Info needed to start a new HTTP request. 69 */ 70 struct JSHttpRequestInfo { 71 /** 72 * Callback called with the response for the request. 73 */ 74 JSHttpResponseCb response_cb; 75 76 /** 77 * Closure for response_cb. 78 */ 79 void *response_cb_cls; 80 81 /** 82 * Request URL. 83 */ 84 const char *url; 85 86 /** 87 * Request method. 88 */ 89 const char *method; 90 91 /** 92 * NULL-terminated array of request headers. 93 */ 94 char **request_headers; 95 96 /** 97 * 0: Handle redirects transparently. 98 * 1: Handle redirects manually. 99 * 2: Redirects result in an error. 100 */ 101 enum JSHttpRedirectFlag redirect; 102 103 /** 104 * Request timeout in milliseconds. 105 * 106 * When 0 is specified, the timeout is the default request 107 * timeout for the platform. 108 * 109 * When -1 is specified, there is no timeout. This might not be 110 * supported on all platforms. 111 */ 112 int timeout_ms; 113 114 /** 115 * Enable debug output for this request. 116 */ 117 int debug; 118 119 /** 120 * Request body or NULL. 121 */ 122 void *req_body; 123 124 /** 125 * Length or request body or 0. 126 */ 127 uint32_t req_body_len; 128 }; 129 130 /** 131 * Contents of an HTTP response. 132 */ 133 struct JSHttpResponseInfo { 134 135 /** 136 * Request that this is a response to. 137 * 138 * (Think of the request ID like a file descriptor number). 139 */ 140 int request_id; 141 142 /** 143 * HTTP response status code or 0 on error. 144 */ 145 int status; 146 147 /** 148 * When status is 0, error message. 149 */ 150 char *errmsg; 151 152 /** 153 * Array of `num_response_headers` response headers. 154 */ 155 char **response_headers; 156 157 /** 158 * Number of response headers. 159 */ 160 int num_response_headers; 161 162 /** 163 * Response body or NULL. 164 */ 165 void *body; 166 167 /** 168 * Length of the response body or 0. 169 */ 170 uint32_t body_len; 171 }; 172 173 /** 174 * Callback called when an HTTP response has arrived. 175 * 176 * IMPORTANT: May be called from an arbitrary thread. 177 */ 178 typedef void (*JSHttpResponseCb)(void *cls, struct JSHttpResponseInfo *resp); 179 180 /** 181 * Function to create a new HTTP fetch request. 182 * The request can still be configured until it is started. 183 * An identifier for the request will be written to @a handle. 184 * 185 * @return negative number on error, positive request_id on success 186 */ 187 typedef int (*JSHttpReqCreateFn)(void *cls, struct JSHttpRequestInfo *req_info); 188 189 /** 190 * Cancel a request. The request_id will become invalid 191 * and the callback won't be called with request_id. 192 */ 193 typedef int (*JSHttpReqCancelFn)(void *cls, int request_id); 194 195 struct JSHttpClientImplementation { 196 /** 197 * Opaque closure passed to client functions. 198 */ 199 void *cls; 200 JSHttpReqCreateFn req_create; 201 JSHttpReqCancelFn req_cancel; 202 }; 203 204 205 struct JSHttpClientImplementation * 206 js_curl_http_client_create(void); 207 208 void 209 js_curl_http_client_destroy(struct JSHttpClientImplementation *impl); 210 211 #endif /* _QUICKJS_HTTP_H */