<p>Kevin Harwell <strong>submitted</strong> this change.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/15114">View Change</a></p><div style="white-space:pre-wrap">Approvals:
Kevin Harwell: Looks good to me, approved; Approved for Submit
George Joseph: Looks good to me, but someone else must approve
</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">func_curl.c: Allow user to set what return codes constitute a failure.<br><br>Currently any response from res_curl where we get an answer from the<br>web server, regardless of what the response is (404, 403 etc.) Asterisk<br>currently treats it as a success. This patch allows you to set which<br>codes should be considered as a failure by Asterisk. If say we set<br>failurecodes=404,403 then when using curl in realtime if a server gives<br>a 404 error Asterisk will try to failover to the next option set in<br>extconfig.conf<br><br>ASTERISK-28825<br><br>Reported by: Dovid Bender<br>Code by: Gobinda Paul<br><br>Change-Id: I94443e508343e0a3e535e51ea6e0562767639987<br>---<br>M configs/samples/res_curl.conf.sample<br>M funcs/func_curl.c<br>2 files changed, 40 insertions(+), 2 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/configs/samples/res_curl.conf.sample b/configs/samples/res_curl.conf.sample</span><br><span>index cc47298..2487bb2 100644</span><br><span>--- a/configs/samples/res_curl.conf.sample</span><br><span>+++ b/configs/samples/res_curl.conf.sample</span><br><span>@@ -6,3 +6,4 @@</span><br><span> proxytype=http</span><br><span> proxyport=8001</span><br><span> ;proxyuserpwd=asterisk:asteriskrocks</span><br><span style="color: hsl(120, 100%, 40%);">+;failurecodes=404,408,503</span><br><span>diff --git a/funcs/func_curl.c b/funcs/func_curl.c</span><br><span>index cc3b195..3b04f12 100644</span><br><span>--- a/funcs/func_curl.c</span><br><span>+++ b/funcs/func_curl.c</span><br><span>@@ -187,6 +187,9 @@</span><br><span> </enum></span><br><span> </enumlist></span><br><span> </enum></span><br><span style="color: hsl(120, 100%, 40%);">+ <enum name="failurecodes"></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>A comma separated list of HTTP response codes to be treated as errors</para></span><br><span style="color: hsl(120, 100%, 40%);">+ </enum></span><br><span> </enumlist></span><br><span> </parameter></span><br><span> </syntax></span><br><span>@@ -206,6 +209,8 @@</span><br><span> </span><br><span> #define CURLOPT_SPECIAL_HASHCOMPAT ((CURLoption) -500)</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+#define CURLOPT_SPECIAL_FAILURE_CODE 999</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> static void curlds_free(void *data);</span><br><span> </span><br><span> static const struct ast_datastore_info curl_info = {</span><br><span>@@ -318,6 +323,9 @@</span><br><span> } else if (!strcasecmp(name, "hashcompat")) {</span><br><span> *key = CURLOPT_SPECIAL_HASHCOMPAT;</span><br><span> *ot = OT_ENUM;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (!strcasecmp(name, "failurecodes")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ *key = CURLOPT_SPECIAL_FAILURE_CODE;</span><br><span style="color: hsl(120, 100%, 40%);">+ *ot = OT_STRING;</span><br><span> } else {</span><br><span> return -1;</span><br><span> }</span><br><span>@@ -655,7 +663,11 @@</span><br><span> static int acf_curl_helper(struct ast_channel *chan, struct curl_args *args)</span><br><span> {</span><br><span> struct ast_str *escapebuf = ast_str_thread_get(&thread_escapebuf, 16);</span><br><span style="color: hsl(0, 100%, 40%);">- int ret = -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ int ret = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ long http_code = 0; /* read curl response */</span><br><span style="color: hsl(120, 100%, 40%);">+ size_t i;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_vector_int hasfailurecode = { NULL };</span><br><span style="color: hsl(120, 100%, 40%);">+ char *failurecodestrings,*found;</span><br><span> CURL **curl;</span><br><span> struct curl_settings *cur;</span><br><span> struct curl_slist *headers = NULL;</span><br><span>@@ -682,12 +694,18 @@</span><br><span> ast_autoservice_start(chan);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ AST_VECTOR_INIT(&hasfailurecode, 0); /*Initialize vector*/</span><br><span> AST_LIST_LOCK(&global_curl_info);</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> } else if (cur->key == CURLOPT_HTTPHEADER) {</span><br><span> headers = curl_slist_append(headers, (char*) cur->value);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (cur->key == CURLOPT_SPECIAL_FAILURE_CODE) {</span><br><span style="color: hsl(120, 100%, 40%);">+ failurecodestrings = (char*) cur->value;</span><br><span style="color: hsl(120, 100%, 40%);">+ while( (found = strsep(&failurecodestrings, ",")) != NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_VECTOR_APPEND(&hasfailurecode, atoi(found));</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> } else {</span><br><span> curl_easy_setopt(*curl, cur->key, cur->value);</span><br><span> }</span><br><span>@@ -706,6 +724,11 @@</span><br><span> hashcompat = (long) cur->value;</span><br><span> } else if (cur->key == CURLOPT_HTTPHEADER) {</span><br><span> headers = curl_slist_append(headers, (char*) cur->value);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (cur->key == CURLOPT_SPECIAL_FAILURE_CODE) {</span><br><span style="color: hsl(120, 100%, 40%);">+ failurecodestrings = (char*) cur->value;</span><br><span style="color: hsl(120, 100%, 40%);">+ while( (found = strsep(&failurecodestrings, ",")) != NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_VECTOR_APPEND(&hasfailurecode, atoi(found));</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> } else {</span><br><span> curl_easy_setopt(*curl, cur->key, cur->value);</span><br><span> }</span><br><span>@@ -739,6 +762,20 @@</span><br><span> * here, but the source allows it. See: "typecheck: allow NULL to unset</span><br><span> * CURLOPT_ERRORBUFFER" (62bcf005f4678a93158358265ba905bace33b834). */</span><br><span> curl_easy_setopt(*curl, CURLOPT_ERRORBUFFER, (char*)NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ curl_easy_getinfo (*curl, CURLINFO_RESPONSE_CODE, &http_code);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ for (i = 0; i < AST_VECTOR_SIZE(&hasfailurecode); ++i) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (http_code == AST_VECTOR_GET(&hasfailurecode,i)){</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_NOTICE, "%s%sCURL '%s' returned response code (%ld).\n",</span><br><span style="color: hsl(120, 100%, 40%);">+ chan ? ast_channel_name(chan) : "",</span><br><span style="color: hsl(120, 100%, 40%);">+ chan ? ast_channel_name(chan) : ": ",</span><br><span style="color: hsl(120, 100%, 40%);">+ args->url,</span><br><span style="color: hsl(120, 100%, 40%);">+ http_code);</span><br><span style="color: hsl(120, 100%, 40%);">+ ret=-1;</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_VECTOR_FREE(&hasfailurecode); /* Release the vector*/</span><br><span> </span><br><span> if (store) {</span><br><span> AST_LIST_UNLOCK(list);</span><br><span>@@ -775,7 +812,6 @@</span><br><span> ast_free(fields);</span><br><span> ast_free(values);</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">- ret = 0;</span><br><span> }</span><br><span> </span><br><span> if (chan) {</span><br><span>@@ -885,6 +921,7 @@</span><br><span> " ssl_verifypeer - Whether to verify the peer certificate (boolean)\n"</span><br><span> " hashcompat - Result data will be compatible for use with HASH()\n"</span><br><span> " - if value is \"legacy\", will translate '+' to ' '\n"</span><br><span style="color: hsl(120, 100%, 40%);">+" failurecodes - A comma separated list of HTTP response codes to be treated as errors\n"</span><br><span> "",</span><br><span> .read = acf_curlopt_read,</span><br><span> .read2 = acf_curlopt_read2,</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/15114">change 15114</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/+/15114"/><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: I94443e508343e0a3e535e51ea6e0562767639987 </div>
<div style="display:none"> Gerrit-Change-Number: 15114 </div>
<div style="display:none"> Gerrit-PatchSet: 2 </div>
<div style="display:none"> Gerrit-Owner: Dovid Bender <dovid@telecurve.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: Kevin Harwell <kharwell@digium.com> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>