Skip to content

Commit 0568097

Browse files
committed
Merge pull request #32 from moteus/master
Change. Returns nothing form reader callback means EOF.
2 parents 4817dad + 7efaba4 commit 0568097

File tree

6 files changed

+35
-8
lines changed

6 files changed

+35
-8
lines changed

doc/curl.ldoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ do
131131
-- <br/>Returned data types: `response`, `header`, `data`, `error`, `done`.
132132
-- <br/>Note. response data may appeare several times (e.g. if you proceed http request
133133
-- with digest auth you should get 401 and 200 responses).
134+
-- <br/> Easy handle is removed at the end of the operation (when get `done` or `error` row).
134135
--
135136
-- @treturn Iterator iterator for generic for
136137
--

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.

examples/cURLv3/multi.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ local f1 = io.open("lua.html", "w+b")
1212
local f2 = io.open("luajit.html", "w+b")
1313

1414
for data, type, easy in m:iperform() do
15-
if type == "data" and c1 == easy then f1:write(data) end
16-
if type == "data" and c2 == easy then f2:write(data) end
15+
if type == "data" and c1 == easy then f1:write(data) end
16+
if type == "data" and c2 == easy then f2:write(data) end
1717
end

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)