2015-12-22 17:19:22 -05:00
|
|
|
package rewrite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2015-12-23 03:36:00 -05:00
|
|
|
|
|
|
|
"github.com/mholt/caddy/middleware"
|
2015-12-22 17:19:22 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Operators
|
|
|
|
Is = "is"
|
|
|
|
Not = "not"
|
|
|
|
Has = "has"
|
2015-12-31 14:10:42 -05:00
|
|
|
NotHas = "not_has"
|
2015-12-22 17:19:22 -05:00
|
|
|
StartsWith = "starts_with"
|
|
|
|
EndsWith = "ends_with"
|
|
|
|
Match = "match"
|
2015-12-31 14:10:42 -05:00
|
|
|
NotMatch = "not_match"
|
2015-12-22 17:19:22 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func operatorError(operator string) error {
|
2015-12-23 03:36:00 -05:00
|
|
|
return fmt.Errorf("Invalid operator %v", operator)
|
2015-12-22 17:19:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func newReplacer(r *http.Request) middleware.Replacer {
|
|
|
|
return middleware.NewReplacer(r, nil, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// condition is a rewrite condition.
|
|
|
|
type condition func(string, string) bool
|
|
|
|
|
|
|
|
var conditions = map[string]condition{
|
|
|
|
Is: isFunc,
|
|
|
|
Not: notFunc,
|
|
|
|
Has: hasFunc,
|
2015-12-31 14:10:42 -05:00
|
|
|
NotHas: notHasFunc,
|
2015-12-22 17:19:22 -05:00
|
|
|
StartsWith: startsWithFunc,
|
|
|
|
EndsWith: endsWithFunc,
|
|
|
|
Match: matchFunc,
|
2015-12-31 14:10:42 -05:00
|
|
|
NotMatch: notMatchFunc,
|
2015-12-22 17:19:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// isFunc is condition for Is operator.
|
|
|
|
// It checks for equality.
|
|
|
|
func isFunc(a, b string) bool {
|
|
|
|
return a == b
|
|
|
|
}
|
|
|
|
|
|
|
|
// notFunc is condition for Not operator.
|
|
|
|
// It checks for inequality.
|
|
|
|
func notFunc(a, b string) bool {
|
|
|
|
return a != b
|
|
|
|
}
|
|
|
|
|
|
|
|
// hasFunc is condition for Has operator.
|
|
|
|
// It checks if b is a substring of a.
|
|
|
|
func hasFunc(a, b string) bool {
|
|
|
|
return strings.Contains(a, b)
|
|
|
|
}
|
|
|
|
|
2015-12-31 14:10:42 -05:00
|
|
|
// notHasFunc is condition for NotHas operator.
|
|
|
|
// It checks if b is not a substring of a.
|
|
|
|
func notHasFunc(a, b string) bool {
|
|
|
|
return !strings.Contains(a, b)
|
|
|
|
}
|
|
|
|
|
2015-12-22 17:19:22 -05:00
|
|
|
// startsWithFunc is condition for StartsWith operator.
|
|
|
|
// It checks if b is a prefix of a.
|
|
|
|
func startsWithFunc(a, b string) bool {
|
|
|
|
return strings.HasPrefix(a, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// endsWithFunc is condition for EndsWith operator.
|
|
|
|
// It checks if b is a suffix of a.
|
|
|
|
func endsWithFunc(a, b string) bool {
|
|
|
|
return strings.HasSuffix(a, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// matchFunc is condition for Match operator.
|
2015-12-23 06:11:11 -05:00
|
|
|
// It does regexp matching of a against pattern in b
|
2015-12-31 14:10:42 -05:00
|
|
|
// and returns if they match.
|
2015-12-22 17:19:22 -05:00
|
|
|
func matchFunc(a, b string) bool {
|
|
|
|
matched, _ := regexp.MatchString(b, a)
|
|
|
|
return matched
|
|
|
|
}
|
|
|
|
|
2015-12-31 14:10:42 -05:00
|
|
|
// notMatchFunc is condition for NotMatch operator.
|
|
|
|
// It does regexp matching of a against pattern in b
|
|
|
|
// and returns if they do not match.
|
|
|
|
func notMatchFunc(a, b string) bool {
|
|
|
|
matched, _ := regexp.MatchString(b, a)
|
|
|
|
return !matched
|
|
|
|
}
|
|
|
|
|
2015-12-22 17:19:22 -05:00
|
|
|
// If is statement for a rewrite condition.
|
|
|
|
type If struct {
|
|
|
|
A string
|
|
|
|
Operator string
|
|
|
|
B string
|
|
|
|
}
|
|
|
|
|
|
|
|
// True returns true if the condition is true and false otherwise.
|
|
|
|
// If r is not nil, it replaces placeholders before comparison.
|
|
|
|
func (i If) True(r *http.Request) bool {
|
|
|
|
if c, ok := conditions[i.Operator]; ok {
|
|
|
|
a, b := i.A, i.B
|
|
|
|
if r != nil {
|
|
|
|
replacer := newReplacer(r)
|
|
|
|
a = replacer.Replace(i.A)
|
|
|
|
b = replacer.Replace(i.B)
|
|
|
|
}
|
|
|
|
return c(a, b)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewIf creates a new If condition.
|
|
|
|
func NewIf(a, operator, b string) (If, error) {
|
|
|
|
if _, ok := conditions[operator]; !ok {
|
|
|
|
return If{}, operatorError(operator)
|
|
|
|
}
|
|
|
|
return If{
|
|
|
|
A: a,
|
|
|
|
Operator: operator,
|
|
|
|
B: b,
|
|
|
|
}, nil
|
|
|
|
}
|