mirror of
https://github.com/caddyserver/caddy.git
synced 2025-06-23 17:11:27 +08:00
Condition upgrades (if, if_op) for rewrite, redir (#889)
* checkpoint * Added RequestMatcher interface. Extract 'if' condition into a RequestMatcher. * Added tests for IfMatcher * Minor refactors * Refactors * Use if_op * conform with new 0.9 beta function changes.
This commit is contained in:

committed by
Matt Holt

parent
0a3f68f0d7
commit
d9b6563d88
@ -45,6 +45,16 @@ type (
|
||||
// ServeHTTP returns a status code and an error. See Handler
|
||||
// documentation for more information.
|
||||
HandlerFunc func(http.ResponseWriter, *http.Request) (int, error)
|
||||
|
||||
// RequestMatcher checks to see if current request should be handled
|
||||
// by underlying handler.
|
||||
//
|
||||
// TODO The long term plan is to get all middleware implement this
|
||||
// interface and have validation done before requests are dispatched
|
||||
// to each middleware.
|
||||
RequestMatcher interface {
|
||||
Match(r *http.Request) bool
|
||||
}
|
||||
)
|
||||
|
||||
// ServeHTTP implements the Handler interface.
|
||||
@ -135,6 +145,24 @@ func (p Path) Matches(other string) bool {
|
||||
return strings.HasPrefix(strings.ToLower(string(p)), strings.ToLower(other))
|
||||
}
|
||||
|
||||
// MergeRequestMatchers merges multiple RequestMatchers into one.
|
||||
// This allows a middleware to use multiple RequestMatchers.
|
||||
func MergeRequestMatchers(matchers ...RequestMatcher) RequestMatcher {
|
||||
return requestMatchers(matchers)
|
||||
}
|
||||
|
||||
type requestMatchers []RequestMatcher
|
||||
|
||||
// Match satisfies RequestMatcher interface.
|
||||
func (m requestMatchers) Match(r *http.Request) bool {
|
||||
for _, matcher := range m {
|
||||
if !matcher.Match(r) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// currentTime, as it is defined here, returns time.Now().
|
||||
// It's defined as a variable for mocking time in tests.
|
||||
var currentTime = func() time.Time { return time.Now() }
|
||||
|
Reference in New Issue
Block a user