forked from zalando/skipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routing.go
309 lines (258 loc) · 7.59 KB
/
routing.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package routing
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"sync/atomic"
"time"
"github.com/zalando/skipper/eskip"
"github.com/zalando/skipper/filters"
"github.com/zalando/skipper/logging"
)
const (
// PathName represents the name of builtin path predicate.
// (See more details about the Path and PathSubtree predicates
// at https://godoc.org/github.com/zalando/skipper/eskip)
PathName = "Path"
// PathSubtreeName represents the name of the builtin path subtree predicate.
// (See more details about the Path and PathSubtree predicates
// at https://godoc.org/github.com/zalando/skipper/eskip)
PathSubtreeName = "PathSubtree"
routesTimestampName = "X-Timestamp"
routesCountName = "X-Count"
defaultRouteListingLimit = 1024
)
// MatchingOptions controls route matching.
type MatchingOptions uint
const (
// MatchingOptionsNone indicates that all options are default.
MatchingOptionsNone MatchingOptions = 0
// IgnoreTrailingSlash indicates that trailing slashes in paths are ignored.
IgnoreTrailingSlash MatchingOptions = 1 << iota
)
func (o MatchingOptions) ignoreTrailingSlash() bool {
return o&IgnoreTrailingSlash > 0
}
// DataClient instances provide data sources for
// route definitions.
type DataClient interface {
LoadAll() ([]*eskip.Route, error)
LoadUpdate() ([]*eskip.Route, []string, error)
}
// Predicate instances are used as custom user defined route
// matching predicates.
type Predicate interface {
// Returns true if the request matches the predicate.
Match(*http.Request) bool
}
// PredicateSpec instances are used to create custom predicates
// (of type Predicate) with concrete arguments during the
// construction of the routing tree.
type PredicateSpec interface {
// Name of the predicate as used in the route definitions.
Name() string
// Creates a predicate instance with concrete arguments.
Create([]interface{}) (Predicate, error)
}
// Options for initialization for routing.
type Options struct {
// Registry containing the available filter
// specifications that are used during processing
// the filter chains in the route definitions.
FilterRegistry filters.Registry
// Matching options are flags that control the
// route matching.
MatchingOptions MatchingOptions
// The timeout between requests to the data
// clients for route definition updates.
PollTimeout time.Duration
// The set of different data clients where the
// route definitions are read from.
DataClients []DataClient
// Specifications of custom, user defined predicates.
Predicates []PredicateSpec
// Performance tuning option.
//
// When zero, the newly constructed routing
// tree will take effect on the next routing
// query after every update from the data
// clients. In case of higher values, the
// routing queries have priority over the
// update channel, but the next routing tree
// takes effect only a few requests later.
//
// (Currently disabled and used with hard wired
// 0, until the performance benefit is verified
// by benchmarks.)
UpdateBuffer int
// Set a custom logger if necessary.
Log logging.Logger
// SuppressLogs indicates whether to log only a summary of the route changes.
SuppressLogs bool
}
// RouteFilter contains extensions to generic filter
// interface, serving mainly logging/monitoring
// purpose.
type RouteFilter struct {
filters.Filter
Name string
Index int
}
// Route object with preprocessed filter instances.
type Route struct {
// Fields from the static route definition.
eskip.Route
// path predicate matching a subtree
path string
// path predicate matching a subtree
pathSubtree string
// The backend scheme and host.
Scheme, Host string
// The preprocessed custom predicate instances.
Predicates []Predicate
// The preprocessed filter instances.
Filters []*RouteFilter
}
// Routing ('router') instance providing live
// updatable request matching.
type Routing struct {
routeTable atomic.Value // of struct routeTable
log logging.Logger
quit chan struct{}
}
// New initializes a routing instance, and starts listening for route
// definition updates.
func New(o Options) *Routing {
if o.Log == nil {
o.Log = &logging.DefaultLog{}
}
r := &Routing{log: o.Log, quit: make(chan struct{})}
initialMatcher, _ := newMatcher(nil, MatchingOptionsNone)
rt := &routeTable{
m: initialMatcher,
created: time.Now().UTC(),
}
r.routeTable.Store(rt)
r.startReceivingUpdates(o)
return r
}
// ServeHTTP renders the list of current routes.
func (r *Routing) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if req.Method != "GET" && req.Method != "HEAD" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
rt := r.routeTable.Load().(*routeTable)
req.ParseForm()
createdUnix := strconv.FormatInt(rt.created.Unix(), 10)
ts := req.Form.Get("timestamp")
if ts != "" && createdUnix != ts {
http.Error(w, "invalid timestamp", http.StatusBadRequest)
return
}
if req.Method == "HEAD" {
w.Header().Set(routesTimestampName, createdUnix)
w.Header().Set(routesCountName, strconv.Itoa(len(rt.validRoutes)))
if strings.Contains(req.Header.Get("Accept"), "application/json") {
w.Header().Set("Content-Type", "application/json")
} else {
w.Header().Set("Content-Type", "text/plain")
}
return
}
offset, err := extractParam(req, "offset", 0)
if err != nil {
http.Error(w, "invalid offset", http.StatusBadRequest)
return
}
limit, err := extractParam(req, "limit", defaultRouteListingLimit)
if err != nil {
http.Error(w, "invalid limit", http.StatusBadRequest)
return
}
w.Header().Set(routesTimestampName, createdUnix)
w.Header().Set(routesCountName, strconv.Itoa(len(rt.validRoutes)))
routes := slice(rt.validRoutes, offset, limit)
if strings.Contains(req.Header.Get("Accept"), "application/json") {
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(routes); err != nil {
http.Error(
w,
http.StatusText(http.StatusInternalServerError),
http.StatusInternalServerError,
)
}
return
}
w.Header().Set("Content-Type", "text/plain")
eskip.Fprint(w, extractPretty(req), routes...)
}
func (r *Routing) startReceivingUpdates(o Options) {
c := make(chan *routeTable)
go receiveRouteMatcher(o, c, r.quit)
go func() {
for {
select {
case rt := <-c:
r.routeTable.Store(rt)
r.log.Info("route settings applied")
case <-r.quit:
return
}
}
}()
}
// Route matches a request in the current routing tree.
//
// If the request matches a route, returns the route and a map of
// parameters constructed from the wildcard parameters in the path
// condition if any. If there is no match, it returns nil.
func (r *Routing) Route(req *http.Request) (*Route, map[string]string) {
rt := r.routeTable.Load().(*routeTable)
return rt.m.match(req)
}
// Close closes routing, stops receiving routes.
func (r *Routing) Close() {
close(r.quit)
}
func slice(r []*eskip.Route, offset int, limit int) []*eskip.Route {
if offset > len(r) {
offset = len(r)
}
end := offset + limit
if end > len(r) {
end = len(r)
}
result := r[offset:end]
if result == nil {
return []*eskip.Route{}
}
return result
}
func extractParam(r *http.Request, key string, defaultValue int) (int, error) {
param := r.Form.Get(key)
if param == "" {
return defaultValue, nil
}
val, err := strconv.Atoi(param)
if err != nil {
return 0, err
}
if val < 0 {
return 0, fmt.Errorf("invalid value `%d` for `%s`", val, key)
}
return val, nil
}
func extractPretty(r *http.Request) bool {
vals, ok := r.Form["nopretty"]
if !ok || len(vals) == 0 {
return true
}
val := vals[0]
if val == "0" || val == "false" {
return true
}
return false
}