@marcel91200 / caddy Mirror · read-only / commits / 14d3fd7d
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

http: path matcher supports exact matching with = prefix

Matthew Holt committed Nov 28, 2019 at 21:11 UTC 14d3fd7d0307e69c3c8995c42052b9103d1e2299
2 files changed +32 -3
modules/caddyhttp/matchers.go
+17 -3
@@ -166,16 +166,30 @@ func (m MatchPath) Provision(_ caddy.Context) error {
166 func (m MatchPath) Match(r *http.Request) bool {
167 lowerPath := strings.ToLower(r.URL.Path)
168 for _, matchPath := range m {
169 - // as a special case, if the first character is a
170 - // wildcard, treat it as a quick suffix match
169 + // special case: first character is equals sign,
170 + // treat it as an exact match
171 + if strings.HasPrefix(matchPath, "=") {
172 + if lowerPath == matchPath[1:] {
173 + return true
174 + }
175 + continue
176 + }
177 +
178 + // special case: first character is a wildcard,
179 + // treat it as a fast suffix match
180 if strings.HasPrefix(matchPath, "*") {
172 - return strings.HasSuffix(lowerPath, matchPath[1:])
181 + if strings.HasSuffix(lowerPath, matchPath[1:]) {
182 + return true
183 + }
184 + continue
185 }
186 +
187 // can ignore error here because we can't handle it anyway
188 matches, _ := filepath.Match(matchPath, lowerPath)
189 if matches {
190 return true
191 }
192 +
193 if strings.HasPrefix(lowerPath, matchPath) {
194 return true
195 }
modules/caddyhttp/matchers_test.go
+15
@@ -212,6 +212,21 @@ func TestPathMatcher(t *testing.T) {
212 input: "/foo/bar/bam",
213 expect: false,
214 },
215 + {
216 + match: MatchPath{"=/foo"},
217 + input: "/foo",
218 + expect: true,
219 + },
220 + {
221 + match: MatchPath{"=/foo"},
222 + input: "/foo/bar",
223 + expect: false,
224 + },
225 + {
226 + match: MatchPath{"=/foo"},
227 + input: "/FOO",
228 + expect: true,
229 + },
230 {
231 match: MatchPath{"/foo"},
232 input: "/FOO",