Make literal $ character work in set $location_path

This commit is contained in:
Bryan Shelton 2018-10-03 15:05:12 -07:00
parent be5b86fa8f
commit 3dc131bd57
3 changed files with 35 additions and 1 deletions

View file

@ -120,6 +120,7 @@ var (
}
return true
},
"escapeLocationPathVar": escapeLocationPathVar,
"shouldConfigureLuaRestyWAF": shouldConfigureLuaRestyWAF,
"buildLuaSharedDictionaries": buildLuaSharedDictionaries,
"buildLocation": buildLocation,
@ -161,6 +162,20 @@ var (
}
)
// escapeLocationPathVar will replace the $ character with ${literal_dollar}
// which is made to work via the following configuration in the http section of
// the template:
// geo $literal_dollar {
// default "$";
// }
func escapeLocationPathVar(input interface{}) string {
inputStr, ok := input.(string)
if !ok {
return ""
}
return strings.Replace(inputStr, `$`, `${literal_dollar}`, -1)
}
// formatIP will wrap IPv6 addresses in [] and return IPv4 addresses
// without modification. If the input cannot be parsed as an IP address
// it is returned without modification.

View file

@ -834,3 +834,16 @@ func TestBuildUpstreamName(t *testing.T) {
}
}
}
func TestEscapeLocationPathVar(t *testing.T) {
escapedPath := escapeLocationPathVar("/$")
expected := "/${literal_dollar}"
if escapedPath != expected {
t.Errorf("Expected %s but got %s", expected, escapedPath)
}
escapedPath = escapeLocationPathVar(false)
expected = ""
if escapedPath != expected {
t.Errorf("Expected %s but got %s", expected, escapedPath)
}
}