-
Notifications
You must be signed in to change notification settings - Fork 351
/
content_length_between.go
55 lines (44 loc) · 1.43 KB
/
content_length_between.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
package content
import (
"github.com/zalando/skipper/predicates"
"github.com/zalando/skipper/routing"
"net/http"
)
type contentLengthBetweenSpec struct{}
type contentLengthBetweenPredicate struct {
min int64
max int64
}
// NewContentLengthBetween creates a predicate specification,
// whose instances match content length header value in range from min (inclusively) to max (exclusively).
// example: ContentLengthBetween(0, 5000)
func NewContentLengthBetween() routing.PredicateSpec { return &contentLengthBetweenSpec{} }
func (*contentLengthBetweenSpec) Name() string {
return predicates.ContentLengthBetweenName
}
// Create a predicate instance that evaluates content length header value range
func (*contentLengthBetweenSpec) Create(args []interface{}) (routing.Predicate, error) {
if len(args) != 2 {
return nil, predicates.ErrInvalidPredicateParameters
}
x, ok := args[0].(float64)
if !ok {
return nil, predicates.ErrInvalidPredicateParameters
}
minLength := int64(x)
x, ok = args[1].(float64)
if !ok {
return nil, predicates.ErrInvalidPredicateParameters
}
maxLength := int64(x)
if minLength < 0 || maxLength < 0 || minLength >= maxLength {
return nil, predicates.ErrInvalidPredicateParameters
}
return &contentLengthBetweenPredicate{
min: minLength,
max: maxLength,
}, nil
}
func (p *contentLengthBetweenPredicate) Match(req *http.Request) bool {
return req.ContentLength >= p.min && req.ContentLength < p.max
}