From: Yann Ylavic
Date: Wed, 22 Apr 2020 16:30:58 +0000 (+0000)
Subject: config: allow for environment variable substitution fallback to default value.
X-Git-Tag: 2.5.0-alpha2-ci-test-only~1496
X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=48aa76e1fce1a670b68a5dc288b2d8d7a96aa237;p=thirdparty%2Fapache%2Fhttpd.git
config: allow for environment variable substitution fallback to default value.
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
---
diff --git a/CHANGES b/CHANGES
index 53a0f8e5a03..bc24d2408f5 100644
--- 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]
diff --git a/docs/manual/configuring.xml b/docs/manual/configuring.xml
index 672491b2a81..cfa52f361a5 100644
--- a/docs/manual/configuring.xml
+++ b/docs/manual/configuring.xml
@@ -81,15 +81,23 @@ Server.
you may indent directives for clarity. Blank lines are also ignored.
The values of variables defined with the Define of or shell environment variables can
- be used in configuration file lines using the syntax ${VAR}.
+ module="core">Define or of shell environment variables can
+ be used in configuration file lines using the syntax
+ ${VAR}.
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.
Variables defined with Define take
- precedence over shell environment variables.
- If the "VAR" variable is not found, the characters ${VAR}
- are left unchanged, and a warning is logged.
+ precedence over shell environment variables.
+ If the "VAR" variable is not defined, the characters ${VAR}
+ are left unchanged, and a warning is logged. If instead a default value
+ should be substituted, the conditional form
+ ${VAR?=some default value} can be used. Note that a
+ defined empty variable will not be
+ substituted with the default value, and that an empty default value like
+ in ${VAR?=} is a valid substitution (which produces an empty
+ value if "VAR" is not defined, but no warning).
Variable names may not contain colon ":" characters, to avoid clashes with
RewriteMap's syntax.
diff --git a/server/core.c b/server/core.c
index adad3e1318f..f1015e05b17 100644
--- a/server/core.c
+++ b/server/core.c
@@ -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)