-
Notifications
You must be signed in to change notification settings - Fork 11
/
mapper.go
185 lines (153 loc) · 3.91 KB
/
mapper.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package route
import (
"net/http"
"strings"
)
const (
pathSep = '/'
domainSep = '.'
headerSep = '/'
methodSep = ' '
)
// requestMapper maps the request to string e.g. maps request to its hostname, or request to header
type requestMapper interface {
// separator returns the separator that makes sense for this request, e.g. / for urls or . for domains
separator() byte
// equals returns the equivalent mapper if the two mappers are equivalent, e.g. map to the same sequence
// mappers are also equivalent if one mapper is subset of another, e.g. combined mapper (host, path) is equivalent of (host) mapper
equivalent(requestMapper) requestMapper
// mapRequest maps request to string, e.g. request to it's URL path
mapRequest(r *http.Request) string
// newIter returns the iterator instead of string for stream matchers
newIter(r *http.Request) *charIter
}
type methodMapper struct{}
func (m *methodMapper) separator() byte {
return methodSep
}
func (m *methodMapper) equivalent(o requestMapper) requestMapper {
_, ok := o.(*methodMapper)
if ok {
return m
}
return nil
}
func (m *methodMapper) mapRequest(r *http.Request) string {
return r.Method
}
func (m *methodMapper) newIter(r *http.Request) *charIter {
return newIter([]string{m.mapRequest(r)}, []byte{m.separator()})
}
type pathMapper struct{}
func (p *pathMapper) separator() byte {
return pathSep
}
func (p *pathMapper) equivalent(o requestMapper) requestMapper {
_, ok := o.(*pathMapper)
if ok {
return p
}
return nil
}
func (p *pathMapper) newIter(r *http.Request) *charIter {
return newIter([]string{p.mapRequest(r)}, []byte{p.separator()})
}
func (p *pathMapper) mapRequest(r *http.Request) string {
return rawPath(r)
}
type hostMapper struct{}
func (h *hostMapper) equivalent(o requestMapper) requestMapper {
_, ok := o.(*hostMapper)
if ok {
return h
}
return nil
}
func (h *hostMapper) separator() byte {
return domainSep
}
func (h *hostMapper) mapRequest(r *http.Request) string {
return strings.Split(strings.ToLower(r.Host), ":")[0]
}
func (h *hostMapper) newIter(r *http.Request) *charIter {
return newIter([]string{h.mapRequest(r)}, []byte{h.separator()})
}
type headerMapper struct {
header string
}
func (h *headerMapper) equivalent(o requestMapper) requestMapper {
hm, ok := o.(*headerMapper)
if ok && hm.header == h.header {
return h
}
return nil
}
func (h *headerMapper) separator() byte {
return headerSep
}
func (h *headerMapper) mapRequest(r *http.Request) string {
return r.Header.Get(h.header)
}
func (h *headerMapper) newIter(r *http.Request) *charIter {
return newIter([]string{h.mapRequest(r)}, []byte{h.separator()})
}
type seqMapper struct {
seq []requestMapper
}
func newSeqMapper(seq ...requestMapper) *seqMapper {
var out []requestMapper
for _, s := range seq {
switch m := s.(type) {
case *seqMapper:
out = append(out, m.seq...)
default:
out = append(out, s)
}
}
return &seqMapper{seq: out}
}
func (s *seqMapper) newIter(r *http.Request) *charIter {
out := make([]string, len(s.seq))
for i := range s.seq {
out[i] = s.seq[i].mapRequest(r)
}
seps := make([]byte, len(s.seq))
for i := range s.seq {
seps[i] = s.seq[i].separator()
}
return newIter(out, seps)
}
func (s *seqMapper) mapRequest(r *http.Request) string {
out := make([]string, len(s.seq))
for i := range s.seq {
out[i] = s.seq[i].mapRequest(r)
}
return strings.Join(out, "")
}
func (s *seqMapper) separator() byte {
return s.seq[0].separator()
}
func (s *seqMapper) equivalent(o requestMapper) requestMapper {
so, ok := o.(*seqMapper)
if !ok {
return nil
}
var longer, shorter *seqMapper
if len(s.seq) > len(so.seq) {
longer = s
shorter = so
} else {
longer = so
shorter = s
}
for i := range longer.seq {
// shorter is subset of longer, return longer sequence mapper
if i >= len(shorter.seq)-1 {
return longer
}
if longer.seq[i].equivalent(shorter.seq[i]) == nil {
return nil
}
}
return longer
}