<p>N A has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/19736">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">utils: Refactor ast_str CURLOPT_WRITEFUNCTION callback.<br><br>This makes a callback function used to retrieve cURL<br>responses in an ast_str available publicly in utils.h,<br>so that modules can avoid duplicating this callback<br>function.<br><br>ASTERISK-30360 #close<br><br>Change-Id: Ib804ada46b345b2281062769affb1ea5a7a4249c<br>---<br>M include/asterisk/utils.h<br>M main/utils.c<br>M tests/test_res_prometheus.c<br>3 files changed, 43 insertions(+), 21 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/36/19736/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/include/asterisk/utils.h b/include/asterisk/utils.h</span><br><span>index 3c06e83..f59a87e 100644</span><br><span>--- a/include/asterisk/utils.h</span><br><span>+++ b/include/asterisk/utils.h</span><br><span>@@ -706,6 +706,12 @@</span><br><span> */</span><br><span> int ast_parse_digest(const char *digest, struct ast_http_digest *d, int request, int pedantic);</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief cURL callback function for CURLOPT_WRITEFUNCTION that fills an ast_str with response</span><br><span style="color: hsl(120, 100%, 40%);">+ * \note CURLOPT_WRITEDATA should be pass in a pointer to the ast_str</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+size_t ast_curl_write_str_callback(void *contents, size_t size, size_t nmemb, void *userdata);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> #ifdef DO_CRASH</span><br><span> #define DO_CRASH_NORETURN attribute_noreturn</span><br><span> #else</span><br><span>diff --git a/main/utils.c b/main/utils.c</span><br><span>index 6111b86..eb6dd38 100644</span><br><span>--- a/main/utils.c</span><br><span>+++ b/main/utils.c</span><br><span>@@ -2714,6 +2714,24 @@</span><br><span> return 0;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+size_t ast_curl_write_str_callback(void *contents, size_t size, size_t nmemb, void *userdata)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_str **buffer = userdata;</span><br><span style="color: hsl(120, 100%, 40%);">+ size_t realsize = size * nmemb;</span><br><span style="color: hsl(120, 100%, 40%);">+ char *rawdata;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ rawdata = ast_malloc(realsize + 1);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!rawdata) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ memcpy(rawdata, contents, realsize);</span><br><span style="color: hsl(120, 100%, 40%);">+ rawdata[realsize] = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_str_append(buffer, 0, "%s", rawdata);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_free(rawdata);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return realsize;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> int ast_get_tid(void)</span><br><span> {</span><br><span> int ret = -1;</span><br><span>diff --git a/tests/test_res_prometheus.c b/tests/test_res_prometheus.c</span><br><span>index 4df4cb9..f35e902 100644</span><br><span>--- a/tests/test_res_prometheus.c</span><br><span>+++ b/tests/test_res_prometheus.c</span><br><span>@@ -89,24 +89,6 @@</span><br><span> return curl;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-static size_t curl_write_string_callback(void *contents, size_t size, size_t nmemb, void *userdata)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_str **buffer = userdata;</span><br><span style="color: hsl(0, 100%, 40%);">- size_t realsize = size * nmemb;</span><br><span style="color: hsl(0, 100%, 40%);">- char *rawdata;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- rawdata = ast_malloc(realsize + 1);</span><br><span style="color: hsl(0, 100%, 40%);">- if (!rawdata) {</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- memcpy(rawdata, contents, realsize);</span><br><span style="color: hsl(0, 100%, 40%);">- rawdata[realsize] = 0;</span><br><span style="color: hsl(0, 100%, 40%);">- ast_str_append(buffer, 0, "%s", rawdata);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_free(rawdata);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return realsize;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> static void metric_values_get_counter_value_cb(struct prometheus_metric *metric)</span><br><span> {</span><br><span> strcpy(metric->value, "2");</span><br><span>@@ -161,7 +143,7 @@</span><br><span> strcpy(test_counter_one.value, "1");</span><br><span> </span><br><span> ast_test_status_update(test, " -> CURLing request...\n");</span><br><span style="color: hsl(0, 100%, 40%);">- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_string_callback);</span><br><span style="color: hsl(120, 100%, 40%);">+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ast_curl_write_str_callback);</span><br><span> curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);</span><br><span> res = curl_easy_perform(curl);</span><br><span> if (res != CURLE_OK) {</span><br><span>@@ -236,7 +218,7 @@</span><br><span> }</span><br><span> </span><br><span> ast_test_status_update(test, " -> CURLing request...\n");</span><br><span style="color: hsl(0, 100%, 40%);">- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_string_callback);</span><br><span style="color: hsl(120, 100%, 40%);">+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ast_curl_write_str_callback);</span><br><span> curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);</span><br><span> res = curl_easy_perform(curl);</span><br><span> if (res != CURLE_OK) {</span><br><span>@@ -675,7 +657,7 @@</span><br><span> }</span><br><span> </span><br><span> ast_test_status_update(test, " -> CURLing request...\n");</span><br><span style="color: hsl(0, 100%, 40%);">- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_string_callback);</span><br><span style="color: hsl(120, 100%, 40%);">+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ast_curl_write_str_callback);</span><br><span> curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);</span><br><span> res = curl_easy_perform(curl);</span><br><span> if (res != CURLE_OK) {</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/19736">change 19736</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/19736"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 18 </div>
<div style="display:none"> Gerrit-Change-Id: Ib804ada46b345b2281062769affb1ea5a7a4249c </div>
<div style="display:none"> Gerrit-Change-Number: 19736 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: N A <asterisk@phreaknet.org> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>