[Asterisk-code-review] funcs/func_curl.c: Allow user to set what return codes constitute a f... (asterisk[16])
Dovid Bender
asteriskteam at digium.com
Wed Oct 28 14:20:34 CDT 2020
Dovid Bender has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/15075 )
Change subject: funcs/func_curl.c: Allow user to set what return codes constitute a failure. configs/samples/res_curl.conf.sample: Updated demo configs with possible options.
......................................................................
funcs/func_curl.c: Allow user to set what return codes constitute a failure.
configs/samples/res_curl.conf.sample: Updated demo configs with possible options.
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
ASTERISK-28825
Reported by: Dovid Bender
Code by: Gobinda Paul
Change-Id: Ie078f87aa0a04bef73e2c3c5db0978cbec756e3e
---
M configs/samples/res_curl.conf.sample
M funcs/func_curl.c
2 files changed, 39 insertions(+), 6 deletions(-)
git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/75/15075/1
diff --git a/configs/samples/res_curl.conf.sample b/configs/samples/res_curl.conf.sample
index cc47298..2487bb2 100644
--- a/configs/samples/res_curl.conf.sample
+++ b/configs/samples/res_curl.conf.sample
@@ -6,3 +6,4 @@
proxytype=http
proxyport=8001
;proxyuserpwd=asterisk:asteriskrocks
+;failurecodes=404,408,503
diff --git a/funcs/func_curl.c b/funcs/func_curl.c
index cc3b195..1e811d9 100644
--- a/funcs/func_curl.c
+++ b/funcs/func_curl.c
@@ -187,6 +187,9 @@
</enum>
</enumlist>
</enum>
+ <enum name="failurecodes">
+ <para>A comma separated list of HTTP response codes to be treated as errors</para>
+ </enum>
</enumlist>
</parameter>
</syntax>
@@ -206,6 +209,8 @@
#define CURLOPT_SPECIAL_HASHCOMPAT ((CURLoption) -500)
+#define CURLOPT_SPECIAL_FAILURE_CODE 999
+
static void curlds_free(void *data);
static const struct ast_datastore_info curl_info = {
@@ -318,6 +323,9 @@
} else if (!strcasecmp(name, "hashcompat")) {
*key = CURLOPT_SPECIAL_HASHCOMPAT;
*ot = OT_ENUM;
+ } else if (!strcasecmp(name, "failurecodes")) {
+ *key = CURLOPT_SPECIAL_FAILURE_CODE;
+ *ot = OT_STRING;
} else {
return -1;
}
@@ -655,7 +663,11 @@
static int acf_curl_helper(struct ast_channel *chan, struct curl_args *args)
{
struct ast_str *escapebuf = ast_str_thread_get(&thread_escapebuf, 16);
- int ret = -1;
+ int ret = 0;
+ long http_code = 0; /* read curl response */
+ size_t i;
+ struct ast_vector_int hasfailurecode = { NULL };
+ char *failurecodestrings,*found;
CURL **curl;
struct curl_settings *cur;
struct curl_slist *headers = NULL;
@@ -682,12 +694,18 @@
ast_autoservice_start(chan);
}
+ AST_VECTOR_INIT(&hasfailurecode, 0); /*Initialize vector*/
AST_LIST_LOCK(&global_curl_info);
AST_LIST_TRAVERSE(&global_curl_info, cur, list) {
if (cur->key == CURLOPT_SPECIAL_HASHCOMPAT) {
hashcompat = (long) cur->value;
} else if (cur->key == CURLOPT_HTTPHEADER) {
headers = curl_slist_append(headers, (char*) cur->value);
+ } else if (cur->key == CURLOPT_SPECIAL_FAILURE_CODE) {
+ failurecodestrings = (char*) cur->value;
+ while( (found = strsep(&failurecodestrings, ",")) != NULL) {
+ AST_VECTOR_APPEND(&hasfailurecode, atoi(found));
+ }
} else {
curl_easy_setopt(*curl, cur->key, cur->value);
}
@@ -706,6 +724,11 @@
hashcompat = (long) cur->value;
} else if (cur->key == CURLOPT_HTTPHEADER) {
headers = curl_slist_append(headers, (char*) cur->value);
+ } else if (cur->key == CURLOPT_SPECIAL_FAILURE_CODE) {
+ failurecodestrings = (char*) cur->value;
+ while( (found = strsep(&failurecodestrings, ",")) != NULL) {
+ AST_VECTOR_APPEND(&hasfailurecode, atoi(found));
+ }
} else {
curl_easy_setopt(*curl, cur->key, cur->value);
}
@@ -721,10 +744,9 @@
curl_easy_setopt(*curl, CURLOPT_POSTFIELDS, args->postdata);
}
- /* Always assign the headers - even when NULL - in case we had
- * custom headers the last time we used this shared cURL
- * instance */
- curl_easy_setopt(*curl, CURLOPT_HTTPHEADER, headers);
+ if (headers) {
+ curl_easy_setopt(*curl, CURLOPT_HTTPHEADER, headers);
+ }
/* Temporarily assign a buffer for curl to write errors to. */
curl_errbuf[0] = curl_errbuf[CURL_ERROR_SIZE] = '\0';
@@ -739,6 +761,16 @@
* here, but the source allows it. See: "typecheck: allow NULL to unset
* CURLOPT_ERRORBUFFER" (62bcf005f4678a93158358265ba905bace33b834). */
curl_easy_setopt(*curl, CURLOPT_ERRORBUFFER, (char*)NULL);
+ curl_easy_getinfo (*curl, CURLINFO_RESPONSE_CODE, &http_code);
+
+ ast_log(LOG_NOTICE, "CURL response code (%ld).\n", http_code);
+ for (i = 0; i < AST_VECTOR_SIZE(&hasfailurecode); ++i) {
+ if (http_code == AST_VECTOR_GET(&hasfailurecode,i)){
+ ret=-1;
+ break;
+ }
+ }
+ AST_VECTOR_FREE(&hasfailurecode); /* Release the vector*/
if (store) {
AST_LIST_UNLOCK(list);
@@ -775,7 +807,6 @@
ast_free(fields);
ast_free(values);
}
- ret = 0;
}
if (chan) {
@@ -885,6 +916,7 @@
" ssl_verifypeer - Whether to verify the peer certificate (boolean)\n"
" hashcompat - Result data will be compatible for use with HASH()\n"
" - if value is \"legacy\", will translate '+' to ' '\n"
+" failurecodes - A comma separated list of HTTP response codes to be treated as errors\n"
"",
.read = acf_curlopt_read,
.read2 = acf_curlopt_read2,
--
To view, visit https://gerrit.asterisk.org/c/asterisk/+/15075
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings
Gerrit-Project: asterisk
Gerrit-Branch: 16
Gerrit-Change-Id: Ie078f87aa0a04bef73e2c3c5db0978cbec756e3e
Gerrit-Change-Number: 15075
Gerrit-PatchSet: 1
Gerrit-Owner: Dovid Bender <dovid at telecurve.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20201028/1e958f60/attachment.html>
More information about the asterisk-code-review
mailing list