Skip to content

Commit 0ce6ce4

Browse files
committed
Runtime: Add FFI bindings for the curl_easy APIs
1 parent c27d9ae commit 0ce6ce4

7 files changed

Lines changed: 1620 additions & 7 deletions

File tree

BuildTools/NinjaBuildTools.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local format = string.format
55
local isWindows = (ffi.os == "Windows")
66
local isMacOS = (ffi.os == "OSX")
77

8-
local GCC_RELEASE_FLAGS = "-O3 -DNDEBUG"
8+
local GCC_RELEASE_FLAGS = "-O0 -DNDEBUG"
99
local GCC_DEBUG_FLAGS = "" -- None, for now (add -g -ggdb here as needed)
1010
local GCC_DIAGNOSTICS_FLAGS =
1111
"-Wall -Wextra -Wno-missing-field-initializers -Wno-unused-parameter -fvisibility=hidden -fno-strict-aliasing -fdiagnostics-color -Wfatal-errors"

Runtime/Bindings/FFI/curl/curl.lua

Lines changed: 249 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ local curl = {
88
}
99

1010
curl.cdefs = [[
11+
12+
// Aliased types (opaque to LuaJIT)
13+
typedef struct CURL CURL;
14+
typedef struct CURLU* url_ptr_t;
15+
typedef struct const CURLU* url_cptr_t;
16+
17+
// Exported from system.h
18+
typedef int64_t curl_off_t;
19+
20+
// Exported from curl.h
1121
typedef enum CURLversion {
1222
CURLVERSION_FIRST, /* 7.10 */
1323
CURLVERSION_SECOND, /* 7.11.1 */
@@ -88,6 +98,139 @@ struct curl_version_info_data {
8898
};
8999
typedef struct curl_version_info_data curl_version_info_data;
90100
101+
typedef enum {
102+
CURLE_OK = 0,
103+
CURLE_UNSUPPORTED_PROTOCOL, /* 1 */
104+
CURLE_FAILED_INIT, /* 2 */
105+
CURLE_URL_MALFORMAT, /* 3 */
106+
CURLE_NOT_BUILT_IN, /* 4 - [was obsoleted in August 2007 for
107+
7.17.0, reused in April 2011 for 7.21.5] */
108+
CURLE_COULDNT_RESOLVE_PROXY, /* 5 */
109+
CURLE_COULDNT_RESOLVE_HOST, /* 6 */
110+
CURLE_COULDNT_CONNECT, /* 7 */
111+
CURLE_WEIRD_SERVER_REPLY, /* 8 */
112+
CURLE_REMOTE_ACCESS_DENIED, /* 9 a service was denied by the server
113+
due to lack of access - when login fails
114+
this is not returned. */
115+
CURLE_FTP_ACCEPT_FAILED, /* 10 - [was obsoleted in April 2006 for
116+
7.15.4, reused in Dec 2011 for 7.24.0]*/
117+
CURLE_FTP_WEIRD_PASS_REPLY, /* 11 */
118+
CURLE_FTP_ACCEPT_TIMEOUT, /* 12 - timeout occurred accepting server
119+
[was obsoleted in August 2007 for 7.17.0,
120+
reused in Dec 2011 for 7.24.0]*/
121+
CURLE_FTP_WEIRD_PASV_REPLY, /* 13 */
122+
CURLE_FTP_WEIRD_227_FORMAT, /* 14 */
123+
CURLE_FTP_CANT_GET_HOST, /* 15 */
124+
CURLE_HTTP2, /* 16 - A problem in the http2 framing layer.
125+
[was obsoleted in August 2007 for 7.17.0,
126+
reused in July 2014 for 7.38.0] */
127+
CURLE_FTP_COULDNT_SET_TYPE, /* 17 */
128+
CURLE_PARTIAL_FILE, /* 18 */
129+
CURLE_FTP_COULDNT_RETR_FILE, /* 19 */
130+
CURLE_OBSOLETE20, /* 20 - NOT USED */
131+
CURLE_QUOTE_ERROR, /* 21 - quote command failure */
132+
CURLE_HTTP_RETURNED_ERROR, /* 22 */
133+
CURLE_WRITE_ERROR, /* 23 */
134+
CURLE_OBSOLETE24, /* 24 - NOT USED */
135+
CURLE_UPLOAD_FAILED, /* 25 - failed upload "command" */
136+
CURLE_READ_ERROR, /* 26 - could not open/read from file */
137+
CURLE_OUT_OF_MEMORY, /* 27 */
138+
CURLE_OPERATION_TIMEDOUT, /* 28 - the timeout time was reached */
139+
CURLE_OBSOLETE29, /* 29 - NOT USED */
140+
CURLE_FTP_PORT_FAILED, /* 30 - FTP PORT operation failed */
141+
CURLE_FTP_COULDNT_USE_REST, /* 31 - the REST command failed */
142+
CURLE_OBSOLETE32, /* 32 - NOT USED */
143+
CURLE_RANGE_ERROR, /* 33 - RANGE "command" did not work */
144+
CURLE_OBSOLETE34, /* 34 */
145+
CURLE_SSL_CONNECT_ERROR, /* 35 - wrong when connecting with SSL */
146+
CURLE_BAD_DOWNLOAD_RESUME, /* 36 - could not resume download */
147+
CURLE_FILE_COULDNT_READ_FILE, /* 37 */
148+
CURLE_LDAP_CANNOT_BIND, /* 38 */
149+
CURLE_LDAP_SEARCH_FAILED, /* 39 */
150+
CURLE_OBSOLETE40, /* 40 - NOT USED */
151+
CURLE_OBSOLETE41, /* 41 - NOT USED starting with 7.53.0 */
152+
CURLE_ABORTED_BY_CALLBACK, /* 42 */
153+
CURLE_BAD_FUNCTION_ARGUMENT, /* 43 */
154+
CURLE_OBSOLETE44, /* 44 - NOT USED */
155+
CURLE_INTERFACE_FAILED, /* 45 - CURLOPT_INTERFACE failed */
156+
CURLE_OBSOLETE46, /* 46 - NOT USED */
157+
CURLE_TOO_MANY_REDIRECTS, /* 47 - catch endless re-direct loops */
158+
CURLE_UNKNOWN_OPTION, /* 48 - User specified an unknown option */
159+
CURLE_SETOPT_OPTION_SYNTAX, /* 49 - Malformed setopt option */
160+
CURLE_OBSOLETE50, /* 50 - NOT USED */
161+
CURLE_OBSOLETE51, /* 51 - NOT USED */
162+
CURLE_GOT_NOTHING, /* 52 - when this is a specific error */
163+
CURLE_SSL_ENGINE_NOTFOUND, /* 53 - SSL crypto engine not found */
164+
CURLE_SSL_ENGINE_SETFAILED, /* 54 - can not set SSL crypto engine as
165+
default */
166+
CURLE_SEND_ERROR, /* 55 - failed sending network data */
167+
CURLE_RECV_ERROR, /* 56 - failure in receiving network data */
168+
CURLE_OBSOLETE57, /* 57 - NOT IN USE */
169+
CURLE_SSL_CERTPROBLEM, /* 58 - problem with the local certificate */
170+
CURLE_SSL_CIPHER, /* 59 - could not use specified cipher */
171+
CURLE_PEER_FAILED_VERIFICATION, /* 60 - peer's certificate or fingerprint
172+
was not verified fine */
173+
CURLE_BAD_CONTENT_ENCODING, /* 61 - Unrecognized/bad encoding */
174+
CURLE_OBSOLETE62, /* 62 - NOT IN USE since 7.82.0 */
175+
CURLE_FILESIZE_EXCEEDED, /* 63 - Maximum file size exceeded */
176+
CURLE_USE_SSL_FAILED, /* 64 - Requested FTP SSL level failed */
177+
CURLE_SEND_FAIL_REWIND, /* 65 - Sending the data requires a rewind
178+
that failed */
179+
CURLE_SSL_ENGINE_INITFAILED, /* 66 - failed to initialise ENGINE */
180+
CURLE_LOGIN_DENIED, /* 67 - user, password or similar was not
181+
accepted and we failed to login */
182+
CURLE_TFTP_NOTFOUND, /* 68 - file not found on server */
183+
CURLE_TFTP_PERM, /* 69 - permission problem on server */
184+
CURLE_REMOTE_DISK_FULL, /* 70 - out of disk space on server */
185+
CURLE_TFTP_ILLEGAL, /* 71 - Illegal TFTP operation */
186+
CURLE_TFTP_UNKNOWNID, /* 72 - Unknown transfer ID */
187+
CURLE_REMOTE_FILE_EXISTS, /* 73 - File already exists */
188+
CURLE_TFTP_NOSUCHUSER, /* 74 - No such user */
189+
CURLE_OBSOLETE75, /* 75 - NOT IN USE since 7.82.0 */
190+
CURLE_OBSOLETE76, /* 76 - NOT IN USE since 7.82.0 */
191+
CURLE_SSL_CACERT_BADFILE, /* 77 - could not load CACERT file, missing
192+
or wrong format */
193+
CURLE_REMOTE_FILE_NOT_FOUND, /* 78 - remote file not found */
194+
CURLE_SSH, /* 79 - error from the SSH layer, somewhat
195+
generic so the error message will be of
196+
interest when this has happened */
197+
198+
CURLE_SSL_SHUTDOWN_FAILED, /* 80 - Failed to shut down the SSL
199+
connection */
200+
CURLE_AGAIN, /* 81 - socket is not ready for send/recv,
201+
wait till it is ready and try again (Added
202+
in 7.18.2) */
203+
CURLE_SSL_CRL_BADFILE, /* 82 - could not load CRL file, missing or
204+
wrong format (Added in 7.19.0) */
205+
CURLE_SSL_ISSUER_ERROR, /* 83 - Issuer check failed. (Added in
206+
7.19.0) */
207+
CURLE_FTP_PRET_FAILED, /* 84 - a PRET command failed */
208+
CURLE_RTSP_CSEQ_ERROR, /* 85 - mismatch of RTSP CSeq numbers */
209+
CURLE_RTSP_SESSION_ERROR, /* 86 - mismatch of RTSP Session Ids */
210+
CURLE_FTP_BAD_FILE_LIST, /* 87 - unable to parse FTP file list */
211+
CURLE_CHUNK_FAILED, /* 88 - chunk callback reported error */
212+
CURLE_NO_CONNECTION_AVAILABLE, /* 89 - No connection available, the
213+
session will be queued */
214+
CURLE_SSL_PINNEDPUBKEYNOTMATCH, /* 90 - specified pinned public key did not
215+
match */
216+
CURLE_SSL_INVALIDCERTSTATUS, /* 91 - invalid certificate status */
217+
CURLE_HTTP2_STREAM, /* 92 - stream error in HTTP/2 framing layer
218+
*/
219+
CURLE_RECURSIVE_API_CALL, /* 93 - an api function was called from
220+
inside a callback */
221+
CURLE_AUTH_ERROR, /* 94 - an authentication function returned an
222+
error */
223+
CURLE_HTTP3, /* 95 - An HTTP/3 layer problem */
224+
CURLE_QUIC_CONNECT_ERROR, /* 96 - QUIC connection error */
225+
CURLE_PROXY, /* 97 - proxy handshake error */
226+
CURLE_SSL_CLIENTCERT, /* 98 - client-side certificate required */
227+
CURLE_UNRECOVERABLE_POLL, /* 99 - poll/select returned fatal error */
228+
CURLE_TOO_LARGE, /* 100 - a value/data met its maximum */
229+
CURLE_ECH_REQUIRED, /* 101 - ECH tried but failed */
230+
CURL_LAST /* never use! */
231+
} CURLcode;
232+
233+
// Exported from urlapi.h
91234
typedef enum {
92235
CURLUE_OK,
93236
CURLUE_BAD_HANDLE, /* 1 */
@@ -138,9 +281,6 @@ typedef enum {
138281
CURLUPART_ZONEID /* added in 7.65.0 */
139282
} CURLUPart;
140283
141-
typedef struct CURLU* url_ptr_t;
142-
typedef struct const CURLU* url_cptr_t;
143-
144284
typedef enum {
145285
CURLU_DEFAULT_FEATURES = 0 << 0,
146286
CURLU_DEFAULT_PORT = (1 << 0),
@@ -161,12 +301,75 @@ typedef enum {
161301
CURLU_NO_GUESS_SCHEME = (1 << 15),
162302
} CURLUFeatureFlags;
163303
304+
// Exported from easy.h
305+
typedef CURLcode curl_ssls_export_cb(CURL* handle,
306+
void* userptr,
307+
const char* session_key,
308+
const unsigned char* shmac,
309+
size_t shmac_len,
310+
const unsigned char* sdata,
311+
size_t sdata_len,
312+
curl_off_t valid_until,
313+
int ietf_tls_id,
314+
const char* alpn,
315+
size_t earlydata_max);
316+
317+
// Exported from header.h
318+
typedef enum {
319+
CURLHE_OK,
320+
CURLHE_BADINDEX, /* header exists but not with this index */
321+
CURLHE_MISSING, /* no such header exists */
322+
CURLHE_NOHEADERS, /* no headers at all exist (yet) */
323+
CURLHE_NOREQUEST, /* no request with this number was used */
324+
CURLHE_OUT_OF_MEMORY, /* out of memory while processing */
325+
CURLHE_BAD_ARGUMENT, /* a function argument was not okay */
326+
CURLHE_NOT_BUILT_IN /* if API was disabled in the build */
327+
} CURLHcode;
164328
struct static_curl_exports_table {
165329
// Exports from curl.h
166330
CURLversion (*curl_version_now)(void);
167331
curl_version_info_data* (*curl_version_info)(CURLversion);
168332
void (*curl_free)(void*);
169333
334+
// Exports from easy.h
335+
void (*curl_easy_cleanup)(CURL* handle);
336+
CURL* (*curl_easy_duphandle)(CURL* handle);
337+
char* (*curl_easy_escape)(CURL* handle, const char* string, int length);
338+
// CURLcode (*curl_easy_getinfo)(CURL* handle, CURLINFO info, ...);
339+
CURLHcode (*curl_easy_header)(CURL* handle,
340+
const char* name,
341+
size_t index,
342+
unsigned int origin,
343+
int request,
344+
struct curl_header** hout);
345+
CURL* (*curl_easy_init)(void);
346+
struct curl_header* (*curl_easy_nextheader)(CURL* handle,
347+
unsigned int origin,
348+
int request,
349+
struct curl_header* prev);
350+
// const struct curl_easyoption* (*curl_easy_option_by_id)(CURLoption id);
351+
const struct curl_easyoption* (*curl_easy_option_by_name)(const char* name);
352+
const struct curl_easyoption* (*curl_easy_option_next)(const struct curl_easyoption* prev);
353+
CURLcode (*curl_easy_pause)(CURL* handle, int bitmask);
354+
CURLcode (*curl_easy_perform)(CURL* handle);
355+
CURLcode (*curl_easy_recv)(CURL* handle, void* buffer, size_t buflen, size_t* n);
356+
void (*curl_easy_reset)(CURL* handle);
357+
CURLcode (*curl_easy_send)(CURL* handle, const void* buffer, size_t buflen, size_t* n);
358+
CURLcode (*curl_easy_setopt)(CURL* handle, const struct curl_easyoption* option, ...);
359+
CURLcode (*curl_easy_ssls_import)(CURL* handle,
360+
const char* session_key,
361+
const unsigned char* shmac,
362+
size_t shmac_len,
363+
const unsigned char* sdata,
364+
size_t sdata_len);
365+
CURLcode (*curl_easy_ssls_export)(CURL* handle,
366+
curl_ssls_export_cb* export_fn,
367+
void* userptr);
368+
const char* (*curl_easy_strerror)(CURLcode status);
369+
char* (*curl_easy_unescape)(CURL* handle, const char* input,
370+
int inlength, int* outlength);
371+
CURLcode (*curl_easy_upkeep)(CURL* handle);
372+
170373
// Exports from urlapi.h
171374
url_ptr_t (*curl_url)(void);
172375
void (*curl_url_cleanup)(url_ptr_t handle);
@@ -217,6 +420,49 @@ function curl.initialize()
217420

218421
url.__index = url
219422
curl.metatypes.CURLU = ffi.metatype("struct CURLU", url)
423+
424+
local easy = {}
425+
426+
-- void (*curl_easy_cleanup)(CURL* handle);
427+
-- CURL* (*curl_easy_duphandle)(CURL* handle);
428+
-- char* (*curl_easy_escape)(CURL* handle, const char* string, int length);
429+
-- // CURLcode (*curl_easy_getinfo)(CURL* handle, CURLINFO info, ...);
430+
-- CURLHcode (*curl_easy_header)(CURL* handle,
431+
-- const char* name,
432+
-- size_t index,
433+
-- unsigned int origin,
434+
-- int request,
435+
-- struct curl_header** hout);
436+
-- CURL* (*curl_easy_init)(void);
437+
-- struct curl_header* (*curl_easy_nextheader)(CURL* handle,
438+
-- unsigned int origin,
439+
-- int request,
440+
-- struct curl_header* prev);
441+
-- // const struct curl_easyoption* (*curl_easy_option_by_id)(CURLoption id);
442+
-- const struct curl_easyoption* (*curl_easy_option_by_name)(const char* name);
443+
-- const struct curl_easyoption* (*curl_easy_option_next)(const struct curl_easyoption* prev);
444+
-- CURLcode (*curl_easy_pause)(CURL* handle, int bitmask);
445+
-- CURLcode (*curl_easy_perform)(CURL* handle);
446+
-- CURLcode (*curl_easy_recv)(CURL* handle, void* buffer, size_t buflen, size_t* n);
447+
-- void (*curl_easy_reset)(CURL* handle);
448+
-- CURLcode (*curl_easy_send)(CURL* handle, const void* buffer, size_t buflen, size_t* n);
449+
-- CURLcode (*curl_easy_setopt)(CURL* handle, const struct curl_easyoption* option, ...);
450+
-- CURLcode (*curl_easy_ssls_import)(CURL* handle,
451+
-- const char* session_key,
452+
-- const unsigned char* shmac,
453+
-- size_t shmac_len,
454+
-- const unsigned char* sdata,
455+
-- size_t sdata_len);
456+
-- CURLcode (*curl_easy_ssls_export)(CURL* handle,
457+
-- curl_ssls_export_cb* export_fn,
458+
-- void* userptr);
459+
-- const char* (*curl_easy_strerror)(CURLcode status);
460+
-- char* (*curl_easy_unescape)(CURL* handle, const char* input,
461+
-- int inlength, int* outlength);
462+
-- CURLcode (*curl_easy_upkeep)(CURL* handle);
463+
464+
easy.__index = easy
465+
curl.metatypes.CURL = ffi.metatype("struct CURL", url)
220466
end
221467

222468
function curl.unpack(cstrings)

0 commit comments

Comments
 (0)