[Asterisk-code-review] res_http_media_cache: Allow to customize parameters (asterisk[20])

Holger Hans Peter Freyther asteriskteam at digium.com
Fri Dec 9 01:07:35 CST 2022


Holger Hans Peter Freyther has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/19658 )


Change subject: res_http_media_cache: Allow to customize parameters
......................................................................

res_http_media_cache: Allow to customize parameters

Introduce the ability to read from res_http_media_cache.conf
but don't make it mandatory yet.

ASTERISK-30340

Change-Id: I2eb02ef44190e026716720419bcbdbcc8125777b
---
M res/res_http_media_cache.c
1 file changed, 103 insertions(+), 4 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/58/19658/1

diff --git a/res/res_http_media_cache.c b/res/res_http_media_cache.c
index cd5fe42..bef3750 100644
--- a/res/res_http_media_cache.c
+++ b/res/res_http_media_cache.c
@@ -31,6 +31,29 @@
 	<support_level>core</support_level>
  ***/
 
+/*** DOCUMENTATION
+	<configInfo name="res_http_media_cache" language="en_US">
+		<synopsis>HTTP media cache</synopsis>
+		<configFile name="http_media_cache.conf">
+			<configObject name="general">
+				<synopsis>General configuration</synopsis>
+				<configOption name="timeout_seconds">
+					<synopsis>The maximum time the transfer is allowed to complete in seconds. See https://curl.se/libcurl/c/CURLOPT_TIMEOUT.html for details.</synopsis>
+				</configOption>
+				<configOption name="useragent">
+					<synopsis>The HTTP User-Agent to use for requests. See https://curl.se/libcurl/c/CURLOPT_USERAGENT.html for details.</synopsis>
+				</configOption>
+				<configOption name="followlocation">
+					<synopsis>Follow HTTP 3xx redirects on requests. See https://curl.se/libcurl/c/CURLOPT_FOLLOWLOCATION.html for details.</synopsis>
+				</configOption>
+				<configOption name="maxrdirects">
+					<synopsis>The maximum number of redirects to follow. See https://curl.se/libcurl/c/CURLOPT_MAXREDIRS.html for details</synopsis>
+				</configOption>
+			</configObject>
+		</configFile>
+	</configInfo>
+***/
+
 #include "asterisk.h"
 
 #include <curl/curl.h>
@@ -44,6 +67,13 @@
 
 #define MAX_HEADER_LENGTH 1023
 
+static int cache_curl_timeout = 180;
+static char *cache_curl_useragent = NULL;
+static int cache_followlocation = 1;
+static int cache_maxredirs = 8;
+
+
+
 /*! \brief Data passed to cURL callbacks */
 struct curl_bucket_file_data {
 	/*! The \c ast_bucket_file object that caused the operation */
@@ -325,11 +355,11 @@
 	}
 
 	curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
-	curl_easy_setopt(curl, CURLOPT_TIMEOUT, 180);
+	curl_easy_setopt(curl, CURLOPT_TIMEOUT, cache_curl_timeout);
 	curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, curl_header_callback);
-	curl_easy_setopt(curl, CURLOPT_USERAGENT, AST_CURL_USER_AGENT);
-	curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
-	curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 8);
+	curl_easy_setopt(curl, CURLOPT_USERAGENT, cache_curl_useragent != NULL ? cache_curl_useragent : AST_CURL_USER_AGENT);
+	curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, cache_followlocation);
+	curl_easy_setopt(curl, CURLOPT_MAXREDIRS, cache_maxredirs);
 	curl_easy_setopt(curl, CURLOPT_URL, ast_sorcery_object_get_id(cb_data->bucket_file));
 	curl_easy_setopt(curl, CURLOPT_HEADERDATA, cb_data);
 
@@ -537,8 +567,63 @@
 	return 0;
 }
 
+static int load_config(void)
+{
+	const char *cat;
+	struct ast_config *cfg;
+	struct ast_flags config_flags = { 0 };
+
+	cfg = ast_config_load("res_http_media_cache.conf", config_flags);
+	/* Compat behavior. Ignore a missing file. */
+	if (cfg == CONFIG_STATUS_FILEMISSING) {
+		return 1;
+	}
+	if (cfg == CONFIG_STATUS_FILEINVALID) {
+		ast_log(LOG_WARNING, "Could not load res_http_media_cache.conf\n");
+		return 0;
+	}
+
+	cat = ast_category_browse(cfg, NULL);
+	while (cat) {
+		struct ast_variable *var;
+
+		var = ast_variable_browse(cfg, cat);
+
+		if (strcasecmp(cat, "general") == 0) {
+			while (var) {
+				if (strcasecmp(var->name, "timeout_seconds") == 0) {
+					cache_curl_timeout = strtoul(var->value, NULL, 0);
+				} else if (strcasecmp(var->name, "useragent") == 0) {
+					ast_free(cache_curl_useragent);
+					cache_curl_useragent = ast_strdup(var->value);
+				} else if (strcasecmp(var->name, "followlocation") == 0) {
+					cache_followlocation = ast_true(var->value);
+				} else if (strcasecmp(var->name, "maxredirects") == 0) {
+					cache_maxredirs = strtoul(var->value, NULL, 0);
+				} else {
+					ast_log(LOG_ERROR, "Unrecognized variable '%s' in category '%s'\n", var->name, cat);
+					ast_config_destroy(cfg);
+					return 0;
+				}
+				var = var->next;
+			}
+		} else {
+			ast_log(LOG_ERROR, "Unrecognized category '%s'\n", cat);
+			ast_config_destroy(cfg);
+			return 0;
+		}
+		cat = ast_category_browse(cfg, cat);
+	}
+	ast_config_destroy(cfg);
+	return 1;
+}
+
 static int load_module(void)
 {
+	if (!load_config()) {
+		return AST_MODULE_LOAD_DECLINE;
+	}
+
 	if (ast_bucket_scheme_register("http", &http_bucket_wizard, &http_bucket_file_wizard,
 			NULL, NULL)) {
 		ast_log(LOG_ERROR, "Failed to register Bucket HTTP wizard scheme implementation\n");

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

Gerrit-Project: asterisk
Gerrit-Branch: 20
Gerrit-Change-Id: I2eb02ef44190e026716720419bcbdbcc8125777b
Gerrit-Change-Number: 19658
Gerrit-PatchSet: 1
Gerrit-Owner: Holger Hans Peter Freyther <automatic at freyther.de>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20221209/7f38fa62/attachment-0001.html>


More information about the asterisk-code-review mailing list