Add. some examples
This commit is contained in:
parent
5305ba55ac
commit
349129bf95
78
examples/multi_iterator.lua
Normal file
78
examples/multi_iterator.lua
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
--
|
||||||
|
local function multi_iterator(...)
|
||||||
|
local curl = require "lcurl.safe"
|
||||||
|
|
||||||
|
local buffers = {_ = {}} do
|
||||||
|
|
||||||
|
function buffers:append(e, ...)
|
||||||
|
local b = self._[e] or {}
|
||||||
|
self._[e] = b
|
||||||
|
b[#b + 1] = {...}
|
||||||
|
end
|
||||||
|
|
||||||
|
function buffers:next()
|
||||||
|
for e, t in pairs(self._) do
|
||||||
|
local m = table.remove(t, 1)
|
||||||
|
if m then return e, m end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
local function multi_init(...)
|
||||||
|
local remain = 0
|
||||||
|
local m = curl.multi()
|
||||||
|
for _, e in ipairs{...} do
|
||||||
|
e:setopt_writefunction(function(str) buffers:append(e, "data", str) end)
|
||||||
|
e:setopt_headerfunction(function(str) buffers:append(e, "header", str) end)
|
||||||
|
m:add_handle(e)
|
||||||
|
remain = remain + 1
|
||||||
|
end
|
||||||
|
return m, remain
|
||||||
|
end
|
||||||
|
|
||||||
|
local m, remain = multi_init(...)
|
||||||
|
m:perform()
|
||||||
|
return function()
|
||||||
|
while true do
|
||||||
|
local e, t = buffers:next()
|
||||||
|
if t then return e, unpack(t) end
|
||||||
|
if remain == 0 then break end
|
||||||
|
|
||||||
|
m:wait()
|
||||||
|
|
||||||
|
local n, err = m:perform()
|
||||||
|
if not n then m:close() error(err) end
|
||||||
|
|
||||||
|
if n <= remain then
|
||||||
|
while true do
|
||||||
|
local e, ok, err = m:info_read()
|
||||||
|
if not e then m:close() error(err) end
|
||||||
|
if e == 0 then break end
|
||||||
|
if ok then buffers:append(e, "done", ok)
|
||||||
|
else buffers:append(e, "error", err) end
|
||||||
|
end
|
||||||
|
remain = n
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
m:close()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--
|
||||||
|
local curl = require "lcurl"
|
||||||
|
|
||||||
|
c1 = curl.easy()
|
||||||
|
:setopt_url("http://www.lua.org/")
|
||||||
|
|
||||||
|
c2 = curl.easy()
|
||||||
|
:setopt_url("http://luajit.org/")
|
||||||
|
|
||||||
|
for easy, type, data in multi_iterator(c1, c2) do
|
||||||
|
if type == 'header' then print(easy, type, (data:gsub("%s*$", "")))
|
||||||
|
elseif type == 'data' then print(easy, type, #data)
|
||||||
|
elseif type == 'error' then print(easy, type, data)
|
||||||
|
elseif type == 'done' then print(easy, type, data)
|
||||||
|
end
|
||||||
|
end
|
@ -12,4 +12,58 @@ cURL.easy()
|
|||||||
:perform()
|
:perform()
|
||||||
:close()
|
:close()
|
||||||
|
|
||||||
|
-- Lua-cURL compatiable function
|
||||||
|
local function post(e, data)
|
||||||
|
local form = cURL.form()
|
||||||
|
local ok, err = true
|
||||||
|
|
||||||
|
for k, v in pairs(data) do
|
||||||
|
if type(v) == "string" then
|
||||||
|
ok, err = form:add_content(k, v)
|
||||||
|
else
|
||||||
|
assert(type(v) == "table")
|
||||||
|
if v.stream_length then
|
||||||
|
form:free()
|
||||||
|
error("Stream does not support")
|
||||||
|
end
|
||||||
|
if v.data then
|
||||||
|
ok, err = form:add_buffer(k, v.file, v.data, v.type, v.headers)
|
||||||
|
else
|
||||||
|
ok, err = form:add_file(k, v.file, v.data, v.type, v.filename, v.headers)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if not ok then break end
|
||||||
|
end
|
||||||
|
|
||||||
|
if not ok then
|
||||||
|
form:free()
|
||||||
|
return nil, err
|
||||||
|
end
|
||||||
|
|
||||||
|
ok, err = e:setopt_httppost(form)
|
||||||
|
if not ok then
|
||||||
|
form:free()
|
||||||
|
return nil, err
|
||||||
|
end
|
||||||
|
|
||||||
|
return e
|
||||||
|
end
|
||||||
|
|
||||||
|
local e = cURL.easy()
|
||||||
|
:setopt_url("http://localhost")
|
||||||
|
|
||||||
|
postdata = {
|
||||||
|
-- post file from filesystem
|
||||||
|
name = {file="post.lua",
|
||||||
|
type="text/plain"
|
||||||
|
},
|
||||||
|
-- post file from data variable
|
||||||
|
name2 = {file="dummy.html",
|
||||||
|
data="<html><bold>bold</bold></html>",
|
||||||
|
type="text/html"
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
post(e, postdata):perform()
|
||||||
|
|
||||||
print("Done")
|
print("Done")
|
Loading…
x
Reference in New Issue
Block a user