libs-imp-curl.lmt /size: 10 Kb    last modification: 2021-10-28 13:51
1if not modules then modules = { } end modules ['libs-imp-curl'] = {
2    version   = 1.001,
3    comment   = "companion to luat-lib.mkxl",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9-- c:/data/develop/tex-context/tex/texmf-win64/bin/lib/luametatex/lua/copies/curl/libcurl.dll
10
11local libname = "curl"
12local libfile = "libcurl"
13
14local curllib = resolvers.libraries.validoptional(libname)
15
16if not curllib then return end
17
18-- We're good, so we continue.
19
20local next, type = next, type
21local lower, gsub = string.lower, string.gsub
22local dirname = file.dirname
23
24local mapping = {
25    ["acceptencoding"]          = 102,
26    ["accepttimeoutms"]         = 212,
27    ["addressscope"]            = 171,
28    ["append"]                  =  50,
29    ["autoreferer"]             =  58,
30    ["buffersize"]              =  98,
31    ["cainfo"]                  =  65,
32    ["capath"]                  =  97,
33    ["certinfo"]                = 172,
34 -- ["chunkbgnfunction"]        = 198,
35    ["chunkdata"]               = 201,
36 -- ["chunkendfunction"]        = 199,
37    ["closepolicy"]             =  72,
38    ["closesocketdata"]         = 209,
39 -- ["closesocketfunction"]     = 208,
40    ["connectonly"]             = 141,
41    ["connecttimeout"]          =  78,
42    ["connecttimeoutms"]        = 156,
43 -- ["convfromnetworkfunction"] = 142,
44 -- ["convfromutf8function"]    = 144,
45 -- ["convtonetworkfunction"]   = 143,
46    ["cookie"]                  =  22,
47    ["cookiefile"]              =  31,
48    ["cookiejar"]               =  82,
49    ["cookielist"]              = 135,
50    ["cookiesessionv"]          =  96,
51    ["copypostfields"]          = 165,
52    ["crlf"]                    =  27,
53    ["crlfile"]                 = 169,
54    ["customrequest"]           =  36,
55    ["debugdata"]               =  95,
56 -- ["debugfunction"]           =  94,
57    ["dirlistonly"]             =  48,
58    ["dnscachetimeout"]         =  92,
59    ["dnsinterface"]            = 221,
60    ["dnslocalip4"]             = 222,
61    ["dnslocalip6"]             = 223,
62    ["dnsservers"]              = 211,
63    ["dnsuseglobalcache"]       =  91,
64    ["egdsocket"]               =  77,
65    ["errorbuffer"]             =  10,
66    ["expect100timeoutms"]      = 227,
67    ["failonerror"]             =  45,
68    ["file"]                    =   1,
69    ["filetime"]                =  69,
70    ["fnmatchdata"]             = 202,
71 -- ["fnmatchfunction"]         = 200,
72    ["followlocation"]          =  52,
73    ["forbidreuse"]             =  75,
74    ["freshconnect"]            =  74,
75    ["ftpaccount"]              = 134,
76    ["ftpalternativetouser"]    = 147,
77    ["ftpcreatemissingdirs"]    = 110,
78    ["ftpfilemethod"]           = 138,
79    ["ftpresponsetimeout"]      = 112,
80    ["ftpskippasvip"]           = 137,
81    ["ftpsslccc"]               = 154,
82    ["ftpuseeprt"]              = 106,
83    ["ftpuseepsv"]              =  85,
84    ["ftpusepret"]              = 188,
85    ["ftpport"]                 =  17,
86    ["ftpsslauth"]              = 129,
87    ["gssapidelegation"]        = 210,
88    ["header"]                  =  42,
89    ["headerdata"]              =  29,
90 -- ["headerfunction"]          =  79,
91    ["http200aliases"]          = 104,
92    ["httpcontentdecoding"]     = 158,
93    ["httptransferdecoding"]    = 157,
94    ["httpversion"]             =  84,
95    ["httpauth"]                = 107,
96    ["httpget"]                 =  80,
97    ["httpheader"]              =  23,
98    ["httppost"]                =  24,
99    ["httpproxytunnel"]         =  61,
100    ["ignorecontentlength"]     = 136,
101    ["infile"]                  =   9,
102    ["infilesize"]              =  14,
103    ["infilesizelarge"]         = 115,
104    ["interface"]               =  62,
105    ["interleavedata"]          = 195,
106 -- ["interleavefunction"]      = 196,
107    ["ioctldata"]               = 131,
108 -- ["ioctlfunction"]           = 130,
109    ["ipresolve"]               = 113,
110    ["issuercert"]              = 170,
111    ["keypasswd"]               =  26,
112    ["krblevel"]                =  63,
113    ["localport"]               = 139,
114    ["localportrange"]          = 140,
115    ["loginoptions"]            = 224,
116    ["lowspeedlimit"]           =  19,
117    ["lowspeedtime"]            =  20,
118    ["mailauth"]                = 217,
119    ["mailfrom"]                = 186,
120    ["mailrcpt"]                = 187,
121    ["maxrecvspeedlarge"]       = 146,
122    ["maxsendspeedlarge"]       = 145,
123    ["maxconnects"]             =  71,
124    ["maxfilesize"]             = 114,
125    ["maxfilesizelarge"]        = 117,
126    ["maxredirs"]               =  68,
127    ["netrc"]                   =  51,
128    ["netrcfile"]               = 118,
129    ["newdirectoryperms"]       = 160,
130    ["newfileperms"]            = 159,
131    ["nobody"]                  =  44,
132    ["noprogress"]              =  43,
133    ["noproxy"]                 = 177,
134    ["nosignal"]                =  99,
135    ["opensocketdata"]          = 164,
136 -- ["opensocketfunction"]      = 163,
137    ["password"]                = 174,
138    ["port"]                    =   3,
139    ["post"]                    =  47,
140 -- ["postfields"]              =  15,
141 -- ["postfieldsize"]           =  60,
142 -- ["postfieldsizelarge"]      = 120,
143    ["postquote"]               =  39,
144    ["postredir"]               = 161,
145    ["prequote"]                =  93,
146    ["private"]                 = 103,
147    ["progressdata"]            =  57,
148 -- ["progressfunction"]        =  56,
149    ["protocols"]               = 181,
150    ["proxy"]                   =   4,
151    ["proxytransfermode"]       = 166,
152    ["proxyauth"]               = 111,
153    ["proxypassword"]           = 176,
154    ["proxyport"]               =  59,
155    ["proxytype"]               = 101,
156    ["proxyusername"]           = 175,
157    ["proxyuserpwd"]            =   6,
158    ["put"]                     =  54,
159    ["quote"]                   =  28,
160    ["randomfile"]              =  76,
161    ["range"]                   =   7,
162    ["readdata"]                =   9,
163 -- ["readfunction"]            =  12,
164    ["redirprotocols"]          = 182,
165    ["referer"]                 =  16,
166    ["resolve"]                 = 203,
167    ["resumefrom"]              =  21,
168    ["resumefromlarge"]         = 116,
169    ["rtspclientcseq"]          = 193,
170    ["rtsprequest"]             = 189,
171    ["rtspservercseq"]          = 194,
172    ["rtspsessionid"]           = 190,
173    ["rtspstreamuri"]           = 191,
174    ["rtsptransport"]           = 192,
175    ["rtspheader"]              =  23,
176    ["saslir"]                  = 218,
177    ["seekdata"]                = 168,
178 -- ["seekfunction"]            = 167,
179    ["serverresponsetimeout"]   = 112,
180    ["share"]                   = 100,
181    ["sockoptdata"]             = 149,
182 -- ["sockoptfunction"]         = 148,
183    ["socks5gssapinec"]         = 180,
184    ["socks5gssapiservice"]     = 179,
185    ["sshauthtypes"]            = 151,
186    ["sshhostpublickeymd5"]     = 162,
187    ["sshkeydata"]              = 185,
188 -- ["sshkeyfunction"]          = 184,
189    ["sshknownhosts"]           = 183,
190    ["sshprivatekeyfile"]       = 153,
191    ["sshpublickeyfile"]        = 152,
192    ["sslcipherlist"]           =  83,
193    ["sslctxdata"]              = 109,
194 -- ["sslctxfunction"]          = 108,
195    ["sslenablealpn"]           = 226,
196    ["sslenablenpn"]            = 225,
197    ["ssloptions"]              = 216,
198    ["sslsessionidcache"]       = 150,
199    ["sslverifyhost"]           =  81,
200    ["sslverifypeer"]           =  64,
201    ["sslcert"]                 =  25,
202    ["sslcerttype"]             =  86,
203    ["sslengine"]               =  89,
204    ["sslenginedefault"]        =  90,
205    ["sslkey"]                  =  87,
206    ["sslkeytype"]              =  88,
207    ["sslversion"]              =  32,
208    ["stderr"]                  =  37,
209    ["tcpkeepalive"]            = 213,
210    ["tcpkeepidle"]             = 214,
211    ["tcpkeepintvl"]            = 215,
212    ["tcpnodelay"]              = 121,
213    ["telnetoptions"]           =  70,
214    ["tftpblksize"]             = 178,
215    ["timecondition"]           =  33,
216    ["timeout"]                 =  13,
217    ["timeoutms"]               = 155,
218    ["timevalue"]               =  34,
219    ["tlsauthpassword"]         = 205,
220    ["tlsauthtype"]             = 206,
221    ["tlsauthusername"]         = 204,
222    ["transferencoding"]        = 207,
223    ["transfertext"]            =  53,
224    ["unrestrictedauth"]        = 105,
225    ["upload"]                  =  46,
226    ["url"]                     =   2,
227    ["usessl"]                  = 119,
228    ["useragent"]               =  18,
229    ["username"]                = 173,
230    ["userpwd"]                 =   5,
231    ["verbose"]                 =  41,
232    ["wildcardmatch"]           = 197,
233    ["writedata"]               =   1,
234 -- ["writefunction"]           =  11,
235    ["writeheader"]             =  29,
236    ["writeinfo"]               =  40,
237    ["xferinfodata"]            =  57,
238 -- ["xferinfofunction"]        = 219,
239    ["xoauth2bearer"]           = 220,
240}
241
242table.setmetatableindex(mapping,function(t,k)
243    local s = gsub(lower(k),"[^a-z0-9]","")
244    local v = rawget(t,s) or false
245    t[k] = v
246    return v
247end)
248
249local curl_fetch      = curllib.fetch
250local curl_escape     = curllib.escape
251local curl_unescape   = curllib.unescape
252local curl_getversion = curllib.getversion
253
254local report          = logs.reporter(libname)
255
256local function okay()
257    if resolvers.libraries.optionalloaded(libname,libfile) then
258        okay = function() return true end
259    else
260        okay = function() return false end
261    end
262    return okay()
263end
264
265local function fetch(options)
266    if okay() then
267        local t = type(options)
268        if t == "table" then
269            local o = { }
270            for name, value in next, options do
271                local index = mapping[name]
272                if index then
273                    o[index] = value
274                end
275            end
276         -- if o.capath == true then
277         --     o.capath = libpath
278         -- end
279            return curl_fetch(o)
280        elseif t == "string" then
281            return curl_fetch { [mapping.url] = options }
282        else
283            report("invalid argument")
284        end
285    else
286        report("no curl library loaded")
287    end
288end
289
290local curl = {
291    getversion = function ()  return okay() and curl_getversion()  end,
292    escape     = function (s) return okay() and curl_escape    (s) end,
293    unescape   = function (s) return okay() and curl_unescape  (s) end,
294    fetch      = fetch,
295    libfile    = libfile,
296    libpath    = dirname(libfile),
297}
298
299-- inspect(curl.fetch("http://www.pragma-ade.com/index.html"))
300-- inspect(curl.fetch { url = "http://www.pragma-ade.com/index.html" })
301--
302-- inspect(curl.fetch {
303--     url           = "https://www.ctan.org/json/2.0/packages",
304--     sslverifyhost = 0,
305--     sslverifypeer = 0,
306--  -- capath        = true
307-- } )
308
309package.loaded[libname] = curl
310
311return curl
312