Add. seek callback

This commit is contained in:
Alexey Melnichuk 2017-07-12 12:15:23 +03:00
parent 479636f5bc
commit 97bd9875f7
2 changed files with 72 additions and 0 deletions

View File

@ -79,6 +79,7 @@ int lcurl_easy_create(lua_State *L, int error_mode){
p->rd.cb_ref = p->rd.ud_ref = LUA_NOREF;
p->hd.cb_ref = p->hd.ud_ref = LUA_NOREF;
p->pr.cb_ref = p->pr.ud_ref = LUA_NOREF;
p->seek.cb_ref = p->seek.ud_ref = LUA_NOREF;
p->rbuffer.ref = LUA_NOREF;
for(i = 0; i < LCURL_LIST_COUNT; ++i){
p->lists[i] = LUA_NOREF;
@ -140,6 +141,8 @@ static int lcurl_easy_cleanup(lua_State *L){
luaL_unref(L, LCURL_LUA_REGISTRY, p->rd.ud_ref);
luaL_unref(L, LCURL_LUA_REGISTRY, p->pr.cb_ref);
luaL_unref(L, LCURL_LUA_REGISTRY, p->pr.ud_ref);
luaL_unref(L, LCURL_LUA_REGISTRY, p->seek.cb_ref);
luaL_unref(L, LCURL_LUA_REGISTRY, p->seek.ud_ref);
luaL_unref(L, LCURL_LUA_REGISTRY, p->hd.cb_ref);
luaL_unref(L, LCURL_LUA_REGISTRY, p->hd.ud_ref);
luaL_unref(L, LCURL_LUA_REGISTRY, p->rbuffer.ref);
@ -148,6 +151,7 @@ static int lcurl_easy_cleanup(lua_State *L){
p->rd.cb_ref = p->rd.ud_ref = LUA_NOREF;
p->hd.cb_ref = p->hd.ud_ref = LUA_NOREF;
p->pr.cb_ref = p->pr.ud_ref = LUA_NOREF;
p->seek.cb_ref = p->seek.ud_ref = LUA_NOREF;
p->rbuffer.ref = LUA_NOREF;
for(i = 0; i < LCURL_LIST_COUNT; ++i){
@ -613,6 +617,19 @@ static int lcurl_easy_unset_POSTFIELDS(lua_State *L){
return 1;
}
static int lcurl_easy_unset_SEEKFUNCTION(lua_State *L){
lcurl_easy_t *p = lcurl_geteasy(L);
CURLcode code = curl_easy_setopt(p->curl, CURLOPT_SEEKFUNCTION, NULL);
if(code != CURLE_OK){
return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_EASY, code);
}
curl_easy_setopt(p->curl, CURLOPT_SEEKDATA, NULL);
lua_settop(L, 1);
return 1;
}
#if LCURL_CURL_VER_GE(7,46,0)
static int lcurl_easy_unset_STREAM_DEPENDS(lua_State *L){
@ -1009,6 +1026,55 @@ static int lcurl_easy_set_PROGRESSFUNCTION(lua_State *L){
//}
//{ Seek
static int lcurl_seek_callback(void *arg, curl_off_t offset, int origin){
lcurl_easy_t *p = arg;
lua_State *L = p->L;
int ret = CURL_SEEKFUNC_OK;
int top = lua_gettop(L);
int n = lcurl_util_push_cb(L, &p->seek);
assert(NULL != p->L);
if (SEEK_SET == origin) lua_pushliteral(L, "set");
else if (SEEK_CUR == origin) lua_pushliteral(L, "cur");
else if (SEEK_END == origin) lua_pushliteral(L, "end");
else lua_pushinteger(L, origin);
lutil_pushint64(L, offset);
if (lua_pcall(L, n+1, LUA_MULTRET, 0)) {
assert(lua_gettop(L) >= top);
lua_pushlightuserdata(L, (void*)LCURL_ERROR_TAG);
lua_insert(L, top + 1);
return CURL_SEEKFUNC_FAIL;
}
if(lua_gettop(L) > top){
if(lua_isnil(L, top + 1) && (!lua_isnoneornil(L, top + 2))){
lua_settop(L, top + 2);
lua_remove(L, top + 1);
lua_pushlightuserdata(L, (void*)LCURL_ERROR_TAG);
lua_insert(L, top + 1);
return CURL_SEEKFUNC_FAIL;
}
ret = lua_toboolean(L, top + 1) ? CURL_SEEKFUNC_OK : CURL_SEEKFUNC_CANTSEEK;
}
lua_settop(L, top);
return ret;
}
static int lcurl_easy_set_SEEKFUNCTION(lua_State *L){
lcurl_easy_t *p = lcurl_geteasy(L);
return lcurl_easy_set_callback(L, p, &p->hd,
CURLOPT_SEEKFUNCTION, CURLOPT_SEEKDATA,
"seek", lcurl_seek_callback
);
}
//}
//}
static int lcurl_easy_setopt(lua_State *L){
@ -1036,6 +1102,7 @@ static int lcurl_easy_setopt(lua_State *L){
OPT_ENTRY(readfunction, READFUNCTION, TTT, 0, 0)
OPT_ENTRY(headerfunction, HEADERFUNCTION, TTT, 0, 0)
OPT_ENTRY(progressfunction, PROGRESSFUNCTION, TTT, 0, 0)
OPT_ENTRY(seekfunction, SEEKFUNCTION, TTT, 0, 0)
#if LCURL_CURL_VER_GE(7,46,0)
OPT_ENTRY(stream_depends, STREAM_DEPENDS, TTT, 0, 0)
OPT_ENTRY(stream_depends_e, STREAM_DEPENDS_E, TTT, 0, 0)
@ -1063,6 +1130,7 @@ static int lcurl_easy_unsetopt(lua_State *L){
OPT_ENTRY(readfunction, READFUNCTION, TTT, 0, 0)
OPT_ENTRY(headerfunction, HEADERFUNCTION, TTT, 0, 0)
OPT_ENTRY(progressfunction, PROGRESSFUNCTION, TTT, 0, 0)
OPT_ENTRY(seekfunction, SEEKFUNCTION, TTT, 0, 0)
#if LCURL_CURL_VER_GE(7,46,0)
OPT_ENTRY(stream_depends, STREAM_DEPENDS, TTT, 0, 0)
OPT_ENTRY(stream_depends_e, STREAM_DEPENDS_E, TTT, 0, 0)
@ -1134,6 +1202,7 @@ static const struct luaL_Reg lcurl_easy_methods[] = {
OPT_ENTRY(readfunction, READFUNCTION, TTT, 0, 0)
OPT_ENTRY(headerfunction, HEADERFUNCTION, TTT, 0, 0)
OPT_ENTRY(progressfunction, PROGRESSFUNCTION, TTT, 0, 0)
OPT_ENTRY(seekfunction, SEEKFUNCTION, TTT, 0, 0)
#if LCURL_CURL_VER_GE(7,46,0)
OPT_ENTRY(stream_depends, STREAM_DEPENDS, TTT, 0, 0)
OPT_ENTRY(stream_depends_e, STREAM_DEPENDS_E, TTT, 0, 0)
@ -1149,6 +1218,7 @@ static const struct luaL_Reg lcurl_easy_methods[] = {
OPT_ENTRY(readfunction, READFUNCTION, TTT, 0, 0)
OPT_ENTRY(headerfunction, HEADERFUNCTION, TTT, 0, 0)
OPT_ENTRY(progressfunction, PROGRESSFUNCTION, TTT, 0, 0)
OPT_ENTRY(seekfunction, SEEKFUNCTION, TTT, 0, 0)
#if LCURL_CURL_VER_GE(7,46,0)
OPT_ENTRY(stream_depends, STREAM_DEPENDS, TTT, 0, 0)
OPT_ENTRY(stream_depends_e, STREAM_DEPENDS_E, TTT, 0, 0)
@ -1189,6 +1259,7 @@ static const lcurl_const_t lcurl_easy_opt[] = {
OPT_ENTRY(readfunction, READFUNCTION, TTT, 0, 0)
OPT_ENTRY(headerfunction, HEADERFUNCTION, TTT, 0, 0)
OPT_ENTRY(progressfunction, PROGRESSFUNCTION, TTT, 0, 0)
OPT_ENTRY(seekfunction, SEEKFUNCTION, TTT, 0, 0)
#if LCURL_CURL_VER_GE(7,46,0)
OPT_ENTRY(stream_depends, STREAM_DEPENDS, TTT, 0, 0)
OPT_ENTRY(stream_depends_e, STREAM_DEPENDS_E, TTT, 0, 0)

View File

@ -60,6 +60,7 @@ typedef struct lcurl_easy_tag{
lcurl_callback_t wr;
lcurl_callback_t hd;
lcurl_callback_t pr;
lcurl_callback_t seek;
}lcurl_easy_t;
int lcurl_easy_create(lua_State *L, int error_mode);