Matthew Holt
5 years ago
No known key found for this signature in database
GPG Key ID: 2A349DD577D586A5
2 changed files with
17 additions and
5 deletions
-
modules/caddyhttp/replacer.go
-
modules/caddyhttp/replacer_test.go
|
|
@ -146,14 +146,18 @@ func addHTTPVarsToReplacer(repl caddy.Replacer, req *http.Request, w http.Respon |
|
|
|
if err != nil { |
|
|
|
return "", false |
|
|
|
} |
|
|
|
hostLabels := strings.Split(req.Host, ".") |
|
|
|
reqHost, _, err := net.SplitHostPort(req.Host) |
|
|
|
if err != nil { |
|
|
|
reqHost = req.Host // OK; assume there was no port
|
|
|
|
} |
|
|
|
hostLabels := strings.Split(reqHost, ".") |
|
|
|
if idx < 0 { |
|
|
|
return "", false |
|
|
|
} |
|
|
|
if idx >= len(hostLabels) { |
|
|
|
if idx > len(hostLabels) { |
|
|
|
return "", true |
|
|
|
} |
|
|
|
return hostLabels[idx], true |
|
|
|
return hostLabels[len(hostLabels)-idx-1], true |
|
|
|
} |
|
|
|
|
|
|
|
// path parts
|
|
|
|
|
|
@ -34,8 +34,8 @@ func TestHTTPVarReplacement(t *testing.T) { |
|
|
|
addHTTPVarsToReplacer(repl, req, res) |
|
|
|
|
|
|
|
for i, tc := range []struct { |
|
|
|
input string |
|
|
|
expect string |
|
|
|
input string |
|
|
|
expect string |
|
|
|
}{ |
|
|
|
{ |
|
|
|
input: "{http.request.scheme}", |
|
|
@ -61,6 +61,14 @@ func TestHTTPVarReplacement(t *testing.T) { |
|
|
|
input: "{http.request.remote.port}", |
|
|
|
expect: "1234", |
|
|
|
}, |
|
|
|
{ |
|
|
|
input: "{http.request.host.labels.0}", |
|
|
|
expect: "com", |
|
|
|
}, |
|
|
|
{ |
|
|
|
input: "{http.request.host.labels.1}", |
|
|
|
expect: "example", |
|
|
|
}, |
|
|
|
} { |
|
|
|
actual := repl.ReplaceAll(tc.input, "<empty>") |
|
|
|
if actual != tc.expect { |
|
|
|