about summary refs log tree commit diff stats
path: root/src/bindings
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2022-07-24 22:58:59 +0200
committerbptato <nincsnevem662@gmail.com>2022-07-24 22:58:59 +0200
commit3803b94ffc58fb81e944633e83519b74bca8a7fe (patch)
tree7eb1a6cc860db969ddcf78f44c9cc9bd0e286aa0 /src/bindings
parent0bf3beb0a40bf58c627ccc79f46e7c20ad857a47 (diff)
downloadchawan-3803b94ffc58fb81e944633e83519b74bca8a7fe.tar.gz
Link to libcurl
Diffstat (limited to 'src/bindings')
-rw-r--r--src/bindings/curl.nim219
-rw-r--r--src/bindings/quickjs.nim47
2 files changed, 244 insertions, 22 deletions
diff --git a/src/bindings/curl.nim b/src/bindings/curl.nim
new file mode 100644
index 00000000..cf516d2d
--- /dev/null
+++ b/src/bindings/curl.nim
@@ -0,0 +1,219 @@
+const curllib = (func(): string =
+  when defined(windows): return "libcurl.dll"
+  elif defined(macos): return "libcurl(|.4|.4.8.0).dylib"
+  else: return "libcurl.so(|.4|.4.8.0)" # assume posix
+)()
+
+const
+  CURL_GLOBAL_SSL* = 1 shl 0 # no purpose since 7.57.0
+  CURL_GLOBAL_WIN32* = 1 shl 1
+  CURL_GLOBAL_ALL* = CURL_GLOBAL_SSL or CURL_GLOBAL_WIN32
+  CURL_GLOBAL_NOTHING* = 0
+  CURL_GLOBAL_DEFAULT* = CURL_GLOBAL_ALL
+  CURL_GLOBAL_ACK_EINTR* = 1 shl 2
+
+const
+  CURLOPTTYPE_LONG = 0
+  CURLOPTTYPE_OBJECTPOINT = 10000
+  CURLOPTTYPE_FUNCTIONPOINT = 20000
+  CURLOPTTYPE_OFF_T = 30000
+  CURLOPTTYPE_BLOB = 40000
+
+const
+  CURLOPTTYPE_STRINGPOINT = CURLOPTTYPE_OBJECTPOINT
+  CURLOPTTYPE_SLISTPOINT = CURLOPTTYPE_OBJECTPOINT
+  CURLOPTTYPE_CBPOINT = CURLOPTTYPE_OBJECTPOINT
+  CURLOPTTYPE_VALUES = CURLOPTTYPE_LONG
+
+{.push cdecl, dynlib: curllib.}
+
+type
+  CURL* = pointer
+
+  curl_mime_struct = object
+  curl_mime* = ptr curl_mime_struct
+  curl_mimepart_struct = object
+  curl_mimepart* = ptr curl_mimepart_struct
+  curl_slist_struct = object
+  curl_slist* = ptr curl_mimepart_struct
+
+  CURLoption* {.size: sizeof(cint).} = enum
+    # Long
+    CURLOPT_PORT = CURLOPTTYPE_LONG + 3
+    CURLOPT_POST = CURLOPTTYPE_LONG + 47
+    CURLOPT_FOLLOWLOCATION = CURLOPTTYPE_LONG + 52
+    CURLOPT_POSTFIELDSIZE = CURLOPTTYPE_LONG + 60
+    CURLOPT_HTTPGET = CURLOPTTYPE_LONG + 80
+
+    # Objectpoint
+    CURLOPT_WRITEDATA = CURLOPTTYPE_CBPOINT + 1
+    CURLOPT_URL = CURLOPTTYPE_STRINGPOINT + 2
+    CURLOPT_POSTFIELDS = CURLOPTTYPE_OBJECTPOINT + 15
+    CURLOPT_HTTPHEADER = CURLOPTTYPE_SLISTPOINT + 23
+    CURLOPT_HEADERDATA = CURLOPTTYPE_CBPOINT + 29
+    CURLOPT_MIMEPOST = CURLOPTTYPE_OBJECTPOINT + 269
+
+    # Functionpoint
+    CURLOPT_WRITEFUNCTION = CURLOPTTYPE_FUNCTIONPOINT + 11
+    CURLOPT_READFUNCTION = CURLOPTTYPE_FUNCTIONPOINT + 12
+    CURLOPT_HEADERFUNCTION = CURLOPTTYPE_FUNCTIONPOINT + 79
+
+    # Off-t
+    CURLOPT_INFILESIZE_LARGE = CURLOPTTYPE_OFF_T + 115
+    CURLOPT_RESUME_FROM_LARGE = CURLOPTTYPE_OFF_T + 116
+
+    # Blob
+    CURLOPT_SSLCERT_BLOB = CURLOPTTYPE_BLOB + 291
+    CURLOPT_SSLKEY_BLOB = CURLOPTTYPE_BLOB + 292
+    CURLOPT_PROXY_SSLCERT_BLOB = CURLOPTTYPE_BLOB + 293
+    CURLOPT_PROXY_SSLKEY_BLOB = CURLOPTTYPE_BLOB + 294
+    CURLOPT_ISSUECERT_BLOB = CURLOPTTYPE_BLOB + 295
+
+  CURLcode* {.size: sizeof(cint).} = enum
+    CURLE_OK = 0,
+    CURLE_UNSUPPORTED_PROTOCOL,    # 1 
+    CURLE_FAILED_INIT,             # 2 
+    CURLE_URL_MALFORMAT,           # 3 
+    CURLE_NOT_BUILT_IN,            # 4 - [was obsoleted in August 2007 for
+                                   # 7.17.0, reused in April 2011 for 7.21.5]
+    CURLE_COULDNT_RESOLVE_PROXY,   # 5 
+    CURLE_COULDNT_RESOLVE_HOST,    # 6 
+    CURLE_COULDNT_CONNECT,         # 7 
+    CURLE_WEIRD_SERVER_REPLY,      # 8 
+    CURLE_REMOTE_ACCESS_DENIED,    # 9 a service was denied by the server
+                                   # due to lack of access - when login fails
+                                   # this is not returned.
+    CURLE_FTP_ACCEPT_FAILED,       # 10 - [was obsoleted in April 2006 for
+                                   # 7.15.4, reused in Dec 2011 for 7.24.0]
+    CURLE_FTP_WEIRD_PASS_REPLY,    # 11 
+    CURLE_FTP_ACCEPT_TIMEOUT,      # 12 - timeout occurred accepting server
+                                   # [was obsoleted in August 2007 for 7.17.0,
+                                   # reused in Dec 2011 for 7.24.0]
+    CURLE_FTP_WEIRD_PASV_REPLY,    # 13 
+    CURLE_FTP_WEIRD_227_FORMAT,    # 14 
+    CURLE_FTP_CANT_GET_HOST,       # 15 
+    CURLE_HTTP2,                   # 16 - A problem in the http2 framing layer.
+                                   # [was obsoleted in August 2007 for 7.17.0,
+                                   # reused in July 2014 for 7.38.0]
+    CURLE_FTP_COULDNT_SET_TYPE,    # 17 
+    CURLE_PARTIAL_FILE,            # 18 
+    CURLE_FTP_COULDNT_RETR_FILE,   # 19 
+    CURLE_OBSOLETE20,              # 20 - NOT USED 
+    CURLE_QUOTE_ERROR,             # 21 - quote command failure 
+    CURLE_HTTP_RETURNED_ERROR,     # 22 
+    CURLE_WRITE_ERROR,             # 23 
+    CURLE_OBSOLETE24,              # 24 - NOT USED 
+    CURLE_UPLOAD_FAILED,           # 25 - failed upload "command" 
+    CURLE_READ_ERROR,              # 26 - couldn't open/read from file 
+    CURLE_OUT_OF_MEMORY,           # 27 
+    CURLE_OPERATION_TIMEDOUT,      # 28 - the timeout time was reached 
+    CURLE_OBSOLETE29,              # 29 - NOT USED 
+    CURLE_FTP_PORT_FAILED,         # 30 - FTP PORT operation failed 
+    CURLE_FTP_COULDNT_USE_REST,    # 31 - the REST command failed 
+    CURLE_OBSOLETE32,              # 32 - NOT USED 
+    CURLE_RANGE_ERROR,             # 33 - RANGE "command" didn't work 
+    CURLE_HTTP_POST_ERROR,         # 34 
+    CURLE_SSL_CONNECT_ERROR,       # 35 - wrong when connecting with SSL 
+    CURLE_BAD_DOWNLOAD_RESUME,     # 36 - couldn't resume download 
+    CURLE_FILE_COULDNT_READ_FILE,  # 37 
+    CURLE_LDAP_CANNOT_BIND,        # 38 
+    CURLE_LDAP_SEARCH_FAILED,      # 39 
+    CURLE_OBSOLETE40,              # 40 - NOT USED 
+    CURLE_FUNCTION_NOT_FOUND,      # 41 - NOT USED starting with 7.53.0 
+    CURLE_ABORTED_BY_CALLBACK,     # 42 
+    CURLE_BAD_FUNCTION_ARGUMENT,   # 43 
+    CURLE_OBSOLETE44,              # 44 - NOT USED 
+    CURLE_INTERFACE_FAILED,        # 45 - CURLOPT_INTERFACE failed 
+    CURLE_OBSOLETE46,              # 46 - NOT USED 
+    CURLE_TOO_MANY_REDIRECTS,      # 47 - catch endless re-direct loops 
+    CURLE_UNKNOWN_OPTION,          # 48 - User specified an unknown option 
+    CURLE_SETOPT_OPTION_SYNTAX,    # 49 - Malformed setopt option 
+    CURLE_OBSOLETE50,              # 50 - NOT USED 
+    CURLE_OBSOLETE51,              # 51 - NOT USED 
+    CURLE_GOT_NOTHING,             # 52 - when this is a specific error 
+    CURLE_SSL_ENGINE_NOTFOUND,     # 53 - SSL crypto engine not found 
+    CURLE_SSL_ENGINE_SETFAILED,    # 54 - can not set SSL crypto engine as
+                                   # default
+    CURLE_SEND_ERROR,              # 55 - failed sending network data 
+    CURLE_RECV_ERROR,              # 56 - failure in receiving network data 
+    CURLE_OBSOLETE57,              # 57 - NOT IN USE 
+    CURLE_SSL_CERTPROBLEM,         # 58 - problem with the local certificate 
+    CURLE_SSL_CIPHER,              # 59 - couldn't use specified cipher 
+    CURLE_PEER_FAILED_VERIFICATION, # 60 - peer's certificate or fingerprint
+                                   # wasn't verified fine
+    CURLE_BAD_CONTENT_ENCODING,    # 61 - Unrecognized/bad encoding 
+    CURLE_OBSOLETE62,              # 62 - NOT IN USE since 7.82.0 
+    CURLE_FILESIZE_EXCEEDED,       # 63 - Maximum file size exceeded 
+    CURLE_USE_SSL_FAILED,          # 64 - Requested FTP SSL level failed 
+    CURLE_SEND_FAIL_REWIND,        # 65 - Sending the data requires a rewind
+                                   # that failed
+    CURLE_SSL_ENGINE_INITFAILED,   # 66 - failed to initialise ENGINE 
+    CURLE_LOGIN_DENIED,            # 67 - user, password or similar was not
+                                   # accepted and we failed to login
+    CURLE_TFTP_NOTFOUND,           # 68 - file not found on server 
+    CURLE_TFTP_PERM,               # 69 - permission problem on server 
+    CURLE_REMOTE_DISK_FULL,        # 70 - out of disk space on server 
+    CURLE_TFTP_ILLEGAL,            # 71 - Illegal TFTP operation 
+    CURLE_TFTP_UNKNOWNID,          # 72 - Unknown transfer ID 
+    CURLE_REMOTE_FILE_EXISTS,      # 73 - File already exists 
+    CURLE_TFTP_NOSUCHUSER,         # 74 - No such user 
+    CURLE_CONV_FAILED,             # 75 - conversion failed 
+    CURLE_OBSOLETE76,              # 76 - NOT IN USE since 7.82.0 
+    CURLE_SSL_CACERT_BADFILE,      # 77 - could not load CACERT file, missing
+                                   # or wrong format
+    CURLE_REMOTE_FILE_NOT_FOUND,   # 78 - remote file not found 
+    CURLE_SSH,                     # 79 - error from the SSH layer, somewhat
+                                   # generic so the error message will be of
+                                   # interest when this has happened
+
+    CURLE_SSL_SHUTDOWN_FAILED,     # 80 - Failed to shut down the SSL
+                                   # connection
+    CURLE_AGAIN,                   # 81 - socket is not ready for send/recv,
+                                   # wait till it's ready and try again (Added
+                                   # in 7.18.2)
+    CURLE_SSL_CRL_BADFILE,         # 82 - could not load CRL file, missing or
+                                   # wrong format (Added in 7.19.0)
+    CURLE_SSL_ISSUER_ERROR,        # 83 - Issuer check failed.  (Added in
+                                   # 7.19.0)
+    CURLE_FTP_PRET_FAILED,         # 84 - a PRET command failed 
+    CURLE_RTSP_CSEQ_ERROR,         # 85 - mismatch of RTSP CSeq numbers 
+    CURLE_RTSP_SESSION_ERROR,      # 86 - mismatch of RTSP Session Ids 
+    CURLE_FTP_BAD_FILE_LIST,       # 87 - unable to parse FTP file list 
+    CURLE_CHUNK_FAILED,            # 88 - chunk callback reported error 
+    CURLE_NO_CONNECTION_AVAILABLE, # 89 - No connection available, the
+                                   # session will be queued
+    CURLE_SSL_PINNEDPUBKEYNOTMATCH, # 90 - specified pinned public key did not
+                                   #  match
+    CURLE_SSL_INVALIDCERTSTATUS,   # 91 - invalid certificate status 
+    CURLE_HTTP2_STREAM,            # 92 - stream error in HTTP/2 framing layer
+    CURLE_RECURSIVE_API_CALL,      # 93 - an api function was called from
+                                   # inside a callback
+    CURLE_AUTH_ERROR,              # 94 - an authentication function returned an
+                                   # error
+    CURLE_HTTP3,                   # 95 - An HTTP/3 layer problem 
+    CURLE_QUIC_CONNECT_ERROR,      # 96 - QUIC connection error 
+    CURLE_PROXY,                   # 97 - proxy handshake error 
+    CURLE_SSL_CLIENTCERT,          # 98 - client-side certificate required 
+    CURLE_UNRECOVERABLE_POLL,      # 99 - poll/select returned fatal error 
+    CURL_LAST # never use! 
+
+proc curl_global_init*(flags: clong): CURLcode {.importc: "curl_global_init".}
+proc curl_global_cleanup*() {.importc: "curl_global_cleanup".}
+
+proc curl_easy_init*(): CURL {.importc: "curl_easy_init".}
+proc curl_easy_cleanup*(handle: CURL) {.importc: "curl_easy_cleanup".}
+proc curl_easy_setopt*(handle: CURL, option: CURLoption) {.importc: "curl_easy_setopt", varargs.}
+proc curl_easy_perform*(handle: CURL): CURLcode {.importc: "curl_easy_perform".}
+
+proc curl_mime_init*(handle: CURL): curl_mime {.importc: "curl_mime_init".}
+proc curl_mime_free*(mime: curl_mime) {.importc: "curl_mime_free".}
+proc curl_mime_addpart*(mime: curl_mime): curl_mimepart {.importc: "curl_mime_addpart".}
+proc curl_mime_name*(part: curl_mimepart, name: cstring) {.importc: "curl_mime_name".}
+proc curl_mime_data*(part: curl_mimepart, data: cstring, datasize: csize_t) {.importc: "curl_mime_name".}
+proc curl_mime_filename*(part: curl_mimepart, name: cstring) {.importc: "curl_mime_filename".}
+proc curl_mime_filedata*(part: curl_mimepart, filename: cstring) {.importc: "curl_mime_filedata".}
+
+proc curl_slist_append*(slist: curl_slist, str: cstring): curl_slist {.importc: "curl_slist_append".}
+proc curl_slist_free_all*(slist: curl_slist) {.importc: "curl_slist_free_all".}
+
+{.pop.}
diff --git a/src/bindings/quickjs.nim b/src/bindings/quickjs.nim
index 73cba0d2..edda5c39 100644
--- a/src/bindings/quickjs.nim
+++ b/src/bindings/quickjs.nim
@@ -92,41 +92,44 @@ const
   JS_EVAL_FLAG_STRIP* = (1 shl 4) ##  force 'strip' mode
   JS_EVAL_FLAG_COMPILE_ONLY* = (1 shl 5) ##  internal use
 
