Skip to content

Commit bb5fa08

Browse files
committed
Add. chunk begin/end callbacks
1 parent d87d030 commit bb5fa08

File tree

2 files changed

+174
-21
lines changed

2 files changed

+174
-21
lines changed

src/lceasy.c

+172-21
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,21 @@ int lcurl_easy_create(lua_State *L, int error_mode){
7070
p->err_mode = error_mode;
7171
if(!p->curl) return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_EASY, CURLE_FAILED_INIT);
7272

73-
p->magic = LCURL_EASY_MAGIC;
74-
p->L = NULL;
75-
p->post = NULL;
76-
p->multi = NULL;
77-
p->storage = lcurl_storage_init(L);
78-
p->wr.cb_ref = p->wr.ud_ref = LUA_NOREF;
79-
p->rd.cb_ref = p->rd.ud_ref = LUA_NOREF;
80-
p->hd.cb_ref = p->hd.ud_ref = LUA_NOREF;
81-
p->pr.cb_ref = p->pr.ud_ref = LUA_NOREF;
82-
p->seek.cb_ref = p->seek.ud_ref = LUA_NOREF;
83-
p->debug.cb_ref = p->debug.ud_ref = LUA_NOREF;
84-
p->match.cb_ref = p->match.ud_ref = LUA_NOREF;
85-
p->rbuffer.ref = LUA_NOREF;
73+
p->magic = LCURL_EASY_MAGIC;
74+
p->L = NULL;
75+
p->post = NULL;
76+
p->multi = NULL;
77+
p->storage = lcurl_storage_init(L);
78+
p->wr.cb_ref = p->wr.ud_ref = LUA_NOREF;
79+
p->rd.cb_ref = p->rd.ud_ref = LUA_NOREF;
80+
p->hd.cb_ref = p->hd.ud_ref = LUA_NOREF;
81+
p->pr.cb_ref = p->pr.ud_ref = LUA_NOREF;
82+
p->seek.cb_ref = p->seek.ud_ref = LUA_NOREF;
83+
p->debug.cb_ref = p->debug.ud_ref = LUA_NOREF;
84+
p->match.cb_ref = p->match.ud_ref = LUA_NOREF;
85+
p->chunk_bgn.cb_ref = p->chunk_bgn.ud_ref = LUA_NOREF;
86+
p->chunk_end.cb_ref = p->chunk_end.ud_ref = LUA_NOREF;
87+
p->rbuffer.ref = LUA_NOREF;
8688
for(i = 0; i < LCURL_LIST_COUNT; ++i){
8789
p->lists[i] = LUA_NOREF;
8890
}
@@ -149,17 +151,23 @@ static int lcurl_easy_cleanup(lua_State *L){
149151
luaL_unref(L, LCURL_LUA_REGISTRY, p->debug.ud_ref);
150152
luaL_unref(L, LCURL_LUA_REGISTRY, p->match.cb_ref);
151153
luaL_unref(L, LCURL_LUA_REGISTRY, p->match.ud_ref);
154+
luaL_unref(L, LCURL_LUA_REGISTRY, p->chunk_bgn.cb_ref);
155+
luaL_unref(L, LCURL_LUA_REGISTRY, p->chunk_bgn.ud_ref);
156+
luaL_unref(L, LCURL_LUA_REGISTRY, p->chunk_end.cb_ref);
157+
luaL_unref(L, LCURL_LUA_REGISTRY, p->chunk_end.ud_ref);
152158
luaL_unref(L, LCURL_LUA_REGISTRY, p->hd.cb_ref);
153159
luaL_unref(L, LCURL_LUA_REGISTRY, p->hd.ud_ref);
154160
luaL_unref(L, LCURL_LUA_REGISTRY, p->rbuffer.ref);
155161

156-
p->wr.cb_ref = p->wr.ud_ref = LUA_NOREF;
157-
p->rd.cb_ref = p->rd.ud_ref = LUA_NOREF;
158-
p->hd.cb_ref = p->hd.ud_ref = LUA_NOREF;
159-
p->pr.cb_ref = p->pr.ud_ref = LUA_NOREF;
160-
p->seek.cb_ref = p->seek.ud_ref = LUA_NOREF;
161-
p->debug.cb_ref = p->debug.ud_ref = LUA_NOREF;
162-
p->match.cb_ref = p->match.ud_ref = LUA_NOREF;
162+
p->wr.cb_ref = p->wr.ud_ref = LUA_NOREF;
163+
p->rd.cb_ref = p->rd.ud_ref = LUA_NOREF;
164+
p->hd.cb_ref = p->hd.ud_ref = LUA_NOREF;
165+
p->pr.cb_ref = p->pr.ud_ref = LUA_NOREF;
166+
p->seek.cb_ref = p->seek.ud_ref = LUA_NOREF;
167+
p->debug.cb_ref = p->debug.ud_ref = LUA_NOREF;
168+
p->match.cb_ref = p->match.ud_ref = LUA_NOREF;
169+
p->chunk_bgn.cb_ref = p->chunk_bgn.ud_ref = LUA_NOREF;
170+
p->chunk_end.cb_ref = p->chunk_end.ud_ref = LUA_NOREF;
163171
p->rbuffer.ref = LUA_NOREF;
164172

165173
for(i = 0; i < LCURL_LIST_COUNT; ++i){
@@ -666,6 +674,31 @@ static int lcurl_easy_unset_FNMATCH_FUNCTION(lua_State *L){
666674
return 1;
667675
}
668676

677+
static int lcurl_easy_unset_CHUNK_BGN_FUNCTION(lua_State *L){
678+
lcurl_easy_t *p = lcurl_geteasy(L);
679+
680+
CURLcode code = curl_easy_setopt(p->curl, CURLOPT_CHUNK_BGN_FUNCTION, NULL);
681+
if(code != CURLE_OK){
682+
return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_EASY, code);
683+
}
684+
// curl_easy_setopt(p->curl, CURLOPT_CHUNK_DATA, NULL);
685+
686+
lua_settop(L, 1);
687+
return 1;
688+
}
689+
static int lcurl_easy_unset_CHUNK_END_FUNCTION(lua_State *L){
690+
lcurl_easy_t *p = lcurl_geteasy(L);
691+
692+
CURLcode code = curl_easy_setopt(p->curl, CURLOPT_CHUNK_END_FUNCTION, NULL);
693+
if(code != CURLE_OK){
694+
return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_EASY, code);
695+
}
696+
// curl_easy_setopt(p->curl, CURLOPT_CHUNK_DATA, NULL);
697+
698+
lua_settop(L, 1);
699+
return 1;
700+
}
701+
669702
#endif
670703

