diff --git a/examples/Lua-cURL/post_stream.lua b/examples/Lua-cURL/post_stream.lua index a10a500..1528392 100644 --- a/examples/Lua-cURL/post_stream.lua +++ b/examples/Lua-cURL/post_stream.lua @@ -1,47 +1,49 @@ local cURL = require("lcurl.cURL") +-- returns size and reader +local function make_stream(ch, n, m) + local size = n * m + local i = -1 + return size, function() + i = i + 1 + if i < m then + return (tostring(ch)):rep(n - 2) .. '\r\n' + end + end +end + c = cURL.easy_init() c:setopt_url("http://posttestserver.com/post.php") c:setopt_post(true) +local length, reader = make_stream("a", 10, 4) + c:post{-- post file from private read function -- Lua-cURL compatiable -- allows only one stream name = { file="stream.txt", - stream_length="5", + stream_length=length, type="text/plain", } } -count = 0 -c:perform{readfunction = function(n) - if count < 5 then - count = 5 - return "stream" - end - return nil -end} +c:perform{readfunction = reader} + +local length, reader = make_stream("b", 10, 4) c:post{-- post file from private read function -- define stream callback name = { file = "stream.txt", type = "text/plain", - stream_length = 5, - stream = function() - if count < 5 then - count = 5 - return "STREAM" - end - return nil - end, + stream_length = length, + stream = reader, } } -count = 0 c:perform{} print("Done") diff --git a/src/lua/cURL.lua b/src/lua/cURL.lua index 355d28f..05e6f2a 100644 --- a/src/lua/cURL.lua +++ b/src/lua/cURL.lua @@ -83,6 +83,7 @@ function Easy:setopt_readfunction(fn, ...) end function Easy:perform(opt) + opt = opt or {} local oerror = opt.errorfunction or function(err) return nil, err end