summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/matchers.go
diff options
context:
space:
mode:
authorMatthew Holt <mholt@users.noreply.github.com>2019-11-15 12:47:06 -0700
committerMatthew Holt <mholt@users.noreply.github.com>2019-11-15 12:47:06 -0700
commit0fc97211abd46098f5953fc6b152aa891060fca5 (patch)
treeaa3f4db1cbd0111bd8916543c60d0eda4334eb63 /modules/caddyhttp/matchers.go
parentad90b273dbb13bf45644f39f6c0be1248c943d9a (diff)
http: Make path matcher case-insensitive
Adds tests for both the path matcher and host matcher for case insensitivity. If case sensitivity is required for the path, a regexp matcher can be used instead. This is the v2 equivalent fix of PR #2882.
Diffstat (limited to 'modules/caddyhttp/matchers.go')
-rw-r--r--modules/caddyhttp/matchers.go19
1 files changed, 14 insertions, 5 deletions
diff --git a/modules/caddyhttp/matchers.go b/modules/caddyhttp/matchers.go
index 23b13dd..bddb214 100644
--- a/modules/caddyhttp/matchers.go
+++ b/modules/caddyhttp/matchers.go
@@ -33,10 +33,10 @@ import (
)
type (
- // MatchHost matches requests by the Host value.
+ // MatchHost matches requests by the Host value (case-insensitive).
MatchHost []string
- // MatchPath matches requests by the URI's path.
+ // MatchPath matches requests by the URI's path (case-insensitive).
MatchPath []string
// MatchPathRE matches requests by a regular expression on the URI's path.
@@ -154,20 +154,29 @@ func (MatchPath) CaddyModule() caddy.ModuleInfo {
}
}
+// Provision lower-cases the paths in m to ensure case-insensitive matching.
+func (m MatchPath) Provision(_ caddy.Context) error {
+ for i := range m {
+ m[i] = strings.ToLower(m[i])
+ }
+ return nil
+}
+
// Match returns true if r matches m.
func (m MatchPath) Match(r *http.Request) bool {
+ lowerPath := strings.ToLower(r.URL.Path)
for _, matchPath := range m {
// as a special case, if the first character is a
// wildcard, treat it as a quick suffix match
if strings.HasPrefix(matchPath, "*") {
- return strings.HasSuffix(r.URL.Path, matchPath[1:])
+ return strings.HasSuffix(lowerPath, matchPath[1:])
}
// can ignore error here because we can't handle it anyway
- matches, _ := filepath.Match(matchPath, r.URL.Path)
+ matches, _ := filepath.Match(matchPath, lowerPath)
if matches {
return true
}
- if strings.HasPrefix(r.URL.Path, matchPath) {
+ if strings.HasPrefix(lowerPath, matchPath) {
return true
}
}