From 4120b470f50beafae7f6aee589b597fac6d5ab74 Mon Sep 17 00:00:00 2001 From: Alexey Melnichuk Date: Fri, 21 Jul 2017 11:21:11 +0300 Subject: [PATCH] Add. Examples to new callback functions. [ci skip] --- examples/lcurl/curl_debug.lua | 42 +++++++++++++++++++++++++++++++ examples/lcurl/fnmatch.lua | 47 +++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 examples/lcurl/curl_debug.lua create mode 100644 examples/lcurl/fnmatch.lua diff --git a/examples/lcurl/curl_debug.lua b/examples/lcurl/curl_debug.lua new file mode 100644 index 0000000..a5493fa --- /dev/null +++ b/examples/lcurl/curl_debug.lua @@ -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() diff --git a/examples/lcurl/fnmatch.lua b/examples/lcurl/fnmatch.lua new file mode 100644 index 0000000..3a7621a --- /dev/null +++ b/examples/lcurl/fnmatch.lua @@ -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()