Skip to content

Commit e30104b

Browse files
committed
Fix. Progress function should return 1 to continue.
1 parent 05147e5 commit e30104b

3 files changed

Lines changed: 118 additions & 3 deletions

File tree

doc/lcurl.ldoc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,11 @@ function setopt_readfunction() end
329329
-- the third is the number of bytes downloaded so far,
330330
-- the fourth is the total number of bytes expected to be uploaded
331331
-- in this transfer, and the fifth is the number of bytes uploaded so far.
332-
-- Function must return `true` or or nothing to continue operation.
333-
-- Otherwise the transfer will be aborted with an error.
332+
-- Function must return `true` or `1` or nothing to continue operation.
333+
-- Otherwise the transfer will be aborted with an error `ABORTED_BY_CALLBACK`.<br/>
334+
--
335+
-- !!! NOTE !!! This is differents form libcurl API. In libcurl returning a non-zero
336+
-- value from this callback will cause libcurl to abort the transfer and return.
334337
--
335338
-- @tparam function progress
336339
-- @param[opt] context progress context

src/lceasy.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,10 @@ static int lcurl_xferinfo_callback(void *arg, curl_off_t dltotal, curl_off_t dln
785785
}
786786
if(lua_isboolean(L, top + 1))
787787
ret = lua_toboolean(L, top + 1)?0:1;
788-
else ret = (size_t)lua_tonumber(L, top + 1);
788+
else{
789+
ret = (size_t)lua_tonumber(L, top + 1);
790+
if(ret == 0) ret = 1; else ret = 0;
791+
}
789792
}
790793

791794
lua_settop(L, top);

test/test_easy.lua

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,115 @@ function test_write_pass_03()
114114
end
115115

116116

117+
end
118+
119+
local _ENV = TEST_CASE'progress_callback' do
120+
121+
local c
122+
123+
local function pass() end
124+
125+
function teardown()
126+
if f then f:close() end
127+
os.remove(fname)
128+
if c then c:close() end
129+
f, c = nil
130+
end
131+
132+
function test_abort_01()
133+
c = assert(scurl.easy{
134+
url = url,
135+
writefunction = pass,
136+
noprogress = false,
137+
progressfunction = function() return false end
138+
})
139+
140+
local _, e = assert_nil(c:perform())
141+
assert_equal(curl.error(curl.ERROR_EASY, curl.E_ABORTED_BY_CALLBACK), e)
142+
end
143+
144+
function test_abort_02()
145+
c = assert(scurl.easy{
146+
url = url,
147+
writefunction = pass,
148+
noprogress = false,
149+
progressfunction = function() return 0 end
150+
})
151+
152+
local _, e = assert_nil(c:perform())
153+
assert_equal(curl.error(curl.ERROR_EASY, curl.E_ABORTED_BY_CALLBACK), e)
154+
end
155+
156+
function test_abort_03()
157+
c = assert(scurl.easy{
158+
url = url,
159+
writefunction = pass,
160+
noprogress = false,
161+
progressfunction = function() return nil end
162+
})
163+
164+
local _, e = assert_nil(c:perform())
165+
assert_equal(curl.error(curl.ERROR_EASY, curl.E_ABORTED_BY_CALLBACK), e)
166+
end
167+
168+
function test_abort_04()
169+
c = assert(scurl.easy{
170+
url = url,
171+
writefunction = pass,
172+
noprogress = false,
173+
progressfunction = function() return nil, "PROGRESSERROR" end
174+
})
175+
176+
local _, e = assert_nil(c:perform())
177+
assert_equal("PROGRESSERROR", e)
178+
end
179+
180+
function test_abort_05()
181+
c = assert(scurl.easy{
182+
url = url,
183+
writefunction = pass,
184+
noprogress = false,
185+
progressfunction = function() error( "PROGRESSERROR" )end
186+
})
187+
188+
assert_error_match("PROGRESSERROR", function()
189+
c:perform()
190+
end)
191+
end
192+
193+
function test_pass_01()
194+
c = assert(scurl.easy{
195+
url = url,
196+
writefunction = pass,
197+
noprogress = false,
198+
progressfunction = function() end
199+
})
200+
201+
assert_equal(c, c:perform())
202+
end
203+
204+
function test_pass_02()
205+
c = assert(scurl.easy{
206+
url = url,
207+
writefunction = pass,
208+
noprogress = false,
209+
progressfunction = function() return true end
210+
})
211+
212+
assert_equal(c, c:perform())
213+
end
214+
215+
function test_pass_03()
216+
c = assert(scurl.easy{
217+
url = url,
218+
writefunction = pass,
219+
noprogress = false,
220+
progressfunction = function() return 1 end
221+
})
222+
223+
assert_equal(c, c:perform())
224+
end
225+
117226
end
118227

119228
local _ENV = TEST_CASE'escape' do

0 commit comments

Comments
 (0)