Skip to content

Commit f78156d

Browse files
committed
Add. Debug callback
1 parent 97bd987 commit f78156d

File tree

2 files changed

+83
-18
lines changed

2 files changed

+83
-18
lines changed

src/lceasy.c

Lines changed: 82 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,18 @@ 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->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->rbuffer.ref = LUA_NOREF;
8485
for(i = 0; i < LCURL_LIST_COUNT; ++i){
8586
p->lists[i] = LUA_NOREF;
8687
}
@@ -143,16 +144,19 @@ static int lcurl_easy_cleanup(lua_State *L){
143144
luaL_unref(L, LCURL_LUA_REGISTRY, p->pr.ud_ref);
144145
luaL_unref(L, LCURL_LUA_REGISTRY, p->seek.cb_ref);
145146
luaL_unref(L, LCURL_LUA_REGISTRY, p->seek.ud_ref);
147+
luaL_unref(L, LCURL_LUA_REGISTRY, p->debug.cb_ref);
148+
luaL_unref(L, LCURL_LUA_REGISTRY, p->debug.ud_ref);
146149
luaL_unref(L, LCURL_LUA_REGISTRY, p->hd.cb_ref);
147150
luaL_unref(L, LCURL_LUA_REGISTRY, p->hd.ud_ref);
148151
luaL_unref(L, LCURL_LUA_REGISTRY, p->rbuffer.ref);
149152

150-
p->wr.cb_ref = p->wr.ud_ref = LUA_NOREF;
151-
p->rd.cb_ref = p->rd.ud_ref = LUA_NOREF;
152-
p->hd.cb_ref = p->hd.ud_ref = LUA_NOREF;
153-
p->pr.cb_ref = p->pr.ud_ref = LUA_NOREF;
154-
p->seek.cb_ref = p->seek.ud_ref = LUA_NOREF;
155-
p->rbuffer.ref = LUA_NOREF;
153+
p->wr.cb_ref = p->wr.ud_ref = LUA_NOREF;
154+
p->rd.cb_ref = p->rd.ud_ref = LUA_NOREF;
155+
p->hd.cb_ref = p->hd.ud_ref = LUA_NOREF;
156+
p->pr.cb_ref = p->pr.ud_ref = LUA_NOREF;
157+
p->seek.cb_ref = p->seek.ud_ref = LUA_NOREF;
158+
p->debug.cb_ref = p->debug.ud_ref = LUA_NOREF;
159+
p->rbuffer.ref = LUA_NOREF;
156160

157161
for(i = 0; i < LCURL_LIST_COUNT; ++i){
158162
p->lists[i] = LUA_NOREF;
@@ -630,6 +634,19 @@ static int lcurl_easy_unset_SEEKFUNCTION(lua_State *L){
630634
return 1;
631635
}
632636

637+
static int lcurl_easy_unset_DEBUGFUNCTION(lua_State *L){
638+
lcurl_easy_t *p = lcurl_geteasy(L);
639+
640+
CURLcode code = curl_easy_setopt(p->curl, CURLOPT_DEBUGFUNCTION, NULL);
641+
if(code != CURLE_OK){
642+
return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_EASY, code);
643+
}
644+
curl_easy_setopt(p->curl, CURLOPT_DEBUGDATA, NULL);
645+
646+
lua_settop(L, 1);
647+
return 1;
648+
}
649+
633650
#if LCURL_CURL_VER_GE(7,46,0)
634651

635652
static int lcurl_easy_unset_STREAM_DEPENDS(lua_State *L){
@@ -1067,14 +1084,45 @@ static int lcurl_seek_callback(void *arg, curl_off_t offset, int origin){
10671084

10681085
static int lcurl_easy_set_SEEKFUNCTION(lua_State *L){
10691086
lcurl_easy_t *p = lcurl_geteasy(L);
1070-
return lcurl_easy_set_callback(L, p, &p->hd,
1087+
return lcurl_easy_set_callback(L, p, &p->seek,
10711088
CURLOPT_SEEKFUNCTION, CURLOPT_SEEKDATA,
10721089
"seek", lcurl_seek_callback
10731090
);
10741091
}
10751092

10761093
//}
10771094

1095+
//{ Debug
1096+
1097+
static int lcurl_debug_callback(CURL *handle, curl_infotype type, char *data, size_t size, void *arg){
1098+
lcurl_easy_t *p = arg;
1099+
lua_State *L = p->L;
1100+
int top = lua_gettop(L);
1101+
int n = lcurl_util_push_cb(L, &p->debug);
1102+
1103+
assert(NULL != p->L);
1104+
assert(handle == p->curl);
1105+
1106+
lua_pushinteger(L, type);
1107+
lua_pushlstring(L, data, size);
1108+
1109+
// just ignore all errors from Lua callback
1110+
lua_pcall(L, n + 1, LUA_MULTRET, 0);
1111+
lua_settop(L, top);
1112+
1113+
return 0;
1114+
}
1115+
1116+
static int lcurl_easy_set_DEBUGFUNCTION(lua_State *L){
1117+
lcurl_easy_t *p = lcurl_geteasy(L);
1118+
return lcurl_easy_set_callback(L, p, &p->debug,
1119+
CURLOPT_DEBUGFUNCTION, CURLOPT_DEBUGDATA,
1120+
"debug", lcurl_debug_callback
1121+
);
1122+
}
1123+
1124+
//}
1125+
10781126
//}
10791127

10801128
static int lcurl_easy_setopt(lua_State *L){
@@ -1103,6 +1151,7 @@ static int lcurl_easy_setopt(lua_State *L){
11031151
OPT_ENTRY(headerfunction, HEADERFUNCTION, TTT, 0, 0)
11041152
OPT_ENTRY(progressfunction, PROGRESSFUNCTION, TTT, 0, 0)
11051153
OPT_ENTRY(seekfunction, SEEKFUNCTION, TTT, 0, 0)
1154+
OPT_ENTRY(debugfunction, DEBUGFUNCTION, TTT, 0, 0)
11061155
#if LCURL_CURL_VER_GE(7,46,0)
11071156
OPT_ENTRY(stream_depends, STREAM_DEPENDS, TTT, 0, 0)
11081157
OPT_ENTRY(stream_depends_e, STREAM_DEPENDS_E, TTT, 0, 0)
@@ -1131,6 +1180,7 @@ static int lcurl_easy_unsetopt(lua_State *L){
11311180
OPT_ENTRY(headerfunction, HEADERFUNCTION, TTT, 0, 0)
11321181
OPT_ENTRY(progressfunction, PROGRESSFUNCTION, TTT, 0, 0)
11331182
OPT_ENTRY(seekfunction, SEEKFUNCTION, TTT, 0, 0)
1183+
OPT_ENTRY(debugfunction, DEBUGFUNCTION, TTT, 0, 0)
11341184
#if LCURL_CURL_VER_GE(7,46,0)
11351185
OPT_ENTRY(stream_depends, STREAM_DEPENDS, TTT, 0, 0)
11361186
OPT_ENTRY(stream_depends_e, STREAM_DEPENDS_E, TTT, 0, 0)
@@ -1203,6 +1253,7 @@ static const struct luaL_Reg lcurl_easy_methods[] = {
12031253
OPT_ENTRY(headerfunction, HEADERFUNCTION, TTT, 0, 0)
12041254
OPT_ENTRY(progressfunction, PROGRESSFUNCTION, TTT, 0, 0)
12051255
OPT_ENTRY(seekfunction, SEEKFUNCTION, TTT, 0, 0)
1256+
OPT_ENTRY(debugfunction, DEBUGFUNCTION, TTT, 0, 0)
12061257
#if LCURL_CURL_VER_GE(7,46,0)
12071258
OPT_ENTRY(stream_depends, STREAM_DEPENDS, TTT, 0, 0)
12081259
OPT_ENTRY(stream_depends_e, STREAM_DEPENDS_E, TTT, 0, 0)
@@ -1219,6 +1270,7 @@ static const struct luaL_Reg lcurl_easy_methods[] = {
12191270
OPT_ENTRY(headerfunction, HEADERFUNCTION, TTT, 0, 0)
12201271
OPT_ENTRY(progressfunction, PROGRESSFUNCTION, TTT, 0, 0)
12211272
OPT_ENTRY(seekfunction, SEEKFUNCTION, TTT, 0, 0)
1273+
OPT_ENTRY(debugfunction, DEBUGFUNCTION, TTT, 0, 0)
12221274
#if LCURL_CURL_VER_GE(7,46,0)
12231275
OPT_ENTRY(stream_depends, STREAM_DEPENDS, TTT, 0, 0)
12241276
OPT_ENTRY(stream_depends_e, STREAM_DEPENDS_E, TTT, 0, 0)
@@ -1260,6 +1312,7 @@ static const lcurl_const_t lcurl_easy_opt[] = {
12601312
OPT_ENTRY(headerfunction, HEADERFUNCTION, TTT, 0, 0)
12611313
OPT_ENTRY(progressfunction, PROGRESSFUNCTION, TTT, 0, 0)
12621314
OPT_ENTRY(seekfunction, SEEKFUNCTION, TTT, 0, 0)
1315+
OPT_ENTRY(debugfunction, DEBUGFUNCTION, TTT, 0, 0)
12631316
#if LCURL_CURL_VER_GE(7,46,0)
12641317
OPT_ENTRY(stream_depends, STREAM_DEPENDS, TTT, 0, 0)
12651318
OPT_ENTRY(stream_depends_e, STREAM_DEPENDS_E, TTT, 0, 0)
@@ -1271,6 +1324,17 @@ static const lcurl_const_t lcurl_easy_opt[] = {
12711324
#include "lcinfoeasy.h"
12721325
#undef OPT_ENTRY
12731326

1327+
#define OPT_ENTRY(N) { #N, CURL##N },
1328+
// Debug message types not easy info
1329+
OPT_ENTRY(INFO_TEXT )
1330+
OPT_ENTRY(INFO_HEADER_IN )
1331+
OPT_ENTRY(INFO_HEADER_OUT )
1332+
OPT_ENTRY(INFO_DATA_IN )
1333+
OPT_ENTRY(INFO_DATA_OUT )
1334+
OPT_ENTRY(INFO_SSL_DATA_OUT )
1335+
OPT_ENTRY(INFO_SSL_DATA_IN )
1336+
#undef OPT_ENTRY
1337+
12741338
{NULL, 0}
12751339
};
12761340

src/lceasy.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ typedef struct lcurl_easy_tag{
6161
lcurl_callback_t hd;
6262
lcurl_callback_t pr;
6363
lcurl_callback_t seek;
64+
lcurl_callback_t debug;
6465
}lcurl_easy_t;
6566

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

0 commit comments

Comments
 (0)