671704
#if LCURL_CURL_VER_GE(7,46,0)
@@ -1194,6 +1227,113 @@ static int lcurl_easy_set_FNMATCH_FUNCTION(lua_State *L){
11941227

11951228
//}
11961229

1230+
//{ Chunk begin/end
1231+
1232+
#if LCURL_CURL_VER_GE(7,21,0)
1233+
1234+
static int lcurl_chunk_bgn_callback(struct curl_fileinfo *info, void *arg, int remains) {
1235+
lcurl_easy_t *p = arg;
1236+
lua_State *L = p->L;
1237+
int ret = CURL_CHUNK_BGN_FUNC_OK;
1238+
int top = lua_gettop(L);
1239+
int n = lcurl_util_push_cb(L, &p->chunk_bgn);
1240+
1241+
assert(NULL != p->L);
1242+
1243+
lua_newtable(L);
1244+
lua_pushstring (L, info->filename ); lua_setfield(L, -2, "filename" );
1245+
lua_pushinteger(L, info->filetype ); lua_setfield(L, -2, "filetype" );
1246+
lutil_pushint64(L, info->time ); lua_setfield(L, -2, "time" );
1247+
lutil_pushint64(L, info->perm ); lua_setfield(L, -2, "perm" );
1248+
lua_pushinteger(L, info->uid ); lua_setfield(L, -2, "uid" );
1249+
lua_pushinteger(L, info->gid ); lua_setfield(L, -2, "gid" );
1250+
lutil_pushint64(L, info->size ); lua_setfield(L, -2, "size" );
1251+
lutil_pushint64(L, info->hardlinks ); lua_setfield(L, -2, "hardlinks" );
1252+
lutil_pushint64(L, info->flags ); lua_setfield(L, -2, "flags" );
1253+
1254+
lua_newtable(L);
1255+
if(info->strings.time) { lua_pushstring (L, info->strings.time ); lua_setfield(L, -2, "time" ); }
1256+
if(info->strings.perm) { lua_pushstring (L, info->strings.perm ); lua_setfield(L, -2, "perm" ); }
1257+
if(info->strings.user) { lua_pushstring (L, info->strings.user ); lua_setfield(L, -2, "user" ); }
1258+
if(info->strings.group) { lua_pushstring (L, info->strings.group ); lua_setfield(L, -2, "group" ); }
1259+
if(info->strings.target){ lua_pushstring (L, info->strings.target); lua_setfield(L, -2, "target"); }
1260+
lua_setfield(L, -2, "strings");
1261+
1262+
lua_pushinteger(L, remains);
1263+
1264+
if (lua_pcall(L, n+1, LUA_MULTRET, 0)) {
1265+
assert(lua_gettop(L) >= top);
1266+
lua_pushlightuserdata(L, (void*)LCURL_ERROR_TAG);
1267+
lua_insert(L, top + 1);
1268+
return CURL_CHUNK_BGN_FUNC_FAIL;
1269+
}
1270+
1271+
if(lua_gettop(L) > top){
1272+
if(lua_isnil(L, top + 1) && (!lua_isnoneornil(L, top + 2))){
1273+
lua_settop(L, top + 2);
1274+
lua_remove(L, top + 1);
1275+
lua_pushlightuserdata(L, (void*)LCURL_ERROR_TAG);
1276+
lua_insert(L, top + 1);
1277+
return CURL_CHUNK_BGN_FUNC_FAIL;
1278+
}
1279+
ret = lua_toboolean(L, top + 1) ? CURL_CHUNK_BGN_FUNC_OK : CURL_CHUNK_BGN_FUNC_SKIP;
1280+
}
1281+
1282+
lua_settop(L, top);
1283+
return ret;
1284+
}
1285+
1286+
static int lcurl_chunk_end_callback(void *arg) {
1287+
lcurl_easy_t *p = arg;
1288+
lua_State *L = p->L;
1289+
int ret = CURL_CHUNK_END_FUNC_OK;
1290+
int top = lua_gettop(L);
1291+
int n = lcurl_util_push_cb(L, &p->chunk_end);
1292+
1293+
assert(NULL != p->L);
1294+
1295+
if (lua_pcall(L, n-1, LUA_MULTRET, 0)) {
1296+
assert(lua_gettop(L) >= top);
1297+
lua_pushlightuserdata(L, (void*)LCURL_ERROR_TAG);
1298+
lua_insert(L, top + 1);
1299+
return CURL_CHUNK_END_FUNC_FAIL;
1300+
}
1301+
1302+
if(lua_gettop(L) > top){
1303+
if(lua_isnil(L, top + 1) && (!lua_isnoneornil(L, top + 2))){
1304+
lua_settop(L, top + 2);
1305+
lua_remove(L, top + 1);
1306+
lua_pushlightuserdata(L, (void*)LCURL_ERROR_TAG);
1307+
lua_insert(L, top + 1);
1308+
return CURL_CHUNK_END_FUNC_FAIL;
1309+
}
1310+
ret = lua_toboolean(L, top + 1) ? CURL_CHUNK_END_FUNC_OK : CURL_CHUNK_END_FUNC_FAIL;
1311+
}
1312+
1313+
lua_settop(L, top);
1314+
return ret;
1315+
}
1316+
1317+
static int lcurl_easy_set_CHUNK_BGN_FUNCTION(lua_State *L){
1318+
lcurl_easy_t *p = lcurl_geteasy(L);
1319+
return lcurl_easy_set_callback(L, p, &p->chunk_bgn,
1320+
CURLOPT_CHUNK_BGN_FUNCTION, CURLOPT_CHUNK_DATA,
1321+
"chunk_bgn", lcurl_chunk_bgn_callback
1322+
);
1323+
}
1324+
1325+
static int lcurl_easy_set_CHUNK_END_FUNCTION(lua_State *L){
1326+
lcurl_easy_t *p = lcurl_geteasy(L);
1327+
return lcurl_easy_set_callback(L, p, &p->chunk_bgn,
1328+
CURLOPT_CHUNK_END_FUNCTION, CURLOPT_CHUNK_DATA,
1329+
"chunk_bgn", lcurl_chunk_bgn_callback
1330+
);
1331+
}
1332+
1333+
#endif
1334+
1335+
//}
1336+
11971337
//}
11981338

11991339
static int lcurl_easy_setopt(lua_State *L){
@@ -1225,6 +1365,8 @@ static int lcurl_easy_setopt(lua_State *L){
12251365
OPT_ENTRY(debugfunction, DEBUGFUNCTION, TTT, 0, 0)
12261366
#if LCURL_CURL_VER_GE(7,21,0)
12271367
OPT_ENTRY(fnmatch_function, FNMATCH_FUNCTION, TTT, 0, 0)
1368+
OPT_ENTRY(chunk_bgn_function, CHUNK_BGN_FUNCTION, TTT, 0, 0)
1369+
OPT_ENTRY(chunk_end_function, CHUNK_END_FUNCTION, TTT, 0, 0)
12281370
#endif
12291371
#if LCURL_CURL_VER_GE(7,46,0)
12301372
OPT_ENTRY(stream_depends, STREAM_DEPENDS, TTT, 0, 0)
@@ -1257,6 +1399,8 @@ static int lcurl_easy_unsetopt(lua_State *L){
12571399
OPT_ENTRY(debugfunction, DEBUGFUNCTION, TTT, 0, 0)
12581400
#if LCURL_CURL_VER_GE(7,21,0)
12591401
OPT_ENTRY(fnmatch_function, FNMATCH_FUNCTION, TTT, 0, 0)
1402+
OPT_ENTRY(chunk_bgn_function, CHUNK_BGN_FUNCTION, TTT, 0, 0)
1403+
OPT_ENTRY(chunk_end_function, CHUNK_END_FUNCTION, TTT, 0, 0)
12601404
#endif
12611405
#if LCURL_CURL_VER_GE(7,46,0)
12621406
OPT_ENTRY(stream_depends, STREAM_DEPENDS, TTT, 0, 0)
@@ -1333,6 +1477,8 @@ static const struct luaL_Reg lcurl_easy_methods[] = {
13331477
OPT_ENTRY(debugfunction, DEBUGFUNCTION, TTT, 0, 0)
13341478
#if LCURL_CURL_VER_GE(7,21,0)
13351479
OPT_ENTRY(fnmatch_function, FNMATCH_FUNCTION, TTT, 0, 0)
1480+
OPT_ENTRY(chunk_bgn_function, CHUNK_BGN_FUNCTION, TTT, 0, 0)
1481+
OPT_ENTRY(chunk_end_function, CHUNK_END_FUNCTION, TTT, 0, 0)
13361482
#endif
13371483
#if LCURL_CURL_VER_GE(7,46,0)
13381484
OPT_ENTRY(stream_depends, STREAM_DEPENDS, TTT, 0, 0)
@@ -1353,6 +1499,8 @@ static const struct luaL_Reg lcurl_easy_methods[] = {
13531499
OPT_ENTRY(debugfunction, DEBUGFUNCTION, TTT, 0, 0)
13541500
#if LCURL_CURL_VER_GE(7,21,0)
13551501
OPT_ENTRY(fnmatch_function, FNMATCH_FUNCTION, TTT, 0, 0)
1502+
OPT_ENTRY(chunk_bgn_function, CHUNK_BGN_FUNCTION, TTT, 0, 0)
1503+
OPT_ENTRY(chunk_end_function, CHUNK_END_FUNCTION, TTT, 0, 0)
13561504
#endif
13571505
#if LCURL_CURL_VER_GE(7,46,0)
13581506
OPT_ENTRY(stream_depends, STREAM_DEPENDS, TTT, 0, 0)
@@ -1398,6 +1546,8 @@ static const lcurl_const_t lcurl_easy_opt[] = {
13981546
OPT_ENTRY(debugfunction, DEBUGFUNCTION, TTT, 0, 0)
13991547
#if LCURL_CURL_VER_GE(7,21,0)
14001548
OPT_ENTRY(fnmatch_function, FNMATCH_FUNCTION, TTT, 0, 0)
1549+
OPT_ENTRY(chunk_bgn_function, CHUNK_BGN_FUNCTION, TTT, 0, 0)
1550+
OPT_ENTRY(chunk_end_function, CHUNK_END_FUNCTION, TTT, 0, 0)
14011551
#endif
14021552
#if LCURL_CURL_VER_GE(7,46,0)
14031553
OPT_ENTRY(stream_depends, STREAM_DEPENDS, TTT, 0, 0)
@@ -1430,7 +1580,8 @@ void lcurl_easy_initlib(lua_State *L, int nup){
14301580
compatiable for readfunction
14311581
*/
14321582
LCURL_STATIC_ASSERT(offsetof(lcurl_easy_t, magic) == offsetof(lcurl_hpost_stream_t, magic));
1433-
LCURL_STATIC_ASSERT(sizeof(((lcurl_easy_t*)0)->magic) == sizeof(((lcurl_hpost_stream_t*)0)->magic));
1583+
LCURL_STATIC_ASSERT(offsetof(lcurl_easy_t, L) == offsetof(lcurl_hpost_stream_t, L));
1584+
LCURL_STATIC_ASSERT(offsetof(lcurl_easy_t, rd) == offsetof(lcurl_hpost_stream_t, rd));
14341585

14351586
if(!lutil_createmetap(L, LCURL_EASY, lcurl_easy_methods, nup))
14361587
lua_pop(L, nup);

src/lceasy.h

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ typedef struct lcurl_easy_tag{
6363
lcurl_callback_t seek;
6464
lcurl_callback_t debug;
6565
lcurl_callback_t match;
66+
lcurl_callback_t chunk_bgn;
67+
lcurl_callback_t chunk_end;
6668
}lcurl_easy_t;
6769

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

0 commit comments

Comments
 (0)