[Asterisk-code-review] utils: Refactor ast_str CURLOPT_WRITEFUNCTION callback. (asterisk[master])
N A
asteriskteam at digium.com
Fri Dec 16 11:40:04 CST 2022
N A has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/19718 )
Change subject: utils: Refactor ast_str CURLOPT_WRITEFUNCTION callback.
......................................................................
utils: Refactor ast_str CURLOPT_WRITEFUNCTION callback.
This makes a callback function used to retrieve cURL
responses in an ast_str available publicly in utils.h,
so that modules can avoid duplicating this callback
function.
ASTERISK-30359 #close
Change-Id: Ib804ada46b345b2281062769affb1ea5a7a4249c
---
M include/asterisk/utils.h
M main/utils.c
M tests/test_res_prometheus.c
3 files changed, 43 insertions(+), 21 deletions(-)
git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/18/19718/1
diff --git a/include/asterisk/utils.h b/include/asterisk/utils.h
index 3c06e83..f59a87e 100644
--- a/include/asterisk/utils.h
+++ b/include/asterisk/utils.h
@@ -706,6 +706,12 @@
*/
int ast_parse_digest(const char *digest, struct ast_http_digest *d, int request, int pedantic);
+/*!
+ * \brief cURL callback function for CURLOPT_WRITEFUNCTION that fills an ast_str with response
+ * \note CURLOPT_WRITEDATA should be pass in a pointer to the ast_str
+ */
+size_t ast_curl_write_str_callback(void *contents, size_t size, size_t nmemb, void *userdata);
+
#ifdef DO_CRASH
#define DO_CRASH_NORETURN attribute_noreturn
#else
diff --git a/main/utils.c b/main/utils.c
index 6111b86..eb6dd38 100644
--- a/main/utils.c
+++ b/main/utils.c
@@ -2714,6 +2714,24 @@
return 0;
}
+size_t ast_curl_write_str_callback(void *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;
+}
+
int ast_get_tid(void)
{
int ret = -1;
diff --git a/tests/test_res_prometheus.c b/tests/test_res_prometheus.c
index 4df4cb9..f35e902 100644
--- a/tests/test_res_prometheus.c
+++ b/tests/test_res_prometheus.c
@@ -89,24 +89,6 @@
return curl;
}
-static size_t curl_write_string_callback(void *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;
-}
-
static void metric_values_get_counter_value_cb(struct prometheus_metric *metric)
{
strcpy(metric->value, "2");
@@ -161,7 +143,7 @@
strcpy(test_counter_one.value, "1");
ast_test_status_update(test, " -> CURLing request...\n");
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_string_callback);
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ast_curl_write_str_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
@@ -236,7 +218,7 @@
}
ast_test_status_update(test, " -> CURLing request...\n");
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_string_callback);
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ast_curl_write_str_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
@@ -675,7 +657,7 @@
}
ast_test_status_update(test, " -> CURLing request...\n");
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_string_callback);
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ast_curl_write_str_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
--
To view, visit https://gerrit.asterisk.org/c/asterisk/+/19718
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: Ib804ada46b345b2281062769affb1ea5a7a4249c
Gerrit-Change-Number: 19718
Gerrit-PatchSet: 1
Gerrit-Owner: N A <asterisk at phreaknet.org>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20221216/d85495c2/attachment-0001.html>
More information about the asterisk-code-review
mailing list