mirror of
https://github.com/ntop/ntopng.git
synced 2026-07-10 00:14:24 +00:00
Rework ntop.httpPost to accept optional parameters in a table. Also rework Utils::httpGetPostPutPatch to reduce the number of parameters.
This commit is contained in:
parent
45237c68b5
commit
bc5fc911cd
10 changed files with 178 additions and 125 deletions
|
|
@ -41,7 +41,7 @@ function telemetry_utils.notify(obj)
|
|||
local msg = {data = obj, mail = mail, timestamp = os.time()}
|
||||
local encoded_msg = json.encode(msg)
|
||||
|
||||
local res = ntop.httpPost(TELEMETRY_URL, encoded_msg, nil, nil, TELEMETRY_TIMEOUT, true)
|
||||
local res = ntop.httpPost(TELEMETRY_URL, encoded_msg, { timeout = TELEMETRY_TIMEOUT, return_content = true })
|
||||
|
||||
if res and res["RESPONSE_CODE"] == 200 then
|
||||
ntop.rpushCache(TELEMETRY_RECORDS_SENT, encoded_msg, TELEMETRY_MAX_NUM_RECORDS)
|
||||
|
|
|
|||
|
|
@ -504,7 +504,7 @@ These functions let Lua scripts make outbound HTTP requests.
|
|||
| Lua call | Params | Returns | Description |
|
||||
|----------|--------|---------|-------------|
|
||||
| `ntop.httpGet(url [, user, pass, timeout, return_content, no_verify_cert, use_compression, follow_redirects])` | … | string\|table | GET request; returns body or nil |
|
||||
| `ntop.httpPost(url, body [, user, pass, timeout, return_content, content_type])` | … | string | POST request |
|
||||
| `ntop.httpPost(url, data [, params])` | `data`: request body; `params`: optional table with `username`, `password`, `timeout`, `return_content`, `use_cookie_auth`, `bearer`, `x_api_key`, `extra_header` | table | POST request |
|
||||
| `ntop.httpFetch(params_table)` | table | table | Full-featured HTTP fetch with all options |
|
||||
| `ntop.httpGetAuthToken(url, token [, timeout, return_content, no_verify_cert])` | … | string | GET with Bearer token |
|
||||
| `ntop.httpPostAuthToken(url, token, body [, timeout, content_type])` | … | string | POST with Bearer token |
|
||||
|
|
|
|||
|
|
@ -8,16 +8,20 @@
|
|||
--! @return table (RESPONSE_CODE, CONTENT_TYPE, EFFECTIVE_URL), with additional CONTENT and CONTENT_LEN if return_content is enabled on success, nil otherwise.
|
||||
function ntop.httpGet(string url, string username=nil, string password=nil, int timeout=nil, bool return_content=false, bool cookie_auth=false)
|
||||
|
||||
--! @brief Send an HTTP POST request with url encoded data.
|
||||
--! @brief Send an HTTP POST request.
|
||||
--! @param url the target URL.
|
||||
--! @param data the url encoded data to send.
|
||||
--! @param username for HTTP authentication.
|
||||
--! @param password for HTTP authentication.
|
||||
--! @param timeout maximum connection timeout in seconds.
|
||||
--! @param return_content enable sending response content back to the caller.
|
||||
--! @param cookie_auth Use basic (default) or cookie (used by ntopng) authentication
|
||||
--! @param data the request body to send.
|
||||
--! @param params optional table with the following fields:
|
||||
--! username (string, optional): HTTP basic auth username.
|
||||
--! password (string, optional): HTTP basic auth password.
|
||||
--! timeout (number, optional): connection timeout in seconds (default 30).
|
||||
--! return_content (boolean, optional): include response body in result (default false).
|
||||
--! use_cookie_auth (boolean, optional): use cookie-based auth instead of basic (default false).
|
||||
--! bearer (string, optional): Authorization: Bearer token value.
|
||||
--! x_api_key (string, optional): x-api-key header value.
|
||||
--! extra_header (string, optional): raw extra header line (e.g. "Foo: bar").
|
||||
--! @return table (RESPONSE_CODE, CONTENT_TYPE, EFFECTIVE_URL), with additional CONTENT and CONTENT_LEN if return_content is enabled on success, nil otherwise.
|
||||
function ntop.httpPost(string url, string data, string username=nil, string password=nil, int timeout=nil, bool return_content=false, bool cookie_auth=false)
|
||||
function ntop.httpPost(string url, string data, table params=nil)
|
||||
|
||||
--! @brief Send an HTTP POST request with json content.
|
||||
--! @param username for HTTP authentication. Pass empty string to disable authentication.
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
Subproject commit f5532295cc0e0491dc6f1d0b8c97be51e353d2b9
|
||||
Subproject commit 9dd5ab8c24cdce4e3bf082a5a07e6adb380d75df
|
||||
|
|
@ -125,12 +125,8 @@ class Utils {
|
|||
char* url, char* path, int connect_timeout,
|
||||
int max_duration_timeout,
|
||||
HTTPTranferStats* stats);
|
||||
static bool httpGetPostPutPatch(lua_State* vm, char* url, char* username, char* password, char *bearer,
|
||||
char* user_header_token, int connect_timeout, int max_duration_timeout,
|
||||
bool return_content, bool use_cookie_authentication,
|
||||
HTTPTranferStats* stats, const char* form_data, char* write_fname,
|
||||
bool follow_redirects, int ip_version, HttpMethod method,
|
||||
char* x_api_key = NULL, char* extra_header = NULL);
|
||||
static bool httpGetPostPutPatch(lua_State* vm, char* url, HttpMethod method,
|
||||
const HttpGetPostOptions& opts = {});
|
||||
static long httpGet(const char* url, const char* username,
|
||||
const char* password, const char* user_header_token,
|
||||
int connect_timeout, int max_duration_timeout,
|
||||
|
|
|
|||
|
|
@ -1288,6 +1288,25 @@ typedef enum {
|
|||
method_patch,
|
||||
} HttpMethod;
|
||||
|
||||
/* Optional parameters for Utils::httpGetPostPutPatch() */
|
||||
struct HttpGetPostOptions {
|
||||
char* username;
|
||||
char* password;
|
||||
char* bearer;
|
||||
char* user_header_token;
|
||||
int connect_timeout;
|
||||
int max_duration_timeout;
|
||||
bool return_content;
|
||||
bool use_cookie_auth;
|
||||
HTTPTranferStats* stats;
|
||||
const char* form_data;
|
||||
char* write_fname;
|
||||
bool follow_redirects;
|
||||
int ip_version;
|
||||
char* x_api_key;
|
||||
char* extra_header;
|
||||
};
|
||||
|
||||
typedef enum {
|
||||
all,
|
||||
customer_asn,
|
||||
|
|
|
|||
|
|
@ -431,8 +431,7 @@ end
|
|||
|
||||
local function multiQueryPost(queries, url, username, password)
|
||||
local query_str = table.concat(queries, ";")
|
||||
local res = ntop.httpPost(url .. "/query", "q=" .. urlencode(query_str), username, password,
|
||||
getInfluxDBQueryTimeout(), true)
|
||||
local res = ntop.httpPost(url .. "/query", "q=" .. urlencode(query_str), { username = username, password = password, timeout = getInfluxDBQueryTimeout(), return_content = true })
|
||||
|
||||
if not res then
|
||||
local err = "Invalid response for query: " .. query_str
|
||||
|
|
@ -1987,7 +1986,7 @@ function driver.init(dbname, url, days_retention, username, password, verbose)
|
|||
traceError(TRACE_NORMAL, TRACE_CONSOLE, "Checking database " .. dbname .. " ...")
|
||||
end
|
||||
local query = "SHOW DATABASES"
|
||||
local res = ntop.httpPost(url .. "/query", "q=" .. query, username, password, timeout, true)
|
||||
local res = ntop.httpPost(url .. "/query", "q=" .. query, { username = username, password = password, timeout = timeout, return_content = true })
|
||||
local db_found = false
|
||||
|
||||
if res and (res.RESPONSE_CODE == 200) and res.CONTENT then
|
||||
|
|
@ -2016,7 +2015,7 @@ function driver.init(dbname, url, days_retention, username, password, verbose)
|
|||
end
|
||||
local query = "CREATE DATABASE \"" .. dbname .. "\""
|
||||
|
||||
local res = ntop.httpPost(url .. "/query", "q=" .. query, username, password, timeout, true)
|
||||
local res = ntop.httpPost(url .. "/query", "q=" .. query, { username = username, password = password, timeout = timeout, return_content = true })
|
||||
if not res or (res.RESPONSE_CODE ~= 200) then
|
||||
local err = i18n("prefs.influxdb_create_error", {
|
||||
db = dbname,
|
||||
|
|
@ -2040,7 +2039,7 @@ function driver.init(dbname, url, days_retention, username, password, verbose)
|
|||
local query = makeRetentionPolicyQuery("ALTER", "autogen", dbname, days_retention)
|
||||
|
||||
-- tprint(query)
|
||||
local res = ntop.httpPost(url .. "/query", "q=" .. query, username, password, timeout, true)
|
||||
local res = ntop.httpPost(url .. "/query", "q=" .. query, { username = username, password = password, timeout = timeout, return_content = true })
|
||||
if not res or (res.RESPONSE_CODE ~= 200) then
|
||||
local warning = i18n("prefs.influxdb_retention_error", {
|
||||
db = dbname,
|
||||
|
|
|
|||
|
|
@ -2550,10 +2550,19 @@ static int ntop_http_get(lua_State* vm) {
|
|||
if (lua_type(vm, 9) == LUA_TSTRING)
|
||||
bearer = (char*)lua_tostring(vm, 9);
|
||||
|
||||
Utils::httpGetPostPutPatch(vm, url, username, pwd, bearer,
|
||||
NULL /* user_header_token */, connection_timeout,
|
||||
lifetime_timeout, return_content, use_cookie_authentication, &stats, NULL,
|
||||
NULL, follow_redirects, ip_version, method_get);
|
||||
HttpGetPostOptions opts;
|
||||
memset(&opts, 0, sizeof(opts));
|
||||
opts.username = username;
|
||||
opts.password = pwd;
|
||||
opts.bearer = bearer;
|
||||
opts.connect_timeout = connection_timeout;
|
||||
opts.max_duration_timeout = lifetime_timeout;
|
||||
opts.return_content = return_content;
|
||||
opts.use_cookie_auth = use_cookie_authentication;
|
||||
opts.stats = &stats;
|
||||
opts.follow_redirects = follow_redirects;
|
||||
opts.ip_version = ip_version;
|
||||
Utils::httpGetPostPutPatch(vm, url, method_get, opts);
|
||||
|
||||
return (ntop_lua_return_value(vm, __FUNCTION__, CONST_LUA_OK));
|
||||
}
|
||||
|
|
@ -2604,12 +2613,17 @@ static int ntop_http_get_auth_token(lua_State* vm) {
|
|||
|
||||
if (lua_type(vm, 7) == LUA_TNUMBER) ip_version = lua_tointeger(vm, 7);
|
||||
|
||||
Utils::httpGetPostPutPatch(vm, url, NULL /* username */, NULL /* pwd */,
|
||||
NULL, /* bearer */
|
||||
auth_token, connection_timeout, lifetime_timeout,
|
||||
return_content, use_cookie_authentication, &stats,
|
||||
NULL, NULL, follow_redirects, ip_version,
|
||||
method_get);
|
||||
HttpGetPostOptions opts;
|
||||
memset(&opts, 0, sizeof(opts));
|
||||
opts.user_header_token = auth_token;
|
||||
opts.connect_timeout = connection_timeout;
|
||||
opts.max_duration_timeout = lifetime_timeout;
|
||||
opts.return_content = return_content;
|
||||
opts.use_cookie_auth = use_cookie_authentication;
|
||||
opts.stats = &stats;
|
||||
opts.follow_redirects = follow_redirects;
|
||||
opts.ip_version = ip_version;
|
||||
Utils::httpGetPostPutPatch(vm, url, method_get, opts);
|
||||
|
||||
return (ntop_lua_return_value(vm, __FUNCTION__, CONST_LUA_OK));
|
||||
}
|
||||
|
|
@ -3248,16 +3262,17 @@ static int ntop_post_http_json_data(lua_State* vm) {
|
|||
|
||||
/* ****************************************** */
|
||||
|
||||
/* @brief Performs an outbound HTTP POST request. Lua: ntop.httpPost(url, body[,user,pass,timeout,return_content,content_type]) → string */
|
||||
/* @brief Performs an outbound HTTP POST request.
|
||||
* Lua: ntop.httpPost(url, data [, params]) where params is a table with optional parameters including
|
||||
* username, password, timeout, return_content, use_cookie_auth, bearer, x_api_key, extra_header */
|
||||
static int ntop_http_post(lua_State* vm) {
|
||||
char *username = (char*)"", *password = (char*)"", *url, *form_data;
|
||||
char *bearer = NULL;
|
||||
char *x_api_key = NULL;
|
||||
char *extra_header = NULL;
|
||||
int connection_timeout = 30, lifetime_timeout = 0;
|
||||
bool return_content = false;
|
||||
bool use_cookie_authentication = false;
|
||||
char *url, *form_data;
|
||||
HTTPTranferStats stats;
|
||||
HttpGetPostOptions opts;
|
||||
memset(&opts, 0, sizeof(opts));
|
||||
opts.follow_redirects = true;
|
||||
opts.connect_timeout = 30;
|
||||
opts.stats = &stats;
|
||||
|
||||
if (ntop_lua_check(vm, __FUNCTION__, 1, LUA_TSTRING) != CONST_LUA_OK)
|
||||
return (ntop_lua_return_value(vm, __FUNCTION__, CONST_LUA_PARAM_ERROR));
|
||||
|
|
@ -3268,38 +3283,51 @@ static int ntop_http_post(lua_State* vm) {
|
|||
return (ntop_lua_return_value(vm, __FUNCTION__, CONST_LUA_PARAM_ERROR));
|
||||
if ((form_data = (char*)lua_tostring(vm, 2)) == NULL)
|
||||
return (ntop_lua_return_value(vm, __FUNCTION__, CONST_LUA_PARAM_ERROR));
|
||||
opts.form_data = form_data;
|
||||
|
||||
if (lua_type(vm, 3) == LUA_TSTRING) /* Optional */
|
||||
if ((username = (char*)lua_tostring(vm, 3)) == NULL)
|
||||
return (ntop_lua_return_value(vm, __FUNCTION__, CONST_LUA_PARAM_ERROR));
|
||||
if (lua_type(vm, 3) == LUA_TTABLE) { /* Optional params table */
|
||||
lua_getfield(vm, 3, "username");
|
||||
if (lua_type(vm, -1) == LUA_TSTRING)
|
||||
opts.username = (char*)lua_tostring(vm, -1);
|
||||
lua_pop(vm, 1);
|
||||
|
||||
if (lua_type(vm, 4) == LUA_TSTRING) /* Optional */
|
||||
if ((password = (char*)lua_tostring(vm, 4)) == NULL)
|
||||
return (ntop_lua_return_value(vm, __FUNCTION__, CONST_LUA_PARAM_ERROR));
|
||||
lua_getfield(vm, 3, "password");
|
||||
if (lua_type(vm, -1) == LUA_TSTRING)
|
||||
opts.password = (char*)lua_tostring(vm, -1);
|
||||
lua_pop(vm, 1);
|
||||
|
||||
if (lua_type(vm, 5) == LUA_TNUMBER) /* Optional */
|
||||
connection_timeout = lua_tonumber(vm, 5);
|
||||
lua_getfield(vm, 3, "timeout");
|
||||
if (lua_type(vm, -1) == LUA_TNUMBER)
|
||||
opts.connect_timeout = (int)lua_tonumber(vm, -1);
|
||||
lua_pop(vm, 1);
|
||||
|
||||
if (lua_type(vm, 6) == LUA_TBOOLEAN) /* Optional */
|
||||
return_content = lua_toboolean(vm, 6) ? true : false;
|
||||
lua_getfield(vm, 3, "return_content");
|
||||
if (lua_type(vm, -1) == LUA_TBOOLEAN)
|
||||
opts.return_content = lua_toboolean(vm, -1) ? true : false;
|
||||
lua_pop(vm, 1);
|
||||
|
||||
if (lua_type(vm, 7) == LUA_TBOOLEAN) /* Optional */
|
||||
use_cookie_authentication = lua_toboolean(vm, 7) ? true : false;
|
||||
lua_getfield(vm, 3, "use_cookie_auth");
|
||||
if (lua_type(vm, -1) == LUA_TBOOLEAN)
|
||||
opts.use_cookie_auth = lua_toboolean(vm, -1) ? true : false;
|
||||
lua_pop(vm, 1);
|
||||
|
||||
if (lua_type(vm, 8) == LUA_TSTRING) /* Optional: Authorization: Bearer <token> */
|
||||
bearer = (char*)lua_tostring(vm, 8);
|
||||
lua_getfield(vm, 3, "bearer");
|
||||
if (lua_type(vm, -1) == LUA_TSTRING)
|
||||
opts.bearer = (char*)lua_tostring(vm, -1);
|
||||
lua_pop(vm, 1);
|
||||
|
||||
if (lua_type(vm, 9) == LUA_TSTRING) /* Optional: x-api-key header */
|
||||
x_api_key = (char*)lua_tostring(vm, 9);
|
||||
lua_getfield(vm, 3, "x_api_key");
|
||||
if (lua_type(vm, -1) == LUA_TSTRING)
|
||||
opts.x_api_key = (char*)lua_tostring(vm, -1);
|
||||
lua_pop(vm, 1);
|
||||
|
||||
if (lua_type(vm, 10) == LUA_TSTRING) /* Optional: raw extra header (e.g. "anthropic-version: 2023-06-01") */
|
||||
extra_header = (char*)lua_tostring(vm, 10);
|
||||
lua_getfield(vm, 3, "extra_header");
|
||||
if (lua_type(vm, -1) == LUA_TSTRING)
|
||||
opts.extra_header = (char*)lua_tostring(vm, -1);
|
||||
lua_pop(vm, 1);
|
||||
}
|
||||
|
||||
Utils::httpGetPostPutPatch(vm, url, username, password, bearer,
|
||||
NULL /* user_header_token */,
|
||||
connection_timeout, lifetime_timeout, return_content,
|
||||
use_cookie_authentication, &stats, form_data, NULL, true, 0, method_post,
|
||||
x_api_key, extra_header);
|
||||
Utils::httpGetPostPutPatch(vm, url, method_post, opts);
|
||||
|
||||
return (ntop_lua_return_value(vm, __FUNCTION__, CONST_LUA_OK));
|
||||
}
|
||||
|
|
@ -3340,11 +3368,17 @@ static int ntop_http_multi_auth_token(lua_State* vm, HttpMethod method) {
|
|||
if (lua_type(vm, 6) == LUA_TBOOLEAN) /* Optional */
|
||||
use_cookie_authentication = lua_toboolean(vm, 6) ? true : false;
|
||||
|
||||
Utils::httpGetPostPutPatch(vm, url, NULL /* username */, NULL /* pwd */,
|
||||
NULL, /* bearer */
|
||||
auth_token, connection_timeout, lifetime_timeout,
|
||||
return_content, use_cookie_authentication, &stats,
|
||||
form_data, NULL, true, 0, method);
|
||||
HttpGetPostOptions opts;
|
||||
memset(&opts, 0, sizeof(opts));
|
||||
opts.user_header_token = auth_token;
|
||||
opts.connect_timeout = connection_timeout;
|
||||
opts.max_duration_timeout = lifetime_timeout;
|
||||
opts.return_content = return_content;
|
||||
opts.use_cookie_auth = use_cookie_authentication;
|
||||
opts.stats = &stats;
|
||||
opts.form_data = form_data;
|
||||
opts.follow_redirects = true;
|
||||
Utils::httpGetPostPutPatch(vm, url, method, opts);
|
||||
|
||||
return (ntop_lua_return_value(vm, __FUNCTION__, CONST_LUA_OK));
|
||||
}
|
||||
|
|
@ -3388,10 +3422,14 @@ static int ntop_http_fetch(lua_State* vm) {
|
|||
snprintf(fname, sizeof(fname), "%s", f);
|
||||
ntop->fixPath(fname);
|
||||
|
||||
Utils::httpGetPostPutPatch(vm, url, NULL, NULL, NULL, /* bearer */
|
||||
NULL /* user_header_token */,
|
||||
connection_timeout, lifetime_timeout, false, false,
|
||||
&stats, NULL, fname, true, 0, method_post);
|
||||
HttpGetPostOptions opts;
|
||||
memset(&opts, 0, sizeof(opts));
|
||||
opts.connect_timeout = connection_timeout;
|
||||
opts.max_duration_timeout = lifetime_timeout;
|
||||
opts.stats = &stats;
|
||||
opts.write_fname = fname;
|
||||
opts.follow_redirects = true;
|
||||
Utils::httpGetPostPutPatch(vm, url, method_post, opts);
|
||||
|
||||
return (ntop_lua_return_value(vm, __FUNCTION__, CONST_LUA_OK));
|
||||
}
|
||||
|
|
|
|||
11
src/Ntop.cpp
11
src/Ntop.cpp
|
|
@ -5501,9 +5501,14 @@ bool Ntop::downloadCustomnDPIProtos(char* url, char* dest_file) {
|
|||
return (true); /* Fresh file: no need to re-download it */
|
||||
}
|
||||
|
||||
bool rc = Utils::httpGetPostPutPatch(NULL, custom_ndpi_protos, NULL, NULL, NULL,
|
||||
NULL, 10, 30, false, false, NULL, NULL,
|
||||
dest_file, true, 4, method_get);
|
||||
HttpGetPostOptions opts;
|
||||
memset(&opts, 0, sizeof(opts));
|
||||
opts.connect_timeout = 10;
|
||||
opts.max_duration_timeout = 30;
|
||||
opts.write_fname = dest_file;
|
||||
opts.follow_redirects = true;
|
||||
opts.ip_version = 4;
|
||||
bool rc = Utils::httpGetPostPutPatch(NULL, custom_ndpi_protos, method_get, opts);
|
||||
|
||||
if (rc == false) {
|
||||
ntop->getTrace()->traceEvent(TRACE_WARNING,
|
||||
|
|
|
|||
|
|
@ -2359,17 +2359,9 @@ static int progress_callback(void* clientp, double dltotal, double dlnow,
|
|||
/* **************************************** */
|
||||
|
||||
/* form_data is in format param=value¶m1=&value1... */
|
||||
bool Utils::httpGetPostPutPatch(lua_State* vm, char* url,
|
||||
/* NOTE if user_header_token != NULL, username
|
||||
AND password are ignored, and vice-versa */
|
||||
char* username, char* password, char *bearer,
|
||||
char* user_header_token, int connect_timeout,
|
||||
int max_duration_timeout, bool return_content,
|
||||
bool use_cookie_authentication,
|
||||
HTTPTranferStats* stats, const char* form_data,
|
||||
char* write_fname, bool follow_redirects,
|
||||
int ip_version, HttpMethod method,
|
||||
char* x_api_key, char* extra_header) {
|
||||
/* NOTE if user_header_token != NULL, username AND password are ignored, and vice-versa */
|
||||
bool Utils::httpGetPostPutPatch(lua_State* vm, char* url, HttpMethod method,
|
||||
const HttpGetPostOptions& opts) {
|
||||
CURL* curl = curl_easy_init();
|
||||
FILE* out_f = NULL;
|
||||
bool ret = true;
|
||||
|
|
@ -2389,32 +2381,32 @@ bool Utils::httpGetPostPutPatch(lua_State* vm, char* url,
|
|||
|
||||
fillcURLProxy(curl);
|
||||
|
||||
if (stats) memset(stats, 0, sizeof(HTTPTranferStats));
|
||||
if (opts.stats) memset(opts.stats, 0, sizeof(HTTPTranferStats));
|
||||
curl_easy_setopt(curl, CURLOPT_URL, url);
|
||||
|
||||
if (user_header_token != NULL) {
|
||||
if (opts.user_header_token != NULL) {
|
||||
snprintf(tokenBuffer, sizeof(tokenBuffer), "Authorization: Token %s",
|
||||
user_header_token);
|
||||
opts.user_header_token);
|
||||
} else {
|
||||
if (bearer && bearer[0] != '\0') {
|
||||
if (opts.bearer && opts.bearer[0] != '\0') {
|
||||
#ifdef CURLAUTH_BEARER
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BEARER);
|
||||
curl_easy_setopt(curl, CURLOPT_XOAUTH2_BEARER, bearer);
|
||||
curl_easy_setopt(curl, CURLOPT_XOAUTH2_BEARER, opts.bearer);
|
||||
#else
|
||||
ntop->getTrace()->traceEvent(TRACE_WARNING, "Bearer auth is not supported by curl (%s)", url);
|
||||
return (false);
|
||||
#endif
|
||||
} else if (username || password) {
|
||||
} else if (opts.username || opts.password) {
|
||||
char auth[64];
|
||||
|
||||
if (use_cookie_authentication) {
|
||||
if (opts.use_cookie_auth) {
|
||||
snprintf(auth, sizeof(auth), "user=%s; password=%s",
|
||||
username ? username : "", password ? password : "");
|
||||
opts.username ? opts.username : "", opts.password ? opts.password : "");
|
||||
curl_easy_setopt(curl, CURLOPT_COOKIE, auth);
|
||||
} else {
|
||||
if (username && (username[0] != '\0')) {
|
||||
snprintf(auth, sizeof(auth), "%s:%s", username ? username : "",
|
||||
password ? password : "");
|
||||
if (opts.username && (opts.username[0] != '\0')) {
|
||||
snprintf(auth, sizeof(auth), "%s:%s",
|
||||
opts.username ? opts.username : "", opts.password ? opts.password : "");
|
||||
curl_easy_setopt(curl, CURLOPT_USERPWD, auth);
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_BASIC);
|
||||
}
|
||||
|
|
@ -2438,15 +2430,15 @@ bool Utils::httpGetPostPutPatch(lua_State* vm, char* url,
|
|||
#endif
|
||||
}
|
||||
|
||||
if (form_data) {
|
||||
if (opts.form_data) {
|
||||
/* This is a POST request */
|
||||
|
||||
if (method == method_post) curl_easy_setopt(curl, CURLOPT_POST, 1L);
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, form_data);
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(form_data));
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, opts.form_data);
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(opts.form_data));
|
||||
|
||||
if (form_data[0] == '{' /* JSON */) {
|
||||
if (opts.form_data[0] == '{' /* JSON */) {
|
||||
headers = curl_slist_append(headers, "Content-Type: application/json");
|
||||
|
||||
if (tokenBuffer[0] != '\0') {
|
||||
|
|
@ -2461,15 +2453,15 @@ bool Utils::httpGetPostPutPatch(lua_State* vm, char* url,
|
|||
used_tokenBuffer = true;
|
||||
}
|
||||
|
||||
if (x_api_key != NULL && x_api_key[0] != '\0') {
|
||||
if (opts.x_api_key != NULL && opts.x_api_key[0] != '\0') {
|
||||
char x_api_key_header[512];
|
||||
|
||||
snprintf(x_api_key_header, sizeof(x_api_key_header), "X-API-Key: %s", x_api_key);
|
||||
|
||||
snprintf(x_api_key_header, sizeof(x_api_key_header), "X-API-Key: %s", opts.x_api_key);
|
||||
headers = curl_slist_append(headers, x_api_key_header);
|
||||
}
|
||||
|
||||
if (extra_header != NULL && extra_header[0] != '\0')
|
||||
headers = curl_slist_append(headers, extra_header);
|
||||
if (opts.extra_header != NULL && opts.extra_header[0] != '\0')
|
||||
headers = curl_slist_append(headers, opts.extra_header);
|
||||
|
||||
if (headers != NULL) curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
||||
|
||||
|
|
@ -2488,18 +2480,18 @@ bool Utils::httpGetPostPutPatch(lua_State* vm, char* url,
|
|||
break;
|
||||
}
|
||||
|
||||
if (!strncmp(url, "https", 5) && ntop->getPrefs()->do_insecure_tls()) {
|
||||
if (!strncmp(url, "https", 5) && ntop->getPrefs()->do_insecure_tls()) {
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
|
||||
}
|
||||
|
||||
if (write_fname) {
|
||||
ntop->fixPath(write_fname);
|
||||
out_f = fopen(write_fname, "wb");
|
||||
|
||||
if (opts.write_fname) {
|
||||
ntop->fixPath(opts.write_fname);
|
||||
out_f = fopen(opts.write_fname, "wb");
|
||||
|
||||
if (out_f == NULL) {
|
||||
ntop->getTrace()->traceEvent(TRACE_ERROR, "Could not open %s for write",
|
||||
write_fname, strerror(errno));
|
||||
opts.write_fname, strerror(errno));
|
||||
curl_easy_cleanup(curl);
|
||||
if (vm) lua_pushnil(vm);
|
||||
return (false);
|
||||
|
|
@ -2518,7 +2510,7 @@ bool Utils::httpGetPostPutPatch(lua_State* vm, char* url,
|
|||
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, curl_hdf);
|
||||
|
||||
state->vm = vm, state->header_over = 0,
|
||||
state->return_content = return_content;
|
||||
state->return_content = opts.return_content;
|
||||
} else {
|
||||
ntop->getTrace()->traceEvent(TRACE_WARNING, "Out of memory");
|
||||
curl_easy_cleanup(curl);
|
||||
|
|
@ -2527,29 +2519,29 @@ bool Utils::httpGetPostPutPatch(lua_State* vm, char* url,
|
|||
}
|
||||
}
|
||||
|
||||
if (follow_redirects) {
|
||||
if (opts.follow_redirects) {
|
||||
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
|
||||
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 5);
|
||||
}
|
||||
|
||||
if (ip_version == 4)
|
||||
if (opts.ip_version == 4)
|
||||
curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
|
||||
else if (ip_version == 6)
|
||||
else if (opts.ip_version == 6)
|
||||
curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6);
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
|
||||
|
||||
if (connect_timeout > 0) {
|
||||
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, connect_timeout);
|
||||
if (opts.connect_timeout > 0) {
|
||||
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, opts.connect_timeout);
|
||||
#ifdef CURLOPT_CONNECTTIMEOUT_MS
|
||||
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, connect_timeout * 1000);
|
||||
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, opts.connect_timeout * 1000);
|
||||
#endif
|
||||
}
|
||||
|
||||
if (max_duration_timeout > 0)
|
||||
curl_easy_setopt(curl, CURLOPT_TIMEOUT, max_duration_timeout);
|
||||
if (opts.max_duration_timeout > 0)
|
||||
curl_easy_setopt(curl, CURLOPT_TIMEOUT, opts.max_duration_timeout);
|
||||
|
||||
if (!form_data) {
|
||||
if (!opts.form_data) {
|
||||
/* A GET request, track client connection status */
|
||||
memset(&progressState, 0, sizeof(progressState));
|
||||
progressState.vm = vm;
|
||||
|
|
@ -2586,7 +2578,7 @@ bool Utils::httpGetPostPutPatch(lua_State* vm, char* url,
|
|||
|
||||
if (curlcode == CURLE_OK) {
|
||||
if (vm) {
|
||||
if (return_content && state) {
|
||||
if (opts.return_content && state) {
|
||||
lua_push_str_table_entry(vm, "CONTENT", state->outbuf);
|
||||
lua_push_uint64_table_entry(vm, "CONTENT_LEN", state->num_bytes);
|
||||
}
|
||||
|
|
@ -2606,7 +2598,7 @@ bool Utils::httpGetPostPutPatch(lua_State* vm, char* url,
|
|||
}
|
||||
|
||||
if (vm) {
|
||||
if (stats) readCurlStats(curl, stats, vm);
|
||||
if (opts.stats) readCurlStats(curl, opts.stats, vm);
|
||||
|
||||
if (curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code) ==
|
||||
CURLE_OK)
|
||||
|
|
@ -2621,7 +2613,7 @@ bool Utils::httpGetPostPutPatch(lua_State* vm, char* url,
|
|||
CURLE_OK)
|
||||
lua_push_str_table_entry(vm, "EFFECTIVE_URL", redirection);
|
||||
|
||||
if (!form_data) {
|
||||
if (!opts.form_data) {
|
||||
lua_push_uint64_table_entry(vm, "BYTES_DOWNLOAD",
|
||||
progressState.bytes.download);
|
||||
lua_push_uint64_table_entry(vm, "BYTES_UPLOAD",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue