-
Notifications
You must be signed in to change notification settings - Fork 342
/
any.go
49 lines (41 loc) · 1.08 KB
/
any.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package host
import (
"net/http"
"github.com/zalando/skipper/predicates"
"github.com/zalando/skipper/routing"
)
type anySpec struct{}
type anyPredicate struct {
hosts []string
}
// NewAny creates a predicate specification, whose instances match request host.
//
// The HostAny predicate requires one or more string hostnames and matches if request host
// exactly equals to any of the hostnames.
func NewAny() routing.PredicateSpec { return &anySpec{} }
func (*anySpec) Name() string {
return predicates.HostAnyName
}
// Create a predicate instance that always evaluates to true
func (*anySpec) Create(args []interface{}) (routing.Predicate, error) {
if len(args) == 0 {
return nil, predicates.ErrInvalidPredicateParameters
}
p := &anyPredicate{}
for _, arg := range args {
if host, ok := arg.(string); ok {
p.hosts = append(p.hosts, host)
} else {
return nil, predicates.ErrInvalidPredicateParameters
}
}
return p, nil
}
func (ap *anyPredicate) Match(r *http.Request) bool {
for _, host := range ap.hosts {
if host == r.Host {
return true
}
}
return false
}