Fix. add_handle/remove_handle on cURLv3

Add. Examples for cURLv3
master
Alexey Melnichuk 2014-09-11 12:50:48 +05:00
parent 56a47e12d0
commit b2e6bcae4e
4 changed files with 83 additions and 0 deletions

16
examples/cURLv3/file.lua Normal file
View 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
View 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
View 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

View File

@ -358,6 +358,9 @@ end
-------------------------------------------
local Multi = class(curl.multi) do
local add_handle = wrap_function("add_handle")
local remove_handle = wrap_function("remove_handle")
function Multi:__init()
self._easy = {n = 0}
return self