|
| 1 | +--- Returns path to cURL ca bundle |
| 2 | +-- |
| 3 | +-- @tparam[opt="curl-ca-bundle.crt"] string name name of bundle |
| 4 | +-- @treturn string path to file (CURLOPT_CAINFO) |
| 5 | +-- @treturn string path to ssl dir path (CURLOPT_CAPATH) |
| 6 | +-- |
| 7 | +-- @usage |
| 8 | +-- local file, path = find_ca_bundle() |
| 9 | +-- if file then e:setopt_cainfo(file) end |
| 10 | +-- if path then e:setopt_capath(path) end |
| 11 | +-- |
| 12 | +local function find_ca_bundle(name) |
| 13 | + name = name or "curl-ca-bundle.crt" |
| 14 | + |
| 15 | + local path = require "path" |
| 16 | + local env = setmetatable({},{__index = function(_, name) return os.getenv(name) end}) |
| 17 | + |
| 18 | + local function split(str, sep, plain) |
| 19 | + local b, res = 1, {} |
| 20 | + while b <= #str do |
| 21 | + local e, e2 = string.find(str, sep, b, plain) |
| 22 | + if e then |
| 23 | + table.insert(res, (string.sub(str, b, e-1))) |
| 24 | + b = e2 + 1 |
| 25 | + else |
| 26 | + table.insert(res, (string.sub(str, b))) |
| 27 | + break |
| 28 | + end |
| 29 | + end |
| 30 | + return res |
| 31 | + end |
| 32 | + |
| 33 | + if env.CURL_CA_BUNDLE and path.isfile(env.CURL_CA_BUNDLE) then |
| 34 | + return env.CURL_CA_BUNDLE |
| 35 | + end |
| 36 | + |
| 37 | + if env.SSL_CERT_DIR and path.isdir(env.SSL_CERT_DIR) then |
| 38 | + return false, env.SSL_CERT_DIR |
| 39 | + end |
| 40 | + |
| 41 | + if env.SSL_CERT_FILE and path.isfile(env.SSL_CERT_FILE) then |
| 42 | + return env.SSL_CERT_FILE |
| 43 | + end |
| 44 | + |
| 45 | + if not path.IS_WINDOWS then return end |
| 46 | + |
| 47 | + local paths = { |
| 48 | + '.', |
| 49 | + path.join(env.windir, "System32"), |
| 50 | + path.join(env.windir, "SysWOW64"), |
| 51 | + env.windir, |
| 52 | + } |
| 53 | + for _, p in ipairs(split(env.path, ';')) do paths[#paths + 1] = p end |
| 54 | + |
| 55 | + for _, p in ipairs(paths) do |
| 56 | + p = path.fullpath(p) |
| 57 | + if path.isdir(p) then |
| 58 | + p = path.join(p, name) |
| 59 | + if path.isfile(p) then |
| 60 | + return p |
| 61 | + end |
| 62 | + end |
| 63 | + end |
| 64 | +end |
| 65 | + |
| 66 | +return { |
| 67 | + find_ca_bundle = find_ca_bundle; |
| 68 | +} |
| 69 | + |
0 commit comments