Change. Returns nothing form reader callback means EOF.

Change. multi:iperform() remove easy handle when it done.
This commit is contained in:
Alexey Melnichuk 2014-09-15 13:17:13 +05:00
parent e6c2ffce5c
commit aff3042434
4 changed files with 32 additions and 6 deletions

View File

@ -303,13 +303,22 @@ function setopt_headerfunction() end
-- A callback accepting one or two parameters. -- 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. -- 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. -- 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. -- Otherwise the transfer will be aborted with an error.
-- --
--
-- @tparam function reader -- @tparam function reader
-- @param[opt] context reader context -- @param[opt] context reader context
-- @return[1] self -- @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 function setopt_readfunction() end
--- Set reader function. --- Set reader function.

View File

@ -714,7 +714,7 @@ static size_t lcurl_read_callback(lua_State *L,
} }
if(lua_gettop(L) == top){ if(lua_gettop(L) == top){
return CURL_READFUNC_ABORT; return 0;
} }
assert(lua_gettop(L) >= top); assert(lua_gettop(L) >= top);

View File

@ -93,6 +93,7 @@ local function make_iterator(self, perform)
ok = e:getinfo_response_code() or ok ok = e:getinfo_response_code() or ok
buffers:append(e, "done", ok) buffers:append(e, "done", ok)
else buffers:append(e, "error", err) end else buffers:append(e, "error", err) end
self:remove_handle(e)
end end
remain = n remain = n
end end

View File

@ -514,10 +514,10 @@ function teardown()
end end
function test_abort_01() function test_abort_01()
assert_equal(c, c:setopt_readfunction(function() end)) -- assert_equal(c, c:setopt_readfunction(function() end))
--
local _, e = assert_nil(c:perform()) -- local _, e = assert_nil(c:perform())
assert_equal(curl.error(curl.ERROR_EASY, curl.E_ABORTED_BY_CALLBACK), e) -- assert_equal(curl.error(curl.ERROR_EASY, curl.E_ABORTED_BY_CALLBACK), e)
end end
function test_abort_02() function test_abort_02()
@ -611,6 +611,7 @@ function test_readbuffer()
end end
function test_pass_01() 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:setopt_readfunction(function() return nil end))
assert_equal(c, c:perform()) assert_equal(c, c:perform())
@ -619,6 +620,21 @@ function test_pass_01()
assert_equal(0, #data) assert_equal(0, #data)
end 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 end
local _ENV = TEST_CASE'escape' if ENABLE then local _ENV = TEST_CASE'escape' if ENABLE then