-proc JS_NewRuntime*(): JSRuntime {.importc: "JS_NewRuntime", header: qjsheader.}
-proc JS_FreeRuntime*(rt: JSRuntime) {.importc: "JS_FreeRuntime", header: qjsheader.}
+{.push header: qjsheader, cdecl.}
 
-proc JS_NewContext*(rt: JSRuntime): JSContext {.importc: "JS_NewContext", header: qjsheader.}
-proc JS_NewContextRaw*(rt: JSRuntime): JSContext {.importc: "JS_NewContextRaw", header: qjsheader.}
-proc JS_FreeContext*(ctx: JSContext) {.importc: "JS_FreeContext", header: qjsheader.}
+proc JS_NewRuntime*(): JSRuntime {.importc: "JS_NewRuntime".}
+proc JS_FreeRuntime*(rt: JSRuntime) {.importc: "JS_FreeRuntime".}
 
-proc JS_GetGlobalObject*(ctx: JSContext): JSValue {.importc: "JS_GetGlobalObject", header: qjsheader.}
+proc JS_NewContext*(rt: JSRuntime): JSContext {.importc: "JS_NewContext".}
+proc JS_NewContextRaw*(rt: JSRuntime): JSContext {.importc: "JS_NewContextRaw".}
+proc JS_FreeContext*(ctx: JSContext) {.importc: "JS_FreeContext".}
 
