mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-23 22:27:38 -05:00
Merge pull request #907 from abiosoft/fastcgi_env_placeholder
Support for placeholders in fastcgi env vars.
This commit is contained in:
commit
6db4771aa8
4 changed files with 67 additions and 21 deletions
|
@ -254,9 +254,11 @@ func (h Handler) buildEnv(r *http.Request, rule Rule, fpath string) (map[string]
|
||||||
env["HTTPS"] = "on"
|
env["HTTPS"] = "on"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
replacer := httpserver.NewReplacer(r, nil, "")
|
||||||
// Add env variables from config
|
// Add env variables from config
|
||||||
for _, envVar := range rule.EnvVars {
|
for _, envVar := range rule.EnvVars {
|
||||||
env[envVar[0]] = envVar[1]
|
// replace request placeholders in environment variables
|
||||||
|
env[envVar[0]] = replacer.Replace(envVar[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add all HTTP headers to env variables
|
// Add all HTTP headers to env variables
|
||||||
|
|
|
@ -123,38 +123,77 @@ func TestBuildEnv(t *testing.T) {
|
||||||
t.Error("Unexpected error:", err.Error())
|
t.Error("Unexpected error:", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
r := http.Request{
|
var newReq = func() *http.Request {
|
||||||
Method: "GET",
|
return &http.Request{
|
||||||
URL: url,
|
Method: "GET",
|
||||||
Proto: "HTTP/1.1",
|
URL: url,
|
||||||
ProtoMajor: 1,
|
Proto: "HTTP/1.1",
|
||||||
ProtoMinor: 1,
|
ProtoMajor: 1,
|
||||||
Host: "localhost:2015",
|
ProtoMinor: 1,
|
||||||
RemoteAddr: "[2b02:1810:4f2d:9400:70ab:f822:be8a:9093]:51688",
|
Host: "localhost:2015",
|
||||||
RequestURI: "/fgci_test.php",
|
RemoteAddr: "[2b02:1810:4f2d:9400:70ab:f822:be8a:9093]:51688",
|
||||||
|
RequestURI: "/fgci_test.php",
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fpath := "/fgci_test.php"
|
fpath := "/fgci_test.php"
|
||||||
|
|
||||||
var envExpected = map[string]string{
|
var newEnv = func() map[string]string {
|
||||||
"REMOTE_ADDR": "2b02:1810:4f2d:9400:70ab:f822:be8a:9093",
|
return map[string]string{
|
||||||
"REMOTE_PORT": "51688",
|
"REMOTE_ADDR": "2b02:1810:4f2d:9400:70ab:f822:be8a:9093",
|
||||||
"SERVER_PROTOCOL": "HTTP/1.1",
|
"REMOTE_PORT": "51688",
|
||||||
"QUERY_STRING": "test=blabla",
|
"SERVER_PROTOCOL": "HTTP/1.1",
|
||||||
"REQUEST_METHOD": "GET",
|
"QUERY_STRING": "test=blabla",
|
||||||
"HTTP_HOST": "localhost:2015",
|
"REQUEST_METHOD": "GET",
|
||||||
|
"HTTP_HOST": "localhost:2015",
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// request
|
||||||
|
var r *http.Request
|
||||||
|
|
||||||
|
// expected environment variables
|
||||||
|
var envExpected map[string]string
|
||||||
|
|
||||||
// 1. Test for full canonical IPv6 address
|
// 1. Test for full canonical IPv6 address
|
||||||
testBuildEnv(&r, rule, fpath, envExpected)
|
r = newReq()
|
||||||
|
testBuildEnv(r, rule, fpath, envExpected)
|
||||||
|
|
||||||
// 2. Test for shorthand notation of IPv6 address
|
// 2. Test for shorthand notation of IPv6 address
|
||||||
|
r = newReq()
|
||||||
r.RemoteAddr = "[::1]:51688"
|
r.RemoteAddr = "[::1]:51688"
|
||||||
|
envExpected = newEnv()
|
||||||
envExpected["REMOTE_ADDR"] = "::1"
|
envExpected["REMOTE_ADDR"] = "::1"
|
||||||
testBuildEnv(&r, rule, fpath, envExpected)
|
testBuildEnv(r, rule, fpath, envExpected)
|
||||||
|
|
||||||
// 3. Test for IPv4 address
|
// 3. Test for IPv4 address
|
||||||
|
r = newReq()
|
||||||
r.RemoteAddr = "192.168.0.10:51688"
|
r.RemoteAddr = "192.168.0.10:51688"
|
||||||
|
envExpected = newEnv()
|
||||||
envExpected["REMOTE_ADDR"] = "192.168.0.10"
|
envExpected["REMOTE_ADDR"] = "192.168.0.10"
|
||||||
testBuildEnv(&r, rule, fpath, envExpected)
|
testBuildEnv(r, rule, fpath, envExpected)
|
||||||
|
|
||||||
|
// 4. Test for environment variable
|
||||||
|
r = newReq()
|
||||||
|
rule.EnvVars = [][2]string{
|
||||||
|
{"HTTP_HOST", "localhost:2016"},
|
||||||
|
{"REQUEST_METHOD", "POST"},
|
||||||
|
}
|
||||||
|
envExpected = newEnv()
|
||||||
|
envExpected["HTTP_HOST"] = "localhost:2016"
|
||||||
|
envExpected["REQUEST_METHOD"] = "POST"
|
||||||
|
testBuildEnv(r, rule, fpath, envExpected)
|
||||||
|
|
||||||
|
// 5. Test for environment variable placeholders
|
||||||
|
r = newReq()
|
||||||
|
rule.EnvVars = [][2]string{
|
||||||
|
{"HTTP_HOST", "{host}"},
|
||||||
|
{"CUSTOM_URI", "custom_uri{uri}"},
|
||||||
|
{"CUSTOM_QUERY", "custom=true&{query}"},
|
||||||
|
}
|
||||||
|
envExpected = newEnv()
|
||||||
|
envExpected["HTTP_HOST"] = "localhost:2015"
|
||||||
|
envExpected["CUSTOM_URI"] = "custom_uri/fgci_test.php?test=blabla"
|
||||||
|
envExpected["CUSTOM_QUERY"] = "custom=true&test=blabla"
|
||||||
|
testBuildEnv(r, rule, fpath, envExpected)
|
||||||
}
|
}
|
||||||
|
|
|
@ -127,6 +127,11 @@ func NewReplacer(r *http.Request, rr *ResponseRecorder, emptyValue string) Repla
|
||||||
// Replace performs a replacement of values on s and returns
|
// Replace performs a replacement of values on s and returns
|
||||||
// the string with the replaced values.
|
// the string with the replaced values.
|
||||||
func (r *replacer) Replace(s string) string {
|
func (r *replacer) Replace(s string) string {
|
||||||
|
// Do not attempt replacements if no placeholder is found.
|
||||||
|
if !strings.ContainsAny(s, "{}") {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
// Make response placeholders now
|
// Make response placeholders now
|
||||||
if r.responseRecorder != nil {
|
if r.responseRecorder != nil {
|
||||||
r.replacements["{status}"] = strconv.Itoa(r.responseRecorder.status)
|
r.replacements["{status}"] = strconv.Itoa(r.responseRecorder.status)
|
||||||
|
|
|
@ -32,7 +32,7 @@ func TestNewReplacer(t *testing.T) {
|
||||||
if got, want := v.replacements["{status}"], ""; got != want {
|
if got, want := v.replacements["{status}"], ""; got != want {
|
||||||
t.Errorf("Expected status to NOT be set before Replace() is called; was: %s", got)
|
t.Errorf("Expected status to NOT be set before Replace() is called; was: %s", got)
|
||||||
}
|
}
|
||||||
rep.Replace("foobar")
|
rep.Replace("{foobar}")
|
||||||
if got, want := v.replacements["{status}"], "200"; got != want {
|
if got, want := v.replacements["{status}"], "200"; got != want {
|
||||||
t.Errorf("Expected status to be %s, was: %s", want, got)
|
t.Errorf("Expected status to be %s, was: %s", want, got)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue