lua_curl: add option to modify header; example: httpheaders={'Accept:application/json', 'authorization:Bearer ' .. token}

Origin commit data
------------------
Commit: 6a4956a0c6
Author: Jacek Jendrzej <overx300@gmail.com>
Date: 2021-04-05 (Mon, 05 Apr 2021)
This commit is contained in:
Jacek Jendrzej
2021-04-05 18:52:28 +02:00
committed by vanhofen
parent 48c49b834f
commit cc1fc0a41b
2 changed files with 23 additions and 3 deletions

View File

@@ -4,4 +4,4 @@
* to luainstance.h changes * to luainstance.h changes
*/ */
#define LUA_API_VERSION_MAJOR 1 #define LUA_API_VERSION_MAJOR 1
#define LUA_API_VERSION_MINOR 89 #define LUA_API_VERSION_MINOR 90

View File

@@ -237,6 +237,22 @@ Example:
bool pass_header = false;//pass headers to the data stream bool pass_header = false;//pass headers to the data stream
tableLookup(L, "header", pass_header); tableLookup(L, "header", pass_header);
/* httpheader */
curl_slist* hlist = NULL;
lua_pushstring(L, "httpheader");
lua_gettable(L, -2);
if (lua_istable(L, -1)) {
for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 2)) {
lua_pushvalue(L, -2);
const char *val = lua_tostring(L, -2);
if (val){
hlist = curl_slist_append(hlist, val);
}
}
}
lua_pop(L, 1);
/* httpheader end*/
lua_Integer connectTimeout = 20; lua_Integer connectTimeout = 20;
tableLookup(L, "connectTimeout", connectTimeout); tableLookup(L, "connectTimeout", connectTimeout);
@@ -299,7 +315,10 @@ Example:
curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, (silent)?1L:0L); curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, (silent)?1L:0L);
curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, (verbose)?1L:0L); curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, (verbose)?1L:0L);
curl_easy_setopt(curl_handle, CURLOPT_HEADER, (pass_header)?1L:0L); curl_easy_setopt(curl_handle, CURLOPT_HEADER, (pass_header)?1L:0L);
if (hlist) {
/* set our custom set of headers */
curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, hlist);
}
progressData pgd; progressData pgd;
if (!silent) { if (!silent) {
@@ -353,7 +372,8 @@ Example:
if ((res2 == CURLE_OK) && dredirect) if ((res2 == CURLE_OK) && dredirect)
msg += std::string("\n redirect to: ") + dredirect; msg += std::string("\n redirect to: ") + dredirect;
} }
if(hlist)
curl_slist_free_all(hlist);
curl_easy_cleanup(curl_handle); curl_easy_cleanup(curl_handle);
if (toFile) if (toFile)
fclose(fp); fclose(fp);