-proc JS_NewObject*(ctx: JSContext): JSValue {.importc: "JS_NewObject", header: qjsheader.}
+proc JS_GetGlobalObject*(ctx: JSContext): JSValue {.importc: "JS_GetGlobalObject".}
 
-proc JS_NewCFunction2*(ctx: JSContext, cfunc: JSCFunction, name: cstring, length: int, proto: JSCFunctionEnum, magic: int): JSValue {.importc: "JS_NewCFunction2", header: qjsheader.}
-proc JS_NewCFunction*(ctx: JSContext, cfunc: JSCFunction, name: cstring, length: int): JSValue  {.importc: "JS_NewCFunction", header: qjsheader.}
+proc JS_NewObject*(ctx: JSContext): JSValue {.importc: "JS_NewObject".}
+
+proc JS_NewCFunction2*(ctx: JSContext, cfunc: JSCFunction, name: cstring, length: int, proto: JSCFunctionEnum, magic: int): JSValue {.importc: "JS_NewCFunction2".}
+proc JS_NewCFunction*(ctx: JSContext, cfunc: JSCFunction, name: cstring, length: int): JSValue  {.importc: "JS_NewCFunction".}
 
 proc JS_SetPropertyStr*(ctx: JSContext, this_obj: JSValue, prop: cstring, val: JSValue): int 
