forked from corestoreio/pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
varyby.go
122 lines (104 loc) · 3.33 KB
/
varyby.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2015-present, Cyrill @ Schumacher.fm and the CoreStore contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ratelimit
import (
"net/http"
"strings"
"github.com/corestoreio/pkg/net/request"
"github.com/corestoreio/pkg/util/bufferpool"
)
// VaryByer is called for each request to generate a key for the limiter. If it
// returns an empty string, all requests use an empty string key ;-). The rate
// limiter checks whether a particular key has exceeded a rate limit.
type VaryByer interface {
Key(*http.Request) string
}
type emptyVaryBy struct{}
func (emptyVaryBy) Key(_ *http.Request) string {
return ""
}
// VaryBy defines the criteria to use to group requests.
type VaryBy struct {
// Vary by the RemoteAddr as specified by the net/http.Request field.
RemoteAddr bool
// Vary by the HTTP Method as specified by the net/http.Request field.
Method bool
// Vary by the URL's Path as specified by the Path field of the net/http.Request
// URL field.
Path bool
// Vary by this list of header names, read from the net/http.Request Header field.
Headers []string
// Vary by this list of parameters, read from the net/http.Request FormValue method.
Params []string
// Vary by this list of cookie names, read from the net/http.Request Cookie method.
Cookies []string
// Use this separator string to concatenate the various criteria of the VaryBy struct.
// Defaults to a newline character if empty (\n).
Separator string
// SafeUnicode enables the usage of unicode safe strings to lower functions.
SafeUnicode bool
}
// Key returns the key for this request based on the criteria defined by the VaryBy struct.
func (vb *VaryBy) Key(r *http.Request) string {
buf := bufferpool.Get()
defer bufferpool.Put(buf)
if vb == nil {
return "" // Special case for no vary-by option
}
sep := vb.Separator
if sep == "" {
sep = "\n" // Separator defaults to newline
}
if vb.RemoteAddr {
_, _ = buf.WriteString(request.RealIP(r, request.IPForwardedTrust).String())
_, _ = buf.WriteString(sep)
}
if vb.Method {
_, _ = buf.WriteString(toLower(r.Method, vb.SafeUnicode))
_, _ = buf.WriteString(sep)
}
for _, h := range vb.Headers {
_, _ = buf.WriteString(toLower(r.Header.Get(h), vb.SafeUnicode))
_, _ = buf.WriteString(sep)
}
if vb.Path {
_, _ = buf.WriteString(r.URL.Path)
_, _ = buf.WriteString(sep)
}
for _, p := range vb.Params {
_, _ = buf.WriteString(r.FormValue(p))
_, _ = buf.WriteString(sep)
}
for _, c := range vb.Cookies {
if ck, err := r.Cookie(c); err == nil {
_, _ = buf.WriteString(ck.Value)
_, _ = buf.WriteString(sep)
}
}
return buf.String()
}
func toLower(s string, safeUnicode bool) string {
if safeUnicode {
return strings.ToLower(s)
}
b := make([]byte, len(s))
for i := range b {
c := s[i]
if c >= 'A' && c <= 'Z' {
c += 'a' - 'A'
}
b[i] = c
}
return string(b)
}