Merge pull request #22 from moteus/master
Fix. Remove easy handle from multi handle
This commit is contained in:
commit
b4deff354a
16
examples/cURLv3/file.lua
Normal file
16
examples/cURLv3/file.lua
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
local cURL = require "cURL"
|
||||||
|
|
||||||
|
-- open output file
|
||||||
|
f = io.open("example_homepage", "w")
|
||||||
|
|
||||||
|
cURL.easy{
|
||||||
|
url = "http://www.example.com/",
|
||||||
|
writefunction = f
|
||||||
|
}
|
||||||
|
:perform()
|
||||||
|
:close()
|
||||||
|
|
||||||
|
-- close output file
|
||||||
|
f:close()
|
||||||
|
|
||||||
|
print("Done")
|
17
examples/cURLv3/multi.lua
Normal file
17
examples/cURLv3/multi.lua
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
local cURL = require("cURL")
|
||||||
|
|
||||||
|
-- setup easy and url
|
||||||
|
c1 = cURL.easy{url = "http://www.lua.org/"}
|
||||||
|
c2 = cURL.easy{url = "http://luajit.org/"}
|
||||||
|
|
||||||
|
m = cURL.multi()
|
||||||
|
:add_handle(c1)
|
||||||
|
:add_handle(c2)
|
||||||
|
|
||||||
|
local f1 = io.open("lua.html", "w+b")
|
||||||
|
local f2 = io.open("luajit.html", "w+b")
|
||||||
|
|
||||||
|
for data, type, easy in m:iperform() do
|
||||||
|
if type == "data" and c1 == easy then f1:write(data) end
|
||||||
|
if type == "data" and c2 == easy then f2:write(data) end
|
||||||
|
end
|
47
examples/cURLv3/rss.lua
Normal file
47
examples/cURLv3/rss.lua
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
-- use LuaExpat and Lua-CuRL together for On-The-Fly XML parsing
|
||||||
|
local lxp = require "lxp"
|
||||||
|
local cURL = require "cURL"
|
||||||
|
|
||||||
|
-- create XML parser
|
||||||
|
items, tags = {}, {}
|
||||||
|
p = lxp.new{
|
||||||
|
StartElement = function (parser, tagname)
|
||||||
|
tags[#tags + 1] = tagname
|
||||||
|
if (tagname == "item") then
|
||||||
|
items[#items + 1] = {}
|
||||||
|
end
|
||||||
|
end;
|
||||||
|
|
||||||
|
CharacterData = function (parser, str)
|
||||||
|
if (tags[#tags -1] == "item") then
|
||||||
|
--we are parsing a item, get rid of trailing whitespace
|
||||||
|
items[#items][tags[#tags]] = string.gsub(str, "%s*$", "")
|
||||||
|
end
|
||||||
|
end;
|
||||||
|
|
||||||
|
EndElement = function (parser, tagname)
|
||||||
|
--assuming well formed xml
|
||||||
|
tags[#tags] = nil
|
||||||
|
end;
|
||||||
|
}
|
||||||
|
|
||||||
|
-- create and setup easy handle
|
||||||
|
c = cURL.easy{url = "http://www.lua.org/news.rss"}
|
||||||
|
|
||||||
|
-- setup writer function with context
|
||||||
|
c:setopt_writefunction(p.parse, p)
|
||||||
|
|
||||||
|
-- perform request and close easy handle
|
||||||
|
-- perform raise error if parser fail
|
||||||
|
c:perform():close()
|
||||||
|
|
||||||
|
--finish document
|
||||||
|
assert(p:parse())
|
||||||
|
p:close()
|
||||||
|
|
||||||
|
for i, item in ipairs(items) do
|
||||||
|
for k, v in pairs(item) do
|
||||||
|
print(k,v)
|
||||||
|
end
|
||||||
|
print()
|
||||||
|
end
|
@ -58,10 +58,12 @@ local function make_iterator(self, perform)
|
|||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local remain = #self._easy
|
local remain = self._easy.n
|
||||||
for _, e in ipairs(self._easy) do
|
for h, e in pairs(self._easy) do
|
||||||
e:setopt_writefunction (function(str) buffers:append(e, "data", str) end)
|
if h ~= 'n' then
|
||||||
e:setopt_headerfunction(function(str) buffers:append(e, "header", str) end)
|
e:setopt_writefunction (function(str) buffers:append(e, "data", str) end)
|
||||||
|
e:setopt_headerfunction(function(str) buffers:append(e, "header", str) end)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
assert(perform(self))
|
assert(perform(self))
|
||||||
@ -78,11 +80,9 @@ local function make_iterator(self, perform)
|
|||||||
|
|
||||||
if n <= remain then
|
if n <= remain then
|
||||||
while true do
|
while true do
|
||||||
local e, ok, err = assert(self:info_read())
|
local h, ok, err = assert(self:info_read())
|
||||||
if e == 0 then break end
|
if h == 0 then break end
|
||||||
for _, a in ipairs(self._easy) do
|
local e = assert(self._easy[h])
|
||||||
if e == a:handle() then e = a break end
|
|
||||||
end
|
|
||||||
if ok then
|
if ok then
|
||||||
ok = e:getinfo_response_code() or ok
|
ok = e:getinfo_response_code() or ok
|
||||||
buffers:append(e, "done", ok)
|
buffers:append(e, "done", ok)
|
||||||
@ -90,7 +90,6 @@ local function make_iterator(self, perform)
|
|||||||
end
|
end
|
||||||
remain = n
|
remain = n
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -276,7 +275,7 @@ local add_handle = wrap_function("add_handle")
|
|||||||
local remove_handle = wrap_function("remove_handle")
|
local remove_handle = wrap_function("remove_handle")
|
||||||
|
|
||||||
function Multi:__init()
|
function Multi:__init()
|
||||||
self._easy = {}
|
self._easy = {n = 0}
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -285,14 +284,26 @@ function Multi:perform()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Multi:add_handle(e)
|
function Multi:add_handle(e)
|
||||||
self._easy = self._easy or {}
|
assert(self._easy.n >= 0)
|
||||||
self._easy[#self._easy + 1] = e
|
|
||||||
return add_handle(self, e:handle())
|
local h = e:handle()
|
||||||
|
if self._easy[h] then return self end
|
||||||
|
|
||||||
|
local ok, err = add_handle(self, h)
|
||||||
|
if not ok then return nil, err end
|
||||||
|
self._easy[h], self._easy.n = e, self._easy.n + 1
|
||||||
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
function Multi:remove_handle(e)
|
function Multi:remove_handle(e)
|
||||||
self._easy[#self._easy + 1] = e
|
local h = e:handle()
|
||||||
return remove_handle(self, e:handle())
|
|
||||||
|
if self._easy[h] then
|
||||||
|
self._easy[h], self._easy.n = nil, self._easy.n - 1
|
||||||
|
end
|
||||||
|
assert(self._easy.n >= 0)
|
||||||
|
|
||||||
|
return remove_handle(self, h)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
@ -347,8 +358,11 @@ end
|
|||||||
-------------------------------------------
|
-------------------------------------------
|
||||||
local Multi = class(curl.multi) do
|
local Multi = class(curl.multi) do
|
||||||
|
|
||||||
|
local add_handle = wrap_function("add_handle")
|
||||||
|
local remove_handle = wrap_function("remove_handle")
|
||||||
|
|
||||||
function Multi:__init()
|
function Multi:__init()
|
||||||
self._easy = {}
|
self._easy = {n = 0}
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -356,18 +370,31 @@ function Multi:iperform()
|
|||||||
return make_iterator(self, self.perform)
|
return make_iterator(self, self.perform)
|
||||||
end
|
end
|
||||||
|
|
||||||
local add_handle = wrap_function("add_handle")
|
|
||||||
function Multi:add_handle(e)
|
function Multi:add_handle(e)
|
||||||
self._easy[#self._easy + 1] = e
|
assert(self._easy.n >= 0)
|
||||||
return add_handle(self, e:handle())
|
|
||||||
|
local h = e:handle()
|
||||||
|
if self._easy[h] then return self end
|
||||||
|
|
||||||
|
local ok, err = add_handle(self, h)
|
||||||
|
if not ok then return nil, err end
|
||||||
|
self._easy[h], self._easy.n = e, self._easy.n + 1
|
||||||
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
local remove_handle = wrap_function("remove_handle")
|
|
||||||
function Multi:remove_handle(e)
|
function Multi:remove_handle(e)
|
||||||
self._easy[#self._easy + 1] = e
|
local h = e:handle()
|
||||||
return remove_handle(self, e:handle())
|
|
||||||
|
if self._easy[h] then
|
||||||
|
self._easy[h], self._easy.n = nil, self._easy.n - 1
|
||||||
|
end
|
||||||
|
assert(self._easy.n >= 0)
|
||||||
|
|
||||||
|
return remove_handle(self, h)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--! @fixme Multi:info_read(true) should also remove easy handle from self._easy
|
||||||
|
|
||||||
end
|
end
|
||||||
-------------------------------------------
|
-------------------------------------------
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user