Skip to content

Commit fe66e72

Browse files
committed
Add. find_ca_bundle function to cURL.utils module to find curl-ca-bundle.crt file
1 parent 1b88ec4 commit fe66e72

3 files changed

Lines changed: 71 additions & 0 deletions

File tree

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ install:
2626

2727
script:
2828
- cd test
29+
- lua -e "print(require 'cURL.utils'.find_ca_bundle())"
2930
- lunit.sh test_easy.lua
3031
- lunit.sh test_safe.lua
3132
- lunit.sh test_form.lua

rockspecs/lua-curl-scm-0.rockspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ build = {
5959
modules = {
6060
["cURL" ] = "src/lua/cURL.lua",
6161
["cURL.safe" ] = "src/lua/cURL/safe.lua",
62+
["cURL.utils" ] = "src/lua/cURL/utils.lua",
6263
["cURL.impl.cURL" ] = "src/lua/cURL/impl/cURL.lua",
6364

6465
lcurl = {

src/lua/cURL/utils.lua

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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

Comments
 (0)