[Asterisk-code-review] Mofified funcs/func_curl.c so that we can set what consitutes a failu... (asterisk[16])

Dovid Bender asteriskteam at digium.com
Sun Oct 18 13:39:46 CDT 2020


Dovid Bender has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/15066 )


Change subject: Mofified funcs/func_curl.c so that we can set what consitutes a failure for curl requests.
......................................................................

Mofified funcs/func_curl.c so that we can set what consitutes a failure for curl requests.

Change-Id: I94443e508343e0a3e535e51ea6e0562767639987
---
M configs/samples/res_curl.conf.sample
M funcs/func_curl.c
2 files changed, 26 insertions(+), 6 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/66/15066/1

diff --git a/configs/samples/res_curl.conf.sample b/configs/samples/res_curl.conf.sample
index cc47298..b511899 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
+;failurecode=404,503
diff --git a/funcs/func_curl.c b/funcs/func_curl.c
index cc3b195..f6dddac 100644
--- a/funcs/func_curl.c
+++ b/funcs/func_curl.c
@@ -187,6 +187,9 @@
 							</enum>
 						</enumlist>
 					</enum>
+					<enum name="failurecode">
+						<para>Interrupt based on specific http real curl error</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, "failurecode")) {
+		*key = CURLOPT_SPECIAL_FAILURE_CODE;
+		*ot = OT_INTEGER;
 	} else {
 		return -1;
 	}
@@ -655,7 +663,9 @@
 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 */
+	long hasfailurecode=0;
 	CURL **curl;
 	struct curl_settings *cur;
 	struct curl_slist *headers = NULL;
@@ -688,6 +698,8 @@
 			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) {
+			hasfailurecode = (long) cur->value;
 		} else {
 			curl_easy_setopt(*curl, cur->key, cur->value);
 		}
@@ -706,6 +718,8 @@
 					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) {
+					hasfailurecode = (long) cur->value;
 				} else {
 					curl_easy_setopt(*curl, cur->key, cur->value);
 				}
@@ -721,10 +735,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 +752,12 @@
 	 * 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);
+	if (http_code == hasfailurecode){
+		ret=-1;
+	}
 
 	if (store) {
 		AST_LIST_UNLOCK(list);
@@ -775,7 +794,6 @@
 			ast_free(fields);
 			ast_free(values);
 		}
-		ret = 0;
 	}
 
 	if (chan) {
@@ -884,6 +902,7 @@
 "  userpwd        - A <user>:<pass> to use for authentication\n"
 "  ssl_verifypeer - Whether to verify the peer certificate (boolean)\n"
 "  hashcompat     - Result data will be compatible for use with HASH()\n"
+"  failurecode    - Interrupt based on specific http error code(integer)\n"
 "                 - if value is \"legacy\", will translate '+' to ' '\n"
 "",
 	.read = acf_curlopt_read,

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/15066
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 16
Gerrit-Change-Id: I94443e508343e0a3e535e51ea6e0562767639987
Gerrit-Change-Number: 15066
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/20201018/4edffbad/attachment.html>


More information about the asterisk-code-review mailing list