Skip to content

Commit b38117b

Browse files
committed
Merge pull request #23 from moteus/master
Fix. multi:info_read; Add. find_ca_bundle
2 parents b4deff3 + fe66e72 commit b38117b

5 files changed

Lines changed: 116 additions & 1 deletion

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

examples/cURLv3/multi2.lua

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
local cURL = require("cURL")
2+
3+
local f1 = io.open("lua.html", "w+b")
4+
local f2 = io.open("luajit.html", "w+b")
5+
6+
-- setup easy and url
7+
c1 = cURL.easy{url = "http://www.lua.org/", writefunction = f1}
8+
c2 = cURL.easy{url = "http://luajit.org/", writefunction = f2}
9+
c3 = cURL.easy{url = "****://luajit.org/"} -- UNSUPPORTED_PROTOCOL
10+
11+
m = cURL.multi()
12+
:add_handle(c1)
13+
:add_handle(c2)
14+
:add_handle(c3)
15+
16+
local remain = 3
17+
while remain > 0 do
18+
local last = m:perform() -- do some work
19+
if last < remain then -- we have done some tasks
20+
while true do -- proceed results/errors
21+
local e, ok, err = m:info_read(true) -- get result and remove handle
22+
if e == 0 then break end -- no more finished tasks
23+
if ok then -- succeed
24+
print(e:getinfo_effective_url(), '-', e:getinfo_response_code())
25+
else -- failure
26+
print(e:getinfo_effective_url(), '-', err)
27+
end
28+
e:close()
29+
end
30+
end
31+
remain = last
32+
33+
-- wait while libcurl do io select
34+
m:wait()
35+
end

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/impl/cURL.lua

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,16 @@ function Multi:remove_handle(e)
393393
return remove_handle(self, h)
394394
end
395395

396-
--! @fixme Multi:info_read(true) should also remove easy handle from self._easy
396+
function Multi:info_read(...)
397+
local h, ok, err = self:handle():info_read(...)
398+
if not h then return nil, ok end
399+
if h == 0 then return h end
400+
401+
if ... and self._easy[h] then
402+
self._easy[h], self._easy.n = nil, self._easy.n - 1
403+
end
404+
return h, ok, err
405+
end
397406

398407
end
399408
-------------------------------------------

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)