Source
xxxxxxxxxx
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
typedef struct
{
CURL *handle;
struct curl_slist *headers;
char *data;
char *headers_in;
size_t data_alloc;
size_t data_offset;
size_t headers_in_alloc;
size_t headers_in_offset;
unsigned char custom_header;
size_t headers_sz;
}
zbx_es_httprequest_t;
/* ZBX_CURL_SETOPT() macro is a code snippet to make code shorter and facilitate resource deallocation */
/* in case of error. Be careful with using ZBX_CURL_SETOPT(), duk_push_error_object() and duk_error() */
/* in functions - it is easy to get memory leaks because duk_error() causes longjmp(). */
/* Note that the caller of ZBX_CURL_SETOPT() must define variable 'int err_index' and label 'out'. */
static size_t curl_write_cb(void *ptr, size_t size, size_t nmemb, void *userdata)
{
size_t r_size = size * nmemb;
zbx_es_httprequest_t *request = (zbx_es_httprequest_t *)userdata;
zbx_str_memcpy_alloc(&request->data, &request->data_alloc, &request->data_offset, (const char *)ptr, r_size);
return r_size;
}
static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *userdata)
{
size_t r_size = size * nmemb;
zbx_es_httprequest_t *request = (zbx_es_httprequest_t *)userdata;
zbx_strncpy_alloc(&request->headers_in, &request->headers_in_alloc, &request->headers_in_offset, (const char *)ptr, r_size);
return r_size;
}
/******************************************************************************
* *
* Purpose: return backing C structure embedded in HttpRequest object *
* *
******************************************************************************/
static zbx_es_httprequest_t *es_httprequest(duk_context *ctx)
{