- {.importc: "JS_SetPropertyStr", header: qjsheader.}
+ {.importc: "JS_SetPropertyStr".}
 proc JS_GetPropertyStr*(ctx: JSContext, this_obj: JSValue, prop: cstring): JSValue
- {.importc: "JS_GetPropertyStr", header: qjsheader.}
+ {.importc: "JS_GetPropertyStr".}
 
-proc JS_FreeValue*(ctx: JSContext, v: JSValue) {.importc: "JS_FreeValue", header: qjsheader.}
+proc JS_FreeValue*(ctx: JSContext, v: JSValue) {.importc: "JS_FreeValue".}
 
 # use toString if possible
-proc JS_ToCStringLen*(ctx: JSContext, plen: ptr int, val1: JSValue): cstring {.importc: "JS_ToCStringLen", header: qjsheader.}
-proc JS_ToCString*(ctx: JSContext, val1: JSValue): cstring {.importc: "JS_ToCString", header: qjsheader.}
-proc JS_FreeCString*(ctx: JSContext, `ptr`: cstring) {.importc: "JS_FreeCString", header: qjsheader.}
+proc JS_ToCStringLen*(ctx: JSContext, plen: ptr int, val1: JSValue): cstring {.importc: "JS_ToCStringLen".}
+proc JS_ToCString*(ctx: JSContext, val1: JSValue): cstring {.importc: "JS_ToCString".}
+proc JS_FreeCString*(ctx: JSContext, `ptr`: cstring) {.importc: "JS_FreeCString".}
 
