Skip to content

Commit aff3042

Browse files
committed
Change. Returns nothing form reader callback means EOF.
Change. multi:iperform() remove easy handle when it done.
1 parent e6c2ffc commit aff3042

4 files changed

Lines changed: 32 additions & 6 deletions

File tree

doc/lcurl.ldoc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,22 @@ function setopt_headerfunction() end
303303
-- A callback accepting one or two parameters.
304304
-- The first is the reader context if any, and the second is the maximum amount of data to be read.
305305
-- You can ignore second argument and pass as mach data as you need. lcurl can split data.
306-
-- Function must return data to continue operation. To stop operation it must return empty string on nil.
306+
-- Function must return data to continue operation. To stop operation it must return empty string or nil or nothing.
307307
-- Otherwise the transfer will be aborted with an error.
308+
--
308309
--
309310
-- @tparam function reader
310311
-- @param[opt] context reader context
311312
-- @return[1] self
312313
--
314+
-- @usage
315+
-- local counter = 10
316+
-- c:setopt_readfunction(function()
317+
-- if counter > 0 then
318+
-- counter = counter - 1
319+
-- return 'a'
320+
-- end
321+
-- end)
313322
function setopt_readfunction() end
314323

315324
--- Set reader function.

src/lceasy.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ static size_t lcurl_read_callback(lua_State *L,
714714
}
715715

716716
if(lua_gettop(L) == top){
717-
return CURL_READFUNC_ABORT;
717+
return 0;
718718
}
719719

720720
assert(lua_gettop(L) >= top);

src/lua/cURL/impl/cURL.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ local function make_iterator(self, perform)
9393
ok = e:getinfo_response_code() or ok
9494
buffers:append(e, "done", ok)
9595
else buffers:append(e, "error", err) end
96+
self:remove_handle(e)
9697
end
9798
remain = n
9899
end

test/test_easy.lua

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -514,10 +514,10 @@ function teardown()
514514
end
515515

516516
function test_abort_01()
517-
assert_equal(c, c:setopt_readfunction(function() end))
518-
519-
local _, e = assert_nil(c:perform())
520-
assert_equal(curl.error(curl.ERROR_EASY, curl.E_ABORTED_BY_CALLBACK), e)
517+
-- assert_equal(c, c:setopt_readfunction(function() end))
518+
--
519+
-- local _, e = assert_nil(c:perform())
520+
-- assert_equal(curl.error(curl.ERROR_EASY, curl.E_ABORTED_BY_CALLBACK), e)
521521
end
522522

523523
function test_abort_02()
@@ -611,6 +611,7 @@ function test_readbuffer()
611611
end
612612

613613
function test_pass_01()
614+
-- We need this to support file:read() method which returns nil as EOF
614615
assert_equal(c, c:setopt_readfunction(function() return nil end))
615616

616617
assert_equal(c, c:perform())
@@ -619,6 +620,21 @@ function test_pass_01()
619620
assert_equal(0, #data)
620621
end
621622

623+
function test_pass_02()
624+
local counter = 10
625+
assert_equal(c, c:setopt_readfunction(function()
626+
if counter > 0 then
627+
counter = counter - 1
628+
return 'a'
629+
end
630+
end))
631+
632+
assert_equal(c, c:perform())
633+
c:close()
634+
local data = read_file(fname)
635+
assert_equal(('a'):rep(10), data)
636+
end
637+
622638
end
623639

624640
local _ENV = TEST_CASE'escape' if ENABLE then

0 commit comments

Comments
 (0)