/
methods.go
90 lines (70 loc) · 1.93 KB
/
methods.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
Package methods implements a custom predicate to match routes
based on the http method in request
# It supports multiple http methods, with case insensitive input
Examples:
// matches GET request
example1: Methods("GET") -> "http://example.org";
// matches GET or POST request
example1: Methods("GET", "post") -> "http://example.org";
*/
package methods
import (
"errors"
"fmt"
"net/http"
"strings"
"github.com/zalando/skipper/predicates"
"github.com/zalando/skipper/routing"
)
// Name is the predicate name
// Deprecated, use predicates.MethodsName instead
const Name = predicates.MethodsName
var ErrInvalidArgumentsCount = errors.New("at least one method should be specified")
var ErrInvalidArgumentType = errors.New("only string values are allowed")
type (
spec struct {
allowedMethods map[string]bool
}
predicate struct {
methods map[string]bool
}
)
// New creates a new Methods predicate specification
func New() routing.PredicateSpec {
return &spec{allowedMethods: map[string]bool{
http.MethodGet: true,
http.MethodHead: true,
http.MethodPost: true,
http.MethodPut: true,
http.MethodPatch: true,
http.MethodDelete: true,
http.MethodConnect: true,
http.MethodOptions: true,
http.MethodTrace: true,
}}
}
func (s *spec) Name() string { return predicates.MethodsName }
func (s *spec) Create(args []interface{}) (routing.Predicate, error) {
if len(args) == 0 {
return nil, ErrInvalidArgumentsCount
}
predicate := predicate{}
predicate.methods = map[string]bool{}
for _, arg := range args {
method, isString := arg.(string)
if !isString {
return nil, ErrInvalidArgumentType
}
method = strings.ToUpper(method)
if s.allowedMethods[method] {
predicate.methods[method] = true
} else {
return nil, fmt.Errorf("method: %s is not allowed", method)
}
}
return &predicate, nil
}
func (p *predicate) Match(r *http.Request) bool {
return p.methods[strings.ToUpper(r.Method)]
}