]> git.ipfire.org Git - thirdparty/apache/httpd.git/commitdiff
config: allow for environment variable substitution fallback to default value.
authorYann Ylavic <ylavic@apache.org>
Wed, 22 Apr 2020 16:30:58 +0000 (16:30 +0000)
committerYann Ylavic <ylavic@apache.org>
Wed, 22 Apr 2020 16:30:58 +0000 (16:30 +0000)
Make ap_resolve_env() handle the ${VAR?=default value} syntax, and update docs.

git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1876835 13f79535-47bb-0310-9956-ffa450edef68

CHANGES
docs/manual/configuring.xml
server/core.c

diff --git a/CHANGES b/CHANGES
index 53a0f8e5a030aef4ced832ac5ca2b2641422dfc9..bc24d2408f5fd52d3fcf3d6a7f7e3abc9f6918d6 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -1,6 +1,10 @@
                                                          -*- coding: utf-8 -*-
 Changes with Apache 2.5.1
 
+  *) config: Allow for environment variable substitution with default value,
+     for when the variable is not defined, using format ${VAR?=default value}.
+     [Yann Ylavic]
+
   *) mod_http2: Fixed regression that no longer set H2_STREAM_ID and H2_STREAM_TAG.
      PR64330 [Stefan Eissing]
 
index 672491b2a8198e1bfe4726808306b99f18ddef88..cfa52f361a5e0aac3e278fce84fcd201f026347a 100644 (file)
@@ -81,15 +81,23 @@ Server.</p>
     you may indent directives for clarity. Blank lines are also ignored.</p>
 
     <p>The values of variables defined with the <directive
-    module="core">Define</directive> of or shell environment variables can
-    be used in configuration file lines using the syntax <code>${VAR}</code>.
+    module="core">Define</directive> or of shell environment variables can
+    be used in configuration file lines using the syntax
+    <code>${VAR}</code>.<br />
     If "VAR" is the name of a valid variable, the value of that variable is
     substituted into that spot in the configuration file line, and processing
-    continues as if that text were found directly in the configuration file.
+    continues as if that text were found directly in the configuration
+    file.<br />
     Variables defined with <directive module="core">Define</directive> take
-    precedence over shell environment variables.
-    If the "VAR" variable is not found, the characters <code>${VAR}</code>
-    are left unchanged, and a warning is logged.
+    precedence over shell environment variables.<br />
+    If the "VAR" variable is not defined, the characters <code>${VAR}</code>
+    are left unchanged, and a warning is logged. If instead a default value
+    should be substituted, the conditional form
+    <code>${VAR?=some default value}</code> can be used. Note that a
+    <strong>defined</strong> empty variable will <strong>not</strong> be
+    substituted with the default value, and that an empty default value like
+    in <code>${VAR?=}</code> is a valid substitution (which produces an empty
+    value if "VAR" is not defined, but no warning).<br />
     Variable names may not contain colon ":" characters, to avoid clashes with
     <directive module="mod_rewrite">RewriteMap</directive>'s syntax.</p>
 
index adad3e1318fc87243aad0253814533c3c893bda6..f1015e05b17b2268fc6b2a260af2ba7a14e90e16 100644 (file)
@@ -1431,6 +1431,12 @@ AP_DECLARE(const char *) ap_resolve_env(apr_pool_t *p, const char * word)
         if (*s == '$') {
             if (s[1] == '{' && (e = ap_strchr_c(s+2, '}'))) {
                 char *name = apr_pstrmemdup(p, s+2, e-s-2);
+                char *dflt = ap_strstr(name, "?=");
+                if (dflt) {
+                    /* Default value for when var is not defined */
+                    *dflt = '\0';
+                    dflt += 2;
+                }
                 word = NULL;
                 if (server_config_defined_vars)
                     word = apr_table_get(server_config_defined_vars, name);
@@ -1441,6 +1447,11 @@ AP_DECLARE(const char *) ap_resolve_env(apr_pool_t *p, const char * word)
                     current->len = strlen(word);
                     outlen += current->len;
                 }
+                else if (dflt) {
+                    current->string = dflt;
+                    current->len = strlen(dflt);
+                    outlen += current->len;
+                }
                 else {
                     if (ap_strchr(name, ':') == 0)
                         ap_log_error(APLOG_MARK, APLOG_WARNING, 0, NULL, APLOGNO(00111)