<p>Friendly Automation <strong>submitted</strong> this change.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/13183">View Change</a></p><div style="white-space:pre-wrap">Approvals:
Kevin Harwell: Looks good to me, but someone else must approve
Joshua Colp: Looks good to me, but someone else must approve
George Joseph: Looks good to me, approved
Friendly Automation: Approved for Submit
</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">func_curl.c: Support custom http headers<br><br>When user wants to send json data, the default Content-Type header<br>is incorect (application/x-www-form-urlencoded). This patch allows<br>to set any custom headers so the Content-Type header can be<br>overriden. User can set multiple headers by multiple calls of<br>curlopt(). This approach is not consistent with other parameters,<br>but is more readable in dialplan than one call with multiple<br>headers.<br><br>ASTERISK-28613<br><br>Change-Id: I4dd68c3f4e25362ef941d73a3861f58348dcfbf9<br>---<br>A doc/CHANGES-staging/func_curl_headers.txt<br>M funcs/func_curl.c<br>2 files changed, 35 insertions(+), 8 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/doc/CHANGES-staging/func_curl_headers.txt b/doc/CHANGES-staging/func_curl_headers.txt</span><br><span>new file mode 100644</span><br><span>index 0000000..9d5c8c3</span><br><span>--- /dev/null</span><br><span>+++ b/doc/CHANGES-staging/func_curl_headers.txt</span><br><span>@@ -0,0 +1,6 @@</span><br><span style="color: hsl(120, 100%, 40%);">+Subject: func_curl</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+A new parameter, httpheader, has been added to CURLOPT function. This parameter</span><br><span style="color: hsl(120, 100%, 40%);">+allows to set custom http headers for subsequent calls off CURL function.</span><br><span style="color: hsl(120, 100%, 40%);">+Any setting of headers will replace the default curl headers</span><br><span style="color: hsl(120, 100%, 40%);">+(e.g. "Content-type: application/x-www-form-urlencoded")</span><br><span>diff --git a/funcs/func_curl.c b/funcs/func_curl.c</span><br><span>index 9e8f882..af54f7d 100644</span><br><span>--- a/funcs/func_curl.c</span><br><span>+++ b/funcs/func_curl.c</span><br><span>@@ -121,6 +121,11 @@</span><br><span> <para>Include header information in the result</span><br><span> (boolean)</para></span><br><span> </enum></span><br><span style="color: hsl(120, 100%, 40%);">+ <enum name="httpheader"></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>Add HTTP header. Multiple calls add multiple headers.</span><br><span style="color: hsl(120, 100%, 40%);">+ Setting of any header will remove the default</span><br><span style="color: hsl(120, 100%, 40%);">+ "Content-Type application/x-www-form-urlencoded"</para></span><br><span style="color: hsl(120, 100%, 40%);">+ </enum></span><br><span> <enum name="httptimeout"></span><br><span> <para>For HTTP(S) URIs, number of seconds to wait for a</span><br><span> server response</para></span><br><span>@@ -181,7 +186,7 @@</span><br><span> </syntax></span><br><span> <description></span><br><span> <para>Options may be set globally or per channel. Per-channel</span><br><span style="color: hsl(0, 100%, 40%);">- settings will override global settings.</para></span><br><span style="color: hsl(120, 100%, 40%);">+ settings will override global settings. Only HTTP headers are added instead of overriding</para></span><br><span> </description></span><br><span> <see-also></span><br><span> <ref type="function">CURL</ref></span><br><span>@@ -243,6 +248,9 @@</span><br><span> if (!strcasecmp(name, "header")) {</span><br><span> *key = CURLOPT_HEADER;</span><br><span> *ot = OT_BOOLEAN;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (!strcasecmp(name, "httpheader")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ *key = CURLOPT_HTTPHEADER;</span><br><span style="color: hsl(120, 100%, 40%);">+ *ot = OT_STRING;</span><br><span> } else if (!strcasecmp(name, "proxy")) {</span><br><span> *key = CURLOPT_PROXY;</span><br><span> *ot = OT_STRING;</span><br><span>@@ -412,16 +420,18 @@</span><br><span> return -1;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- /* Remove any existing entry */</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Remove any existing entry, only http headers are left */</span><br><span> AST_LIST_LOCK(list);</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE_SAFE_BEGIN(list, cur, list) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (cur->key == new->key) {</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_REMOVE_CURRENT(list);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_free(cur);</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (new->key != CURLOPT_HTTPHEADER) {</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE_SAFE_BEGIN(list, cur, list) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (cur->key == new->key) {</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_REMOVE_CURRENT(list);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_free(cur);</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> }</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE_SAFE_END</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">- AST_LIST_TRAVERSE_SAFE_END</span><br><span> </span><br><span> /* Insert new entry */</span><br><span> ast_debug(1, "Inserting entry %p with key %d and value %p\n", new, new->key, new->value);</span><br><span>@@ -639,6 +649,7 @@</span><br><span> int ret = -1;</span><br><span> CURL **curl;</span><br><span> struct curl_settings *cur;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct curl_slist *headers = NULL;</span><br><span> struct ast_datastore *store = NULL;</span><br><span> int hashcompat = 0;</span><br><span> AST_LIST_HEAD(global_curl_info, curl_settings) *list = NULL;</span><br><span>@@ -666,6 +677,8 @@</span><br><span> AST_LIST_TRAVERSE(&global_curl_info, cur, list) {</span><br><span> if (cur->key == CURLOPT_SPECIAL_HASHCOMPAT) {</span><br><span> hashcompat = (long) cur->value;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (cur->key == CURLOPT_HTTPHEADER) {</span><br><span style="color: hsl(120, 100%, 40%);">+ headers = curl_slist_append(headers, (char*) cur->value);</span><br><span> } else {</span><br><span> curl_easy_setopt(*curl, cur->key, cur->value);</span><br><span> }</span><br><span>@@ -682,6 +695,8 @@</span><br><span> AST_LIST_TRAVERSE(list, cur, list) {</span><br><span> if (cur->key == CURLOPT_SPECIAL_HASHCOMPAT) {</span><br><span> hashcompat = (long) cur->value;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (cur->key == CURLOPT_HTTPHEADER) {</span><br><span style="color: hsl(120, 100%, 40%);">+ headers = curl_slist_append(headers, (char*) cur->value);</span><br><span> } else {</span><br><span> curl_easy_setopt(*curl, cur->key, cur->value);</span><br><span> }</span><br><span>@@ -697,6 +712,10 @@</span><br><span> curl_easy_setopt(*curl, CURLOPT_POSTFIELDS, args->postdata);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ if (headers) {</span><br><span style="color: hsl(120, 100%, 40%);">+ curl_easy_setopt(*curl, CURLOPT_HTTPHEADER, headers);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /* Temporarily assign a buffer for curl to write errors to. */</span><br><span> curl_errbuf[0] = curl_errbuf[CURL_ERROR_SIZE] = '\0';</span><br><span> curl_easy_setopt(*curl, CURLOPT_ERRORBUFFER, curl_errbuf);</span><br><span>@@ -714,6 +733,7 @@</span><br><span> if (store) {</span><br><span> AST_LIST_UNLOCK(list);</span><br><span> }</span><br><span style="color: hsl(120, 100%, 40%);">+ curl_slist_free_all(headers);</span><br><span> </span><br><span> if (args->postdata) {</span><br><span> curl_easy_setopt(*curl, CURLOPT_POST, 0);</span><br><span>@@ -841,6 +861,7 @@</span><br><span> " ftptext - For FTP, force a text transfer (boolean)\n"</span><br><span> " ftptimeout - For FTP, the server response timeout\n"</span><br><span> " header - Retrieve header information (boolean)\n"</span><br><span style="color: hsl(120, 100%, 40%);">+" httpheader - Add new custom http header (string)\n"</span><br><span> " httptimeout - Number of seconds to wait for HTTP response\n"</span><br><span> " maxredirs - Maximum number of redirects to follow\n"</span><br><span> " proxy - Hostname or IP to use as a proxy\n"</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/13183">change 13183</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/+/13183"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: I4dd68c3f4e25362ef941d73a3861f58348dcfbf9 </div>
<div style="display:none"> Gerrit-Change-Number: 13183 </div>
<div style="display:none"> Gerrit-PatchSet: 2 </div>
<div style="display:none"> Gerrit-Owner: Martin Tomec <tomec.martin@gmail.com> </div>
<div style="display:none"> Gerrit-Reviewer: Friendly Automation </div>
<div style="display:none"> Gerrit-Reviewer: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Joshua Colp <jcolp@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Kevin Harwell <kharwell@digium.com> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>