Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

res_rpt_http_registration: optimize curl_write_string_callback #500

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions res/res_rpt_http_registrations.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,27 +91,17 @@ static AST_RWLIST_HEAD_STATIC(registrations, http_registry);

/* from test_res_prometheus.c */
/*! \todo should be an ast_ function for this? */
static size_t curl_write_string_callback(void *contents, size_t size, size_t nmemb, void *userdata)
static size_t curl_write_string_callback(char *contents, size_t size, size_t nmemb, void *userdata)
{
struct ast_str **buffer = userdata;
size_t realsize = size * nmemb;
char *rawdata;

rawdata = ast_malloc(realsize + 1);
if (!rawdata) {
return 0;
}
memcpy(rawdata, contents, realsize);
rawdata[realsize] = 0;
ast_str_append(buffer, 0, "%s", rawdata);
ast_free(rawdata);

return realsize;
return ast_str_append(buffer, 0, "%.*s", (int) (size * nmemb), contents);
}

static struct ast_str *curl_post(const char *url, const char *header, const char *data)
{
CURL **curl;
CURLcode res;
struct ast_str *str;
long int http_code;
struct curl_slist *slist = NULL;
Expand Down Expand Up @@ -142,10 +132,13 @@ static struct ast_str *curl_post(const char *url, const char *header, const char
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 15L);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errbuf);

if (curl_easy_perform(curl) != CURLE_OK) {
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
curl_slist_free_all(slist);
if (*curl_errbuf) {
ast_log(LOG_WARNING, "%s\n", curl_errbuf);
} else {
ast_log(LOG_WARNING, "%s\n", curl_easy_strerror(res));
}
ast_log(LOG_WARNING, "Failed to curl URL '%s'\n", url);
curl_easy_cleanup(curl);
Expand Down