Skip to content

Commit f2ab8ee

Browse files
committed
Fix. Return nil from read callback means EOF.
1 parent c79e255 commit f2ab8ee

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

src/lceasy.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,10 @@ static size_t lcurl_read_callback(lua_State *L,
711711

712712
if(lua_type(L, top + 1) != LUA_TSTRING){
713713
if(lua_isnil(L, top + 1)){
714-
if(lua_gettop(L) == (top+1)) lua_settop(L, top);
714+
if(lua_gettop(L) == (top+1)){// only nil -> EOF
715+
lua_settop(L, top);
716+
return 0;
717+
}
715718
}
716719
else{
717720
if(lua_type(L, top + 1) == LUA_TNUMBER){

test/test_easy.lua

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ function setup()
382382
f = assert(scurl.form())
383383
c = assert(scurl.easy{
384384
url = url,
385+
timeout = 60,
385386
})
386387
assert_equal(c, c:setopt_writefunction(table.insert, t))
387388
end
@@ -441,6 +442,23 @@ function test_abort_05()
441442
assert_error_match("READERROR", function() c:perform() end)
442443
end
443444

445+
function test_abort_06()
446+
assert_equal(f, f:add_stream('SSSSS', 128, function() return false end))
447+
assert_equal(c, c:setopt_httppost(f))
448+
449+
local _, e = assert_nil(c:perform())
450+
assert_equal(curl.error(curl.ERROR_EASY, curl.E_ABORTED_BY_CALLBACK), e)
451+
end
452+
453+
function test_pass_01()
454+
assert_equal(c, c:setopt_timeout(10))
455+
assert_equal(f, f:add_stream('SSSSS', 128, function() return nil end))
456+
assert_equal(c, c:setopt_httppost(f))
457+
458+
local _, e = assert_nil(c:perform())
459+
assert_equal(curl.error(curl.ERROR_EASY, curl.E_OPERATION_TIMEDOUT), e)
460+
end
461+
444462
function test_pause()
445463
local counter = 0
446464
assert_equal(f, f:add_stream('SSSSS', 128, function()
@@ -529,6 +547,13 @@ function test_abort_05()
529547
assert_error_match("READERROR", function() c:perform() end)
530548
end
531549

550+
function test_abort_06()
551+
assert_equal(c, c:setopt_readfunction(function() return false end))
552+
553+
local _, e = assert_nil(c:perform())
554+
assert_equal(curl.error(curl.ERROR_EASY, curl.E_ABORTED_BY_CALLBACK), e)
555+
end
556+
532557
function test_pause()
533558
local counter = 0
534559
assert_equal(c, c:setopt_readfunction(function()
@@ -576,6 +601,15 @@ function test_readbuffer()
576601
assert_equal(("s"):rep(N), data)
577602
end
578603

604+
function test_pass_01()
605+
assert_equal(c, c:setopt_readfunction(function() return nil end))
606+
607+
assert_equal(c, c:perform())
608+
c:close()
609+
local data = read_file(fname)
610+
assert_equal(0, #data)
611+
end
612+
579613
end
580614

581615
local _ENV = TEST_CASE'escape' if ENABLE then

0 commit comments

Comments
 (0)