Skip to content

Commit 82edc79

Browse files
authored
Merge pull request #109 from moteus/master
Add. Examples to new callback functions. [ci skip]
2 parents d0cde48 + 4120b47 commit 82edc79

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

examples/lcurl/curl_debug.lua

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--
2+
-- convert `debug.c` example from libcurl examples
3+
--
4+
5+
local curl = require "lcurl"
6+
7+
local function printf(...)
8+
io.stderr:write(string.format(...))
9+
end
10+
11+
local function dumb(title, data, n)
12+
n = n or 16
13+
printf("%s, %10.10d bytes (0x%8.8x)\n", title, #data, #data)
14+
for i = 1, #data do
15+
if (i - 1) % n == 0 then printf("%4.4x: ", i-1) end
16+
printf("%02x ", string.byte(data, i, i))
17+
if i % n == 0 then printf("\n") end
18+
end
19+
if #data % n ~= 0 then printf("\n") end
20+
end
21+
22+
local function my_trace(type, data)
23+
local text
24+
if type == curl.INFO_TEXT then printf("== Info: %s", data) end
25+
if type == curl.INFO_HEADER_OUT then text = "=> Send header" end
26+
if type == curl.INFO_DATA_OUT then text = "=> Send data" end
27+
if type == curl.INFO_SSL_DATA_OUT then text = "=> Send SSL data" end
28+
if type == curl.INFO_HEADER_IN then text = "<= Recv header" end
29+
if type == curl.INFO_DATA_IN then text = "<= Recv data" end
30+
if type == curl.INFO_SSL_DATA_IN then text = "<= Recv SSL data" end
31+
if text then dumb(text, data) end
32+
end
33+
34+
local easy = curl.easy{
35+
url = "http://google.com",
36+
verbose = true,
37+
debugfunction = my_trace,
38+
followlocation = true,
39+
writefunction = function()end,
40+
}
41+
42+
easy:perform()

examples/lcurl/fnmatch.lua

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
--
2+
-- Example of how to download multiple files in single perform
3+
--
4+
5+
local curl = require "lcurl"
6+
7+
local function printf(...)
8+
io.stderr:write(string.format(...))
9+
end
10+
11+
local function pat2pat(s)
12+
return "^" .. string.gsub(s, ".", {
13+
["*"] = '[^%.]*';
14+
["."] = '%.';
15+
}) .. "$"
16+
end
17+
18+
local c = curl.easy{
19+
url = "ftp://moteus:123456@127.0.0.1/test.*";
20+
wildcardmatch = true;
21+
}
22+
23+
local data, n = 0, 0
24+
c:setopt_writefunction(function(chunk) data = #chunk + 1 end)
25+
26+
-- called before each new file
27+
c:setopt_chunk_bgn_function(function(info, remains)
28+
data, n = 0, n + 1
29+
printf('\n======================================================\n')
30+
printf('new file `%s` #%d, remains - %d\n', info.filename, n, remains or -1)
31+
end)
32+
33+
-- called after file download complite
34+
c:setopt_chunk_end_function(function()
35+
printf('total size %d\n', data)
36+
printf('------------------------------------------------------\n')
37+
end)
38+
39+
-- custom pattern matching function
40+
c:setopt_fnmatch_function(function(pattern, name)
41+
local p = pat2pat(pattern)
42+
local r = not not string.match(name, p)
43+
printf("%s %s %s\n", r and '+' or '-', pattern, name)
44+
return r
45+
end)
46+
47+
c:perform()

0 commit comments

Comments
 (0)