Skip to content

Commit b2e6bca

Browse files
committed
Fix. add_handle/remove_handle on cURLv3
Add. Examples for cURLv3
1 parent 56a47e1 commit b2e6bca

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

examples/cURLv3/file.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
local cURL = require "cURL"
2+
3+
-- open output file
4+
f = io.open("example_homepage", "w")
5+
6+
cURL.easy{
7+
url = "http://www.example.com/",
8+
writefunction = f
9+
}
10+
:perform()
11+
:close()
12+
13+
-- close output file
14+
f:close()
15+
16+
print("Done")

examples/cURLv3/multi.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
local cURL = require("cURL")
2+
3+
-- setup easy and url
4+
c1 = cURL.easy{url = "http://www.lua.org/"}
5+
c2 = cURL.easy{url = "http://luajit.org/"}
6+
7+
m = cURL.multi()
8+
:add_handle(c1)
9+
:add_handle(c2)
10+
11+
local f1 = io.open("lua.html", "w+b")
12+
local f2 = io.open("luajit.html", "w+b")
13+
14+
for data, type, easy in m:iperform() do
15+
if type == "data" and c1 == easy then f1:write(data) end
16+
if type == "data" and c2 == easy then f2:write(data) end
17+
end

examples/cURLv3/rss.lua

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
-- use LuaExpat and Lua-CuRL together for On-The-Fly XML parsing
2+
local lxp = require "lxp"
3+
local cURL = require "cURL"
4+
5+
-- create XML parser
6+
items, tags = {}, {}
7+
p = lxp.new{
8+
StartElement = function (parser, tagname)
9+
tags[#tags + 1] = tagname
10+
if (tagname == "item") then
11+
items[#items + 1] = {}
12+
end
13+
end;
14+
15+
CharacterData = function (parser, str)
16+
if (tags[#tags -1] == "item") then
17+
--we are parsing a item, get rid of trailing whitespace
18+
items[#items][tags[#tags]] = string.gsub(str, "%s*$", "")
19+
end
20+
end;
21+
22+
EndElement = function (parser, tagname)
23+
--assuming well formed xml
24+
tags[#tags] = nil
25+
end;
26+
}
27+
28+
-- create and setup easy handle
29+
c = cURL.easy{url = "http://www.lua.org/news.rss"}
30+
31+
-- setup writer function with context
32+
c:setopt_writefunction(p.parse, p)
33+
34+
-- perform request and close easy handle
35+
-- perform raise error if parser fail
36+
c:perform():close()
37+
38+
--finish document
39+
assert(p:parse())
40+
p:close()
41+
42+
for i, item in ipairs(items) do
43+
for k, v in pairs(item) do
44+
print(k,v)
45+
end
46+
print()
47+
end

src/lua/cURL/impl/cURL.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,9 @@ end
358358
-------------------------------------------
359359
local Multi = class(curl.multi) do
360360

361+
local add_handle = wrap_function("add_handle")
362+
local remove_handle = wrap_function("remove_handle")
363+
361364
function Multi:__init()
362365
self._easy = {n = 0}
363366
return self

0 commit comments

Comments
 (0)