Skip to content

Commit 886f696

Browse files
committed
Add. Allow set null to callbacks.
1 parent e5f68e0 commit 886f696

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

src/lcutils.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,6 @@ int lcurl_set_callback(lua_State *L, lcurl_callback_t *c, int i, const char *met
171171
luaL_argcheck(L, !lua_isnoneornil(L, i), i, "no function present");
172172
luaL_argcheck(L, (top < (i + 2)), i + 2, "no arguments expected");
173173

174-
// if(top > (i + 1)) lua_settop(L, i + 1); // this for force ignore other arguments
175-
176174
assert((top == i)||(top == (i + 1)));
177175

178176
if(c->ud_ref != LUA_NOREF){
@@ -185,6 +183,19 @@ int lcurl_set_callback(lua_State *L, lcurl_callback_t *c, int i, const char *met
185183
c->cb_ref = LUA_NOREF;
186184
}
187185

186+
if(lutil_is_null(L, i)){
187+
if(top == (i + 1)){
188+
// Do we can just ignore this?
189+
luaL_argcheck(L,
190+
lua_isnoneornil(L, i + 1) || lutil_is_null(L, i + 1)
191+
,i + 1, "no context allowed when set callback to null"
192+
);
193+
}
194+
lua_pop(L, top - i + 1);
195+
196+
return 1;
197+
}
198+
188199
if(lua_gettop(L) == (i + 1)){// function + context
189200
c->ud_ref = luaL_ref(L, LCURL_LUA_REGISTRY);
190201
c->cb_ref = luaL_ref(L, LCURL_LUA_REGISTRY);

test/test_easy.lua

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,16 @@ function test_reset_write_callback()
153153
assert_equal(c, c:setopt_writefunction(f))
154154
assert_equal(c, c:setopt_writefunction(f.write, f))
155155
assert_equal(c, c:setopt_writefunction(print))
156+
assert_equal(c, c:setopt_writefunction(print, null))
157+
assert_equal(c, c:setopt_writefunction(null))
158+
assert_equal(c, c:setopt_writefunction(null, nil))
159+
assert_equal(c, c:setopt_writefunction(null, null))
156160
assert_error(function()c:setopt_writefunction()end)
157161
assert_error(function()c:setopt_writefunction(nil)end)
158162
assert_error(function()c:setopt_writefunction(nil, f)end)
163+
assert_error(function()c:setopt_writefunction(null, {})end)
164+
assert_error(function()c:setopt_writefunction(print, {}, nil)end)
165+
assert_error(function()c:setopt_writefunction(print, {}, null)end)
159166
end
160167

161168
function test_write_pass_01()
@@ -210,6 +217,38 @@ function test_write_coro()
210217
assert_equal(co2, called)
211218
end
212219

220+
function test_write_pass_null_context()
221+
c = assert(curl.easy{
222+
url = GET_URL;
223+
})
224+
225+
local context
226+
assert_equal(c, c:setopt_writefunction(function(ctx)
227+
context = ctx
228+
return true
229+
end, null))
230+
231+
assert_equal(c, c:perform())
232+
assert_equal(null, context)
233+
end
234+
235+
function test_write_pass_nil_context()
236+
c = assert(curl.easy{
237+
url = GET_URL;
238+
})
239+
240+
local context, called
241+
assert_equal(c, c:setopt_writefunction(function(ctx)
242+
context = ctx
243+
called = true
244+
return true
245+
end, nil))
246+
247+
assert_equal(c, c:perform())
248+
assert_true(called)
249+
assert_nil(context)
250+
end
251+
213252
end
214253

215254
local _ENV = TEST_CASE'progress_callback' if ENABLE then
@@ -381,9 +420,14 @@ function test_reset_header_callback()
381420
assert_equal(c, c:setopt_headerfunction(f))
382421
assert_equal(c, c:setopt_headerfunction(f.header, f))
383422
assert_equal(c, c:setopt_headerfunction(print))
423+
assert_equal(c, c:setopt_headerfunction(null))
424+
assert_equal(c, c:setopt_headerfunction(null, nil))
425+
assert_equal(c, c:setopt_headerfunction(null, null))
384426
assert_error(function()c:setopt_headerfunction()end)
385427
assert_error(function()c:setopt_headerfunction(nil)end)
386428
assert_error(function()c:setopt_headerfunction(nil, f)end)
429+
assert_error(function()c:setopt_headerfunction(null, {})end)
430+
assert_error(function()c:setopt_headerfunction(print, {}, nil)end)
387431
end
388432

389433
function test_header_pass_01()

0 commit comments

Comments
 (0)