I would like to be able to specify a default/fallback value for when
an environment variable is not defined while loading the
configuration, like the ${SOME_VAR-default} form in bash/shells.

In docker (mainly docker-compose) where quite some settings are passed
to the container through env vars, this would be really useful for an
httpd.conf "template" with default values for undefined vars.

I first thought of "<IfEnv [!]blah>" or alike, but it quickly becomes
hard to read/follow, while e.g:
    LogLevel "${LOG_LEVEL-info}"
would be lovely :)

So would a patch like the attached be acceptable?
It uses '-' as separator but any other char would do (if the minus
sign is likely to break configuration where it shouldn't be
interpreted), though that would look less like a shell expansion..

Regards,
Yann.
Index: server/core.c
===================================================================
--- server/core.c	(revision 1876784)
+++ server/core.c	(working copy)
@@ -1431,6 +1431,10 @@ AP_DECLARE(const char *) ap_resolve_env(apr_pool_t
         if (*s == '$') {
             if (s[1] == '{' && (e = ap_strchr_c(s+2, '}'))) {
                 char *name = apr_pstrmemdup(p, s+2, e-s-2);
+                char *dflt = ap_strchr(name, '-');
+                if (dflt) {
+                    *dflt++ = '\0';
+                }
                 word = NULL;
                 if (server_config_defined_vars)
                     word = apr_table_get(server_config_defined_vars, name);
@@ -1441,6 +1445,11 @@ AP_DECLARE(const char *) ap_resolve_env(apr_pool_t
                     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)

Reply via email to