<p>Dovid Bender has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/15075">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">funcs/func_curl.c: Allow user to set what return codes constitute a failure.<br>configs/samples/res_curl.conf.sample: Updated demo configs with possible options.<br><br>Currently any response from res_curl where we get an answer from the web server, regardless of what the response is (404, 403 etc.) Asterisk currently treats it as a success. This patch allows you to set which codes should be considered as a failure by Asterisk. If say we set failurecodes=404,403 then when using curl in realtime if a server gives a 404 error Asterisk will try to failover to the next option set in extconfig.conf<br><br>ASTERISK-28825<br><br>Reported by: Dovid Bender<br>Code by: Gobinda Paul<br><br>Change-Id: Ie078f87aa0a04bef73e2c3c5db0978cbec756e3e<br>---<br>M configs/samples/res_curl.conf.sample<br>M funcs/func_curl.c<br>2 files changed, 39 insertions(+), 6 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/75/15075/1</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..1e811d9 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>@@ -721,10 +744,9 @@</span><br><span>             curl_easy_setopt(*curl, CURLOPT_POSTFIELDS, args->postdata);</span><br><span>      }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-   /* Always assign the headers - even when NULL - in case we had</span><br><span style="color: hsl(0, 100%, 40%);">-   * custom headers the last time we used this shared cURL</span><br><span style="color: hsl(0, 100%, 40%);">-         * instance */</span><br><span style="color: hsl(0, 100%, 40%);">-  curl_easy_setopt(*curl, CURLOPT_HTTPHEADER, headers);</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> </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>@@ -739,6 +761,16 @@</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%);">+  ast_log(LOG_NOTICE, "CURL response code (%ld).\n", http_code);</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%);">+                      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 +807,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 +916,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/+/15075">change 15075</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/+/15075"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 16 </div>
<div style="display:none"> Gerrit-Change-Id: Ie078f87aa0a04bef73e2c3c5db0978cbec756e3e </div>
<div style="display:none"> Gerrit-Change-Number: 15075 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Dovid Bender <dovid@telecurve.com> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>