<p>Holger Hans Peter Freyther has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/19658">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">res_http_media_cache: Allow to customize parameters<br><br>Introduce the ability to read from res_http_media_cache.conf<br>but don't make it mandatory yet.<br><br>ASTERISK-30340<br><br>Change-Id: I2eb02ef44190e026716720419bcbdbcc8125777b<br>---<br>M res/res_http_media_cache.c<br>1 file changed, 103 insertions(+), 4 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/58/19658/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/res/res_http_media_cache.c b/res/res_http_media_cache.c</span><br><span>index cd5fe42..bef3750 100644</span><br><span>--- a/res/res_http_media_cache.c</span><br><span>+++ b/res/res_http_media_cache.c</span><br><span>@@ -31,6 +31,29 @@</span><br><span> <support_level>core</support_level></span><br><span> ***/</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+/*** DOCUMENTATION</span><br><span style="color: hsl(120, 100%, 40%);">+ <configInfo name="res_http_media_cache" language="en_US"></span><br><span style="color: hsl(120, 100%, 40%);">+ <synopsis>HTTP media cache</synopsis></span><br><span style="color: hsl(120, 100%, 40%);">+ <configFile name="http_media_cache.conf"></span><br><span style="color: hsl(120, 100%, 40%);">+ <configObject name="general"></span><br><span style="color: hsl(120, 100%, 40%);">+ <synopsis>General configuration</synopsis></span><br><span style="color: hsl(120, 100%, 40%);">+ <configOption name="timeout_seconds"></span><br><span style="color: hsl(120, 100%, 40%);">+ <synopsis>The maximum time the transfer is allowed to complete in seconds. See https://curl.se/libcurl/c/CURLOPT_TIMEOUT.html for details.</synopsis></span><br><span style="color: hsl(120, 100%, 40%);">+ </configOption></span><br><span style="color: hsl(120, 100%, 40%);">+ <configOption name="useragent"></span><br><span style="color: hsl(120, 100%, 40%);">+ <synopsis>The HTTP User-Agent to use for requests. See https://curl.se/libcurl/c/CURLOPT_USERAGENT.html for details.</synopsis></span><br><span style="color: hsl(120, 100%, 40%);">+ </configOption></span><br><span style="color: hsl(120, 100%, 40%);">+ <configOption name="followlocation"></span><br><span style="color: hsl(120, 100%, 40%);">+ <synopsis>Follow HTTP 3xx redirects on requests. See https://curl.se/libcurl/c/CURLOPT_FOLLOWLOCATION.html for details.</synopsis></span><br><span style="color: hsl(120, 100%, 40%);">+ </configOption></span><br><span style="color: hsl(120, 100%, 40%);">+ <configOption name="maxrdirects"></span><br><span style="color: hsl(120, 100%, 40%);">+ <synopsis>The maximum number of redirects to follow. See https://curl.se/libcurl/c/CURLOPT_MAXREDIRS.html for details</synopsis></span><br><span style="color: hsl(120, 100%, 40%);">+ </configOption></span><br><span style="color: hsl(120, 100%, 40%);">+ </configObject></span><br><span style="color: hsl(120, 100%, 40%);">+ </configFile></span><br><span style="color: hsl(120, 100%, 40%);">+ </configInfo></span><br><span style="color: hsl(120, 100%, 40%);">+***/</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> #include "asterisk.h"</span><br><span> </span><br><span> #include <curl/curl.h></span><br><span>@@ -44,6 +67,13 @@</span><br><span> </span><br><span> #define MAX_HEADER_LENGTH 1023</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+static int cache_curl_timeout = 180;</span><br><span style="color: hsl(120, 100%, 40%);">+static char *cache_curl_useragent = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+static int cache_followlocation = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+static int cache_maxredirs = 8;</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%);">+</span><br><span> /*! \brief Data passed to cURL callbacks */</span><br><span> struct curl_bucket_file_data {</span><br><span> /*! The \c ast_bucket_file object that caused the operation */</span><br><span>@@ -325,11 +355,11 @@</span><br><span> }</span><br><span> </span><br><span> curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);</span><br><span style="color: hsl(0, 100%, 40%);">- curl_easy_setopt(curl, CURLOPT_TIMEOUT, 180);</span><br><span style="color: hsl(120, 100%, 40%);">+ curl_easy_setopt(curl, CURLOPT_TIMEOUT, cache_curl_timeout);</span><br><span> curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, curl_header_callback);</span><br><span style="color: hsl(0, 100%, 40%);">- curl_easy_setopt(curl, CURLOPT_USERAGENT, AST_CURL_USER_AGENT);</span><br><span style="color: hsl(0, 100%, 40%);">- curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);</span><br><span style="color: hsl(0, 100%, 40%);">- curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 8);</span><br><span style="color: hsl(120, 100%, 40%);">+ curl_easy_setopt(curl, CURLOPT_USERAGENT, cache_curl_useragent != NULL ? cache_curl_useragent : AST_CURL_USER_AGENT);</span><br><span style="color: hsl(120, 100%, 40%);">+ curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, cache_followlocation);</span><br><span style="color: hsl(120, 100%, 40%);">+ curl_easy_setopt(curl, CURLOPT_MAXREDIRS, cache_maxredirs);</span><br><span> curl_easy_setopt(curl, CURLOPT_URL, ast_sorcery_object_get_id(cb_data->bucket_file));</span><br><span> curl_easy_setopt(curl, CURLOPT_HEADERDATA, cb_data);</span><br><span> </span><br><span>@@ -537,8 +567,63 @@</span><br><span> return 0;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+static int load_config(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ const char *cat;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_config *cfg;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_flags config_flags = { 0 };</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ cfg = ast_config_load("res_http_media_cache.conf", config_flags);</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Compat behavior. Ignore a missing file. */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (cfg == CONFIG_STATUS_FILEMISSING) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (cfg == CONFIG_STATUS_FILEINVALID) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Could not load res_http_media_cache.conf\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</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%);">+ cat = ast_category_browse(cfg, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ while (cat) {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_variable *var;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ var = ast_variable_browse(cfg, cat);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (strcasecmp(cat, "general") == 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ while (var) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (strcasecmp(var->name, "timeout_seconds") == 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ cache_curl_timeout = strtoul(var->value, NULL, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (strcasecmp(var->name, "useragent") == 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_free(cache_curl_useragent);</span><br><span style="color: hsl(120, 100%, 40%);">+ cache_curl_useragent = ast_strdup(var->value);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (strcasecmp(var->name, "followlocation") == 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ cache_followlocation = ast_true(var->value);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (strcasecmp(var->name, "maxredirects") == 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ cache_maxredirs = strtoul(var->value, NULL, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Unrecognized variable '%s' in category '%s'\n", var->name, cat);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_config_destroy(cfg);</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ var = var->next;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Unrecognized category '%s'\n", cat);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_config_destroy(cfg);</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ cat = ast_category_browse(cfg, cat);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_config_destroy(cfg);</span><br><span style="color: hsl(120, 100%, 40%);">+ return 1;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> static int load_module(void)</span><br><span> {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!load_config()) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_MODULE_LOAD_DECLINE;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> if (ast_bucket_scheme_register("http", &http_bucket_wizard, &http_bucket_file_wizard,</span><br><span> NULL, NULL)) {</span><br><span> ast_log(LOG_ERROR, "Failed to register Bucket HTTP wizard scheme implementation\n");</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/19658">change 19658</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/+/19658"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 20 </div>
<div style="display:none"> Gerrit-Change-Id: I2eb02ef44190e026716720419bcbdbcc8125777b </div>
<div style="display:none"> Gerrit-Change-Number: 19658 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Holger Hans Peter Freyther <automatic@freyther.de> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>