File tree Expand file tree Collapse file tree 4 files changed +83
-0
lines changed Expand file tree Collapse file tree 4 files changed +83
-0
lines changed Original file line number Diff line number Diff line change 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" )
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 358358---- ---------------------------------------
359359local Multi = class (curl .multi ) do
360360
361+ local add_handle = wrap_function (" add_handle" )
362+ local remove_handle = wrap_function (" remove_handle" )
363+
361364function Multi :__init ()
362365 self ._easy = {n = 0 }
363366 return self
You can’t perform that action at this time.
0 commit comments