Add. Examples to new callback functions. [ci skip]

master
Alexey Melnichuk 2017-07-21 11:21:11 +03:00
parent d0cde48bd0
commit 4120b470f5
2 changed files with 89 additions and 0 deletions

View File

@ -0,0 +1,42 @@
--
-- convert `debug.c` example from libcurl examples
--
local curl = require "lcurl"
local function printf(...)
io.stderr:write(string.format(...))
end
local function dumb(title, data, n)
n = n or 16
printf("%s, %10.10d bytes (0x%8.8x)\n", title, #data, #data)
for i = 1, #data do
if (i - 1) % n == 0 then printf("%4.4x: ", i-1) end
printf("%02x ", string.byte(data, i, i))
if i % n == 0 then printf("\n") end
end
if #data % n ~= 0 then printf("\n") end
end
local function my_trace(type, data)
local text
if type == curl.INFO_TEXT then printf("== Info: %s", data) end
if type == curl.INFO_HEADER_OUT then text = "=> Send header" end
if type == curl.INFO_DATA_OUT then text = "=> Send data" end
if type == curl.INFO_SSL_DATA_OUT then text = "=> Send SSL data" end
if type == curl.INFO_HEADER_IN then text = "<= Recv header" end
if type == curl.INFO_DATA_IN then text = "<= Recv data" end
if type == curl.INFO_SSL_DATA_IN then text = "<= Recv SSL data" end
if text then dumb(text, data) end
end
local easy = curl.easy{
url = "http://google.com",
verbose = true,
debugfunction = my_trace,
followlocation = true,
writefunction = function()end,
}
easy:perform()

View File

@ -0,0 +1,47 @@
--
-- Example of how to download multiple files in single perform
--
local curl = require "lcurl"
local function printf(...)
io.stderr:write(string.format(...))
end
local function pat2pat(s)
return "^" .. string.gsub(s, ".", {
["*"] = '[^%.]*';
["."] = '%.';
}) .. "$"
end
local c = curl.easy{
url = "ftp://moteus:123456@127.0.0.1/test.*";
wildcardmatch = true;
}
local data, n = 0, 0
c:setopt_writefunction(function(chunk) data = #chunk + 1 end)
-- called before each new file
c:setopt_chunk_bgn_function(function(info, remains)
data, n = 0, n + 1
printf('\n======================================================\n')
printf('new file `%s` #%d, remains - %d\n', info.filename, n, remains or -1)
end)
-- called after file download complite
c:setopt_chunk_end_function(function()
printf('total size %d\n', data)
printf('------------------------------------------------------\n')
end)
-- custom pattern matching function
c:setopt_fnmatch_function(function(pattern, name)
local p = pat2pat(pattern)
local r = not not string.match(name, p)
printf("%s %s %s\n", r and '+' or '-', pattern, name)
return r
end)
c:perform()