diff --git a/doc/lcurl.ldoc b/doc/lcurl.ldoc index 3b6fef3..386ff76 100644 --- a/doc/lcurl.ldoc +++ b/doc/lcurl.ldoc @@ -329,8 +329,11 @@ function setopt_readfunction() end -- the third is the number of bytes downloaded so far, -- the fourth is the total number of bytes expected to be uploaded -- in this transfer, and the fifth is the number of bytes uploaded so far. --- Function must return `true` or or nothing to continue operation. --- Otherwise the transfer will be aborted with an error. +-- Function must return `true` or `1` or nothing to continue operation. +-- Otherwise the transfer will be aborted with an error `ABORTED_BY_CALLBACK`.
+-- +-- !!! NOTE !!! This is differents form libcurl API. In libcurl returning a non-zero +-- value from this callback will cause libcurl to abort the transfer and return. -- -- @tparam function progress -- @param[opt] context progress context diff --git a/src/lceasy.c b/src/lceasy.c index c89e3c0..89da432 100644 --- a/src/lceasy.c +++ b/src/lceasy.c @@ -785,7 +785,10 @@ static int lcurl_xferinfo_callback(void *arg, curl_off_t dltotal, curl_off_t dln } if(lua_isboolean(L, top + 1)) ret = lua_toboolean(L, top + 1)?0:1; - else ret = (size_t)lua_tonumber(L, top + 1); + else{ + ret = (size_t)lua_tonumber(L, top + 1); + if(ret == 0) ret = 1; else ret = 0; + } } lua_settop(L, top); diff --git a/test/test_easy.lua b/test/test_easy.lua index b90f00f..bc48889 100644 --- a/test/test_easy.lua +++ b/test/test_easy.lua @@ -114,6 +114,115 @@ function test_write_pass_03() end +end + +local _ENV = TEST_CASE'progress_callback' do + +local c + +local function pass() end + +function teardown() + if f then f:close() end + os.remove(fname) + if c then c:close() end + f, c = nil +end + +function test_abort_01() + c = assert(scurl.easy{ + url = url, + writefunction = pass, + noprogress = false, + progressfunction = function() return false 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() + c = assert(scurl.easy{ + url = url, + writefunction = pass, + noprogress = false, + progressfunction = function() return 0 end + }) + + local _, e = assert_nil(c:perform()) + assert_equal(curl.error(curl.ERROR_EASY, curl.E_ABORTED_BY_CALLBACK), e) +end + +function test_abort_03() + c = assert(scurl.easy{ + url = url, + writefunction = pass, + noprogress = false, + progressfunction = function() return nil end + }) + + local _, e = assert_nil(c:perform()) + assert_equal(curl.error(curl.ERROR_EASY, curl.E_ABORTED_BY_CALLBACK), e) +end + +function test_abort_04() + c = assert(scurl.easy{ + url = url, + writefunction = pass, + noprogress = false, + progressfunction = function() return nil, "PROGRESSERROR" end + }) + + local _, e = assert_nil(c:perform()) + assert_equal("PROGRESSERROR", e) +end + +function test_abort_05() + c = assert(scurl.easy{ + url = url, + writefunction = pass, + noprogress = false, + progressfunction = function() error( "PROGRESSERROR" )end + }) + + assert_error_match("PROGRESSERROR", function() + c:perform() + end) +end + +function test_pass_01() + c = assert(scurl.easy{ + url = url, + writefunction = pass, + noprogress = false, + progressfunction = function() end + }) + + assert_equal(c, c:perform()) +end + +function test_pass_02() + c = assert(scurl.easy{ + url = url, + writefunction = pass, + noprogress = false, + progressfunction = function() return true end + }) + + assert_equal(c, c:perform()) +end + +function test_pass_03() + c = assert(scurl.easy{ + url = url, + writefunction = pass, + noprogress = false, + progressfunction = function() return 1 end + }) + + assert_equal(c, c:perform()) +end + end local _ENV = TEST_CASE'escape' do