summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/fileserver/matcher.go
diff options
context:
space:
mode:
authorMatthew Holt <mholt@users.noreply.github.com>2020-07-31 13:55:01 -0600
committerMatthew Holt <mholt@users.noreply.github.com>2020-07-31 13:55:01 -0600
commit3860b235d074209c495d34a3966fc7fb2d5015a5 (patch)
tree444621ef8c11e2445a81e3d01b09285e476862fb /modules/caddyhttp/fileserver/matcher.go
parent6f73a358f43013a43c55d57f0c7726a6277f62b6 (diff)
fileserver: Don't assume len(str) == len(ToLower(str)) (fix #3623)
We can't use a positional index on an original string that we got from its lower-cased equivalent. Implement our own IndexFold() function b/c the std lib does not have one.
Diffstat (limited to 'modules/caddyhttp/fileserver/matcher.go')
-rw-r--r--modules/caddyhttp/fileserver/matcher.go20
1 files changed, 17 insertions, 3 deletions
diff --git a/modules/caddyhttp/fileserver/matcher.go b/modules/caddyhttp/fileserver/matcher.go
index 9144ca4..1844421 100644
--- a/modules/caddyhttp/fileserver/matcher.go
+++ b/modules/caddyhttp/fileserver/matcher.go
@@ -117,11 +117,13 @@ func (m *MatchFile) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
return d.ArgErr()
}
m.TryPolicy = d.Val()
- case "split":
+ case "split_path":
m.SplitPath = d.RemainingArgs()
if len(m.SplitPath) == 0 {
return d.ArgErr()
}
+ default:
+ return d.Errf("unrecognized subdirective: %s", d.Val())
}
}
}
@@ -279,9 +281,8 @@ func strictFileExists(file string) bool {
// in the split value. Returns the path as-is if the
// path cannot be split.
func (m MatchFile) firstSplit(path string) string {
- lowerPath := strings.ToLower(path)
for _, split := range m.SplitPath {
- if idx := strings.Index(lowerPath, strings.ToLower(split)); idx > -1 {
+ if idx := indexFold(path, split); idx > -1 {
pos := idx + len(split)
// skip the split if it's not the final part of the filename
if pos != len(path) && !strings.HasPrefix(path[pos:], "/") {
@@ -293,6 +294,19 @@ func (m MatchFile) firstSplit(path string) string {
return path
}
+// There is no strings.IndexFold() function like there is strings.EqualFold(),
+// but we can use strings.EqualFold() to build our own case-insensitive
+// substring search (as of Go 1.14).
+func indexFold(haystack, needle string) int {
+ nlen := len(needle)
+ for i := 0; i+nlen < len(haystack); i++ {
+ if strings.EqualFold(haystack[i:i+nlen], needle) {
+ return i
+ }
+ }
+ return -1
+}
+
const (
tryPolicyFirstExist = "first_exist"
tryPolicyLargestSize = "largest_size"