-proc JS_Eval*(ctx: JSContext, input: cstring, input_len: int, filename: cstring, eval_flags: int): JSValue {.importc: "JS_Eval", header: qjsheader.}
+proc JS_Eval*(ctx: JSContext, input: cstring, input_len: int, filename: cstring, eval_flags: int): JSValue {.importc: "JS_Eval".}
 
-proc JS_IsException*(v: JSValue): bool {.importc: "JS_IsException", header: qjsheader.}
-proc JS_IsError*(v: JSValue): bool {.importc: "JS_IsError", header: qjsheader.}
-proc JS_IsUndefined*(v: JSValue): bool {.importc: "JS_IsUndefined", header: qjsheader.}
+proc JS_IsException*(v: JSValue): bool {.importc: "JS_IsException".}
+proc JS_IsError*(v: JSValue): bool {.importc: "JS_IsError".}
+proc JS_IsUndefined*(v: JSValue): bool {.importc: "JS_IsUndefined".}
 
-proc JS_GetException*(ctx: JSContext): JSValue {.importc: "JS_GetException", header: qjsheader.}
+proc JS_GetException*(ctx: JSContext): JSValue {.importc: "JS_GetException".}
 
-proc JS_SetContextOpaque*(ctx: JSContext, opaque: pointer) {.importc: "JS_SetContextOpaque", header: qjsheader.}
-proc JS_GetContextOpaque*(ctx: JSContext): pointer {.importc: "JS_GetContextOpaque", header: qjsheader.}
+proc JS_SetContextOpaque*(ctx: JSContext, opaque: pointer) {.importc: "JS_SetContextOpaque".}
+proc JS_GetContextOpaque*(ctx: JSContext): pointer {.importc: "JS_GetContextOpaque".}
 
 proc js_free_rt*(rt: JSRuntime, p: pointer) {.importc: "js_free_rt".}
+{.pop.}