Skip to content

Commit 97bd987

Browse files
committed
Add. seek callback
1 parent 479636f commit 97bd987

2 files changed

Lines changed: 72 additions & 0 deletions

File tree

src/lceasy.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ int lcurl_easy_create(lua_State *L, int error_mode){
7979
p->rd.cb_ref = p->rd.ud_ref = LUA_NOREF;
8080
p->hd.cb_ref = p->hd.ud_ref = LUA_NOREF;
8181
p->pr.cb_ref = p->pr.ud_ref = LUA_NOREF;
82+
p->seek.cb_ref = p->seek.ud_ref = LUA_NOREF;
8283
p->rbuffer.ref = LUA_NOREF;
8384
for(i = 0; i < LCURL_LIST_COUNT; ++i){
8485
p->lists[i] = LUA_NOREF;
@@ -140,6 +141,8 @@ static int lcurl_easy_cleanup(lua_State *L){
140141
luaL_unref(L, LCURL_LUA_REGISTRY, p->rd.ud_ref);
141142
luaL_unref(L, LCURL_LUA_REGISTRY, p->pr.cb_ref);
142143
luaL_unref(L, LCURL_LUA_REGISTRY, p->pr.ud_ref);
144+
luaL_unref(L, LCURL_LUA_REGISTRY, p->seek.cb_ref);
145+
luaL_unref(L, LCURL_LUA_REGISTRY, p->seek.ud_ref);
143146
luaL_unref(L, LCURL_LUA_REGISTRY, p->hd.cb_ref);
144147
luaL_unref(L, LCURL_LUA_REGISTRY, p->hd.ud_ref);
145148
luaL_unref(L, LCURL_LUA_REGISTRY, p->rbuffer.ref);
@@ -148,6 +151,7 @@ static int lcurl_easy_cleanup(lua_State *L){
148151
p->rd.cb_ref = p->rd.ud_ref = LUA_NOREF;
149152
p->hd.cb_ref = p->hd.ud_ref = LUA_NOREF;
150153
p->pr.cb_ref = p->pr.ud_ref = LUA_NOREF;
154+
p->seek.cb_ref = p->seek.ud_ref = LUA_NOREF;
151155
p->rbuffer.ref = LUA_NOREF;
152156

153157
for(i = 0; i < LCURL_LIST_COUNT; ++i){
@@ -613,6 +617,19 @@ static int lcurl_easy_unset_POSTFIELDS(lua_State *L){
613617
return 1;
614618
}
615619

620+
static int lcurl_easy_unset_SEEKFUNCTION(lua_State *L){
621+
lcurl_easy_t *p = lcurl_geteasy(L);
622+
623+
CURLcode code = curl_easy_setopt(p->curl, CURLOPT_SEEKFUNCTION, NULL);
624+
if(code != CURLE_OK){
625+
return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_EASY, code);
626+
}
627+
curl_easy_setopt(p->curl, CURLOPT_SEEKDATA, NULL);
628+
629+
lua_settop(L, 1);
630+
return 1;
631+
}
632+
616633
#if LCURL_CURL_VER_GE(7,46,0)
617634

618635
static int lcurl_easy_unset_STREAM_DEPENDS(lua_State *L){
@@ -1009,6 +1026,55 @@ static int lcurl_easy_set_PROGRESSFUNCTION(lua_State *L){
10091026

10101027
//}
10111028

1029+
//{ Seek
1030+
1031+
static int lcurl_seek_callback(void *arg, curl_off_t offset, int origin){
1032+
lcurl_easy_t *p = arg;
1033+
lua_State *L = p->L;
1034+
int ret = CURL_SEEKFUNC_OK;
1035+
int top = lua_gettop(L);
1036+
int n = lcurl_util_push_cb(L, &p->seek);
1037+
1038+
assert(NULL != p->L);
1039+
1040+
if (SEEK_SET == origin) lua_pushliteral(L, "set");
1041+
else if (SEEK_CUR == origin) lua_pushliteral(L, "cur");
1042+
else if (SEEK_END == origin) lua_pushliteral(L, "end");
1043+
else lua_pushinteger(L, origin);
1044+
lutil_pushint64(L, offset);
1045+
1046+
if (lua_pcall(L, n+1, LUA_MULTRET, 0)) {
1047+
assert(lua_gettop(L) >= top);
1048+
lua_pushlightuserdata(L, (void*)LCURL_ERROR_TAG);
1049+
lua_insert(L, top + 1);
1050+
return CURL_SEEKFUNC_FAIL;
1051+
}
1052+
1053+
if(lua_gettop(L) > top){
1054+
if(lua_isnil(L, top + 1) && (!lua_isnoneornil(L, top + 2))){
1055+
lua_settop(L, top + 2);
1056+
lua_remove(L, top + 1);
1057+
lua_pushlightuserdata(L, (void*)LCURL_ERROR_TAG);
1058+
lua_insert(L, top + 1);
1059+
return CURL_SEEKFUNC_FAIL;
1060+
}
1061+
ret = lua_toboolean(L, top + 1) ? CURL_SEEKFUNC_OK : CURL_SEEKFUNC_CANTSEEK;
1062+
}
1063+
1064+
lua_settop(L, top);
1065+
return ret;
1066+
}
1067+
1068+
static int lcurl_easy_set_SEEKFUNCTION(lua_State *L){
1069+
lcurl_easy_t *p = lcurl_geteasy(L);
1070+
return lcurl_easy_set_callback(L, p, &p->hd,
1071+
CURLOPT_SEEKFUNCTION, CURLOPT_SEEKDATA,
1072+
"seek", lcurl_seek_callback
1073+
);
1074+
}
1075+
1076+
//}
1077+
10121078
//}
10131079

10141080
static int lcurl_easy_setopt(lua_State *L){
@@ -1036,6 +1102,7 @@ static int lcurl_easy_setopt(lua_State *L){
10361102
OPT_ENTRY(readfunction, READFUNCTION, TTT, 0, 0)
10371103
OPT_ENTRY(headerfunction, HEADERFUNCTION, TTT, 0, 0)
10381104
OPT_ENTRY(progressfunction, PROGRESSFUNCTION, TTT, 0, 0)
1105+
OPT_ENTRY(seekfunction, SEEKFUNCTION, TTT, 0, 0)
10391106
#if LCURL_CURL_VER_GE(7,46,0)
10401107
OPT_ENTRY(stream_depends, STREAM_DEPENDS, TTT, 0, 0)
10411108
OPT_ENTRY(stream_depends_e, STREAM_DEPENDS_E, TTT, 0, 0)
@@ -1063,6 +1130,7 @@ static int lcurl_easy_unsetopt(lua_State *L){
10631130
OPT_ENTRY(readfunction, READFUNCTION, TTT, 0, 0)
10641131
OPT_ENTRY(headerfunction, HEADERFUNCTION, TTT, 0, 0)
10651132
OPT_ENTRY(progressfunction, PROGRESSFUNCTION, TTT, 0, 0)
1133+
OPT_ENTRY(seekfunction, SEEKFUNCTION, TTT, 0, 0)
10661134
#if LCURL_CURL_VER_GE(7,46,0)
10671135
OPT_ENTRY(stream_depends, STREAM_DEPENDS, TTT, 0, 0)
10681136
OPT_ENTRY(stream_depends_e, STREAM_DEPENDS_E, TTT, 0, 0)
@@ -1134,6 +1202,7 @@ static const struct luaL_Reg lcurl_easy_methods[] = {
11341202
OPT_ENTRY(readfunction, READFUNCTION, TTT, 0, 0)
11351203
OPT_ENTRY(headerfunction, HEADERFUNCTION, TTT, 0, 0)
11361204
OPT_ENTRY(progressfunction, PROGRESSFUNCTION, TTT, 0, 0)
1205+
OPT_ENTRY(seekfunction, SEEKFUNCTION, TTT, 0, 0)
11371206
#if LCURL_CURL_VER_GE(7,46,0)
11381207
OPT_ENTRY(stream_depends, STREAM_DEPENDS, TTT, 0, 0)
11391208
OPT_ENTRY(stream_depends_e, STREAM_DEPENDS_E, TTT, 0, 0)
@@ -1149,6 +1218,7 @@ static const struct luaL_Reg lcurl_easy_methods[] = {
11491218
OPT_ENTRY(readfunction, READFUNCTION, TTT, 0, 0)
11501219
OPT_ENTRY(headerfunction, HEADERFUNCTION, TTT, 0, 0)
11511220
OPT_ENTRY(progressfunction, PROGRESSFUNCTION, TTT, 0, 0)
1221+
OPT_ENTRY(seekfunction, SEEKFUNCTION, TTT, 0, 0)
11521222
#if LCURL_CURL_VER_GE(7,46,0)
11531223
OPT_ENTRY(stream_depends, STREAM_DEPENDS, TTT, 0, 0)
11541224
OPT_ENTRY(stream_depends_e, STREAM_DEPENDS_E, TTT, 0, 0)
@@ -1189,6 +1259,7 @@ static const lcurl_const_t lcurl_easy_opt[] = {
11891259
OPT_ENTRY(readfunction, READFUNCTION, TTT, 0, 0)
11901260
OPT_ENTRY(headerfunction, HEADERFUNCTION, TTT, 0, 0)
11911261
OPT_ENTRY(progressfunction, PROGRESSFUNCTION, TTT, 0, 0)
1262+
OPT_ENTRY(seekfunction, SEEKFUNCTION, TTT, 0, 0)
11921263
#if LCURL_CURL_VER_GE(7,46,0)
11931264
OPT_ENTRY(stream_depends, STREAM_DEPENDS, TTT, 0, 0)
11941265
OPT_ENTRY(stream_depends_e, STREAM_DEPENDS_E, TTT, 0, 0)

src/lceasy.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ typedef struct lcurl_easy_tag{
6060
lcurl_callback_t wr;
6161
lcurl_callback_t hd;
6262
lcurl_callback_t pr;
63+
lcurl_callback_t seek;
6364
}lcurl_easy_t;
6465

6566
int lcurl_easy_create(lua_State *L, int error_mode);

0 commit comments

Comments
 (0)