2016-07-09 08:12:52 +08:00
|
|
|
package httpserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2017-05-02 23:43:43 +08:00
|
|
|
// Path represents a URI path. It should usually be
|
|
|
|
// set to the value of a request path.
|
2016-07-09 08:12:52 +08:00
|
|
|
type Path string
|
|
|
|
|
2017-05-02 23:43:43 +08:00
|
|
|
// Matches checks to see if base matches p. The correct
|
|
|
|
// usage of this method sets p as the request path, and
|
|
|
|
// base as a Caddyfile (user-defined) rule path.
|
2016-07-09 08:12:52 +08:00
|
|
|
//
|
|
|
|
// Path matching will probably not always be a direct
|
|
|
|
// comparison; this method assures that paths can be
|
|
|
|
// easily and consistently matched.
|
2017-05-02 23:43:43 +08:00
|
|
|
func (p Path) Matches(base string) bool {
|
|
|
|
if base == "/" {
|
|
|
|
return true
|
|
|
|
}
|
2016-07-09 08:12:52 +08:00
|
|
|
if CaseSensitivePath {
|
2017-05-02 23:43:43 +08:00
|
|
|
return strings.HasPrefix(string(p), base)
|
2016-07-09 08:12:52 +08:00
|
|
|
}
|
2017-05-02 23:43:43 +08:00
|
|
|
return strings.HasPrefix(strings.ToLower(string(p)), strings.ToLower(base))
|
2016-07-09 08:12:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// PathMatcher is a Path RequestMatcher.
|
|
|
|
type PathMatcher string
|
|
|
|
|
|
|
|
// Match satisfies RequestMatcher.
|
|
|
|
func (p PathMatcher) Match(r *http.Request) bool {
|
|
|
|
return Path(r.URL.Path).Matches(string(p))
|
|
|
|
}
|