diff --git a/doc/lcurl.ldoc b/doc/lcurl.ldoc index c05c2e3..d5566d6 100644 --- a/doc/lcurl.ldoc +++ b/doc/lcurl.ldoc @@ -303,13 +303,22 @@ function setopt_headerfunction() end -- A callback accepting one or two parameters. -- The first is the reader context if any, and the second is the maximum amount of data to be read. -- You can ignore second argument and pass as mach data as you need. lcurl can split data. --- Function must return data to continue operation. To stop operation it must return empty string on nil. +-- Function must return data to continue operation. To stop operation it must return empty string or nil or nothing. -- Otherwise the transfer will be aborted with an error. +-- -- -- @tparam function reader -- @param[opt] context reader context -- @return[1] self -- +-- @usage +-- local counter = 10 +-- c:setopt_readfunction(function() +-- if counter > 0 then +-- counter = counter - 1 +-- return 'a' +-- end +-- end) function setopt_readfunction() end --- Set reader function. diff --git a/src/lceasy.c b/src/lceasy.c index 25f5763..48a35f4 100644 --- a/src/lceasy.c +++ b/src/lceasy.c @@ -714,7 +714,7 @@ static size_t lcurl_read_callback(lua_State *L, } if(lua_gettop(L) == top){ - return CURL_READFUNC_ABORT; + return 0; } assert(lua_gettop(L) >= top); diff --git a/src/lua/cURL/impl/cURL.lua b/src/lua/cURL/impl/cURL.lua index f444eb5..f7f7b90 100644 --- a/src/lua/cURL/impl/cURL.lua +++ b/src/lua/cURL/impl/cURL.lua @@ -93,6 +93,7 @@ local function make_iterator(self, perform) ok = e:getinfo_response_code() or ok buffers:append(e, "done", ok) else buffers:append(e, "error", err) end + self:remove_handle(e) end remain = n end diff --git a/test/test_easy.lua b/test/test_easy.lua index 27dd6ef..348f605 100644 --- a/test/test_easy.lua +++ b/test/test_easy.lua @@ -514,10 +514,10 @@ function teardown() end function test_abort_01() - assert_equal(c, c:setopt_readfunction(function() end)) - - local _, e = assert_nil(c:perform()) - assert_equal(curl.error(curl.ERROR_EASY, curl.E_ABORTED_BY_CALLBACK), e) +-- assert_equal(c, c:setopt_readfunction(function() end)) +-- +-- local _, e = assert_nil(c:perform()) +-- assert_equal(curl.error(curl.ERROR_EASY, curl.E_ABORTED_BY_CALLBACK), e) end function test_abort_02() @@ -611,6 +611,7 @@ function test_readbuffer() end function test_pass_01() + -- We need this to support file:read() method which returns nil as EOF assert_equal(c, c:setopt_readfunction(function() return nil end)) assert_equal(c, c:perform()) @@ -619,6 +620,21 @@ function test_pass_01() assert_equal(0, #data) end +function test_pass_02() + local counter = 10 + assert_equal(c, c:setopt_readfunction(function() + if counter > 0 then + counter = counter - 1 + return 'a' + end + end)) + + assert_equal(c, c:perform()) + c:close() + local data = read_file(fname) + assert_equal(('a'):rep(10), data) +end + end local _ENV = TEST_CASE'escape' if ENABLE then