Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions examples/lcurl/curl_debug.lua
Original file line number Diff line number Diff line change
@@ -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()
47 changes: 47 additions & 0 deletions examples/lcurl/fnmatch.lua
Original file line number Diff line number Diff line change
@@ -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()