diff --git a/caskethttp/httpserver/replacer.go b/caskethttp/httpserver/replacer.go index 3c447418..fe66572f 100644 --- a/caskethttp/httpserver/replacer.go +++ b/caskethttp/httpserver/replacer.go @@ -296,6 +296,19 @@ func (r *replacer) getSubstitution(key string) string { name := key[2 : len(key)-1] return query.Get(name) } + // next check for environment variable + if key[1] == '$' { + name := key[2 : len(key)-1] + // check for a default value + if i := strings.Index(name, "="); i != -1 { + if value := os.Getenv(name[:i]); value != "" { + return value + } + return name[i+1:] + } + + return os.Getenv(name) + } // search default replacements in the end switch key { diff --git a/caskethttp/httpserver/replacer_test.go b/caskethttp/httpserver/replacer_test.go index d3e578a4..c4705237 100644 --- a/caskethttp/httpserver/replacer_test.go +++ b/caskethttp/httpserver/replacer_test.go @@ -75,6 +75,9 @@ func TestReplace(t *testing.T) { // add some headers after creating replacer request.Header.Set("CustomAdd", "casket") request.Header.Set("Cookie", "foo=bar; taste=delicious") + // add some environment variables + os.Setenv("TEST_ENV", "test") + os.Setenv("TEST_ENV_EMPTY", "") // add some response headers recordRequest.Header().Set("Custom", "CustomResponseHeader") @@ -108,6 +111,11 @@ func TestReplace(t *testing.T) { {"The Custom header is {>Custom}.", "The Custom header is foobarbaz."}, {"The CustomAdd header is {>CustomAdd}.", "The CustomAdd header is casket."}, {"The Custom response header is {Custom placeholder", "Bad {>Custom placeholder"}, {"The request is {request}.", "The request is POST /?foo=bar HTTP/1.1\\r\\nHost: localhost.local\\r\\n" + "Cookie: foo=bar; taste=delicious\\r\\nCustom: foobarbaz\\r\\nCustomadd: casket\\r\\n" + diff --git a/go.sum b/go.sum index 27649606..483788b6 100644 --- a/go.sum +++ b/go.sum @@ -783,6 +783,7 @@ github.com/tmpim/casket-plugins v0.0.4-0.20210411234607-8b023eeb664d h1:vn0yn5M2 github.com/tmpim/casket-plugins v0.0.4-0.20210411234607-8b023eeb664d/go.mod h1:RYBOo9WtK3LmyjGZVzEm1ptZV9uvPprzvz0V6i9qyuI= github.com/tmpim/certmagic v0.12.4 h1:k8MDMZT65fadsCu7fUONdksu5L7NONN6kgtX6dLd18w= github.com/tmpim/certmagic v0.12.4/go.mod h1:GrjIQ+gs5GW+jmqRkOnfoBjIiuk12VV4zbxoWBs8lQc= +github.com/tmpim/dnsproviders v0.4.2/go.mod h1:QNqmtAefbbf/2BYM2hhVHaFglA584r3ayGps32Gu/c8= github.com/tmpim/dnsproviders v0.4.3-0.20211231213508-66e13a82678d h1:5IkgvDcbqfqrb635H58scO8qwiCy9Qpq7rp7zEq9VJw= github.com/tmpim/dnsproviders v0.4.3-0.20211231213508-66e13a82678d/go.mod h1:QNqmtAefbbf/2BYM2hhVHaFglA584r3ayGps32Gu/c8= github.com/transip/gotransip v0.0.0-20190812104329-6d8d9179b66f/go.mod h1:i0f4R4o2HM0m3DZYQWsj6/MEowD57VzoH0v3d7igeFY=