-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathmw_url_rewrite.go
491 lines (420 loc) · 11.5 KB
/
mw_url_rewrite.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/textproto"
"net/url"
"reflect"
"strconv"
"strings"
"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/regexp"
"github.com/TykTechnologies/tyk/user"
)
const (
metaLabel = "$tyk_meta."
contextLabel = "$tyk_context."
)
var dollarMatch = regexp.MustCompile(`\$\d+`)
var contextMatch = regexp.MustCompile(`\$tyk_context.([A-Za-z0-9_\-\.]+)`)
var metaMatch = regexp.MustCompile(`\$tyk_meta.([A-Za-z0-9_\-\.]+)`)
func urlRewrite(meta *apidef.URLRewriteMeta, r *http.Request) (string, error) {
path := r.URL.String()
log.Debug("Inbound path: ", path)
newpath := path
if meta.MatchRegexp == nil {
var err error
meta.MatchRegexp, err = regexp.Compile(meta.MatchPattern)
if err != nil {
return path, fmt.Errorf("URLRewrite regexp error %s", meta.MatchPattern)
}
}
// Check triggers
rewriteToPath := meta.RewriteTo
if len(meta.Triggers) > 0 {
// This feature uses context, we must force it if it doesn't exist
contextData := ctxGetData(r)
if contextData == nil {
contextDataObject := make(map[string]interface{})
ctxSetData(r, contextDataObject)
}
for tn, triggerOpts := range meta.Triggers {
checkAny := false
setCount := 0
if triggerOpts.On == apidef.Any {
checkAny = true
}
// Check headers
if len(triggerOpts.Options.HeaderMatches) > 0 {
if checkHeaderTrigger(r, triggerOpts.Options.HeaderMatches, checkAny, tn) {
setCount += 1
if checkAny {
rewriteToPath = triggerOpts.RewriteTo
break
}
}
}
// Check query string
if len(triggerOpts.Options.QueryValMatches) > 0 {
if checkQueryString(r, triggerOpts.Options.QueryValMatches, checkAny, tn) {
setCount += 1
if checkAny {
rewriteToPath = triggerOpts.RewriteTo
break
}
}
}
// Check path parts
if len(triggerOpts.Options.PathPartMatches) > 0 {
if checkPathParts(r, triggerOpts.Options.PathPartMatches, checkAny, tn) {
setCount += 1
if checkAny {
rewriteToPath = triggerOpts.RewriteTo
break
}
}
}
// Check session meta
if session := ctxGetSession(r); session != nil {
if len(triggerOpts.Options.SessionMetaMatches) > 0 {
if checkSessionTrigger(r, session, triggerOpts.Options.SessionMetaMatches, checkAny, tn) {
setCount += 1
if checkAny {
rewriteToPath = triggerOpts.RewriteTo
break
}
}
}
}
// Check payload
if triggerOpts.Options.PayloadMatches.MatchPattern != "" {
if checkPayload(r, triggerOpts.Options.PayloadMatches, tn) {
setCount += 1
if checkAny {
rewriteToPath = triggerOpts.RewriteTo
break
}
}
}
if !checkAny {
// Set total count:
total := 0
if len(triggerOpts.Options.HeaderMatches) > 0 {
total += 1
}
if len(triggerOpts.Options.QueryValMatches) > 0 {
total += 1
}
if len(triggerOpts.Options.PathPartMatches) > 0 {
total += 1
}
if len(triggerOpts.Options.SessionMetaMatches) > 0 {
total += 1
}
if triggerOpts.Options.PayloadMatches.MatchPattern != "" {
total += 1
}
if total == setCount {
rewriteToPath = triggerOpts.RewriteTo
}
}
}
}
matchGroups := meta.MatchRegexp.FindAllStringSubmatch(path, -1)
// Make sure it matches the string
log.Debug("Rewriter checking matches, len is: ", len(matchGroups))
if len(matchGroups) > 0 {
newpath = rewriteToPath
// get the indices for the replacements:
replaceGroups := dollarMatch.FindAllStringSubmatch(rewriteToPath, -1)
log.Debug(matchGroups)
log.Debug(replaceGroups)
groupReplace := make(map[string]string)
for mI, replacementVal := range matchGroups[0] {
indexVal := "$" + strconv.Itoa(mI)
groupReplace[indexVal] = replacementVal
}
for _, v := range replaceGroups {
newpath = strings.Replace(newpath, v[0], groupReplace[v[0]], -1)
}
log.Debug("URL Re-written from: ", path)
log.Debug("URL Re-written to: ", newpath)
// put url_rewrite path to context to be used in ResponseTransformMiddleware
ctxSetUrlRewritePath(r, meta.Path)
}
newpath = replaceTykVariables(r, newpath, true)
return newpath, nil
}
func replaceTykVariables(r *http.Request, in string, escape bool) string {
if strings.Contains(in, contextLabel) {
contextData := ctxGetData(r)
replaceGroups := contextMatch.FindAllStringSubmatch(in, -1)
for _, v := range replaceGroups {
contextKey := strings.Replace(v[0], "$tyk_context.", "", 1)
if val, ok := contextData[contextKey]; ok {
valStr := valToStr(val)
// If contains url with domain
if escape && !strings.HasPrefix(valStr, "http") {
valStr = url.QueryEscape(valStr)
}
in = strings.Replace(in, v[0], valStr, -1)
} else {
in = ""
}
}
}
if strings.Contains(in, metaLabel) {
// Meta data from the token
session := ctxGetSession(r)
if session == nil {
return in
}
replaceGroups := metaMatch.FindAllStringSubmatch(in, -1)
for _, v := range replaceGroups {
contextKey := strings.Replace(v[0], "$tyk_meta.", "", 1)
val, ok := session.MetaData[contextKey]
if ok {
valStr := valToStr(val)
// If contains url with domain
if escape && !strings.HasPrefix(valStr, "http") {
valStr = url.QueryEscape(valStr)
}
in = strings.Replace(in, v[0], valStr, -1)
} else {
in = ""
}
}
}
return in
}
func valToStr(v interface{}) string {
s := ""
switch x := v.(type) {
case string:
s = x
case float64:
s = strconv.FormatFloat(x, 'f', -1, 32)
case int64:
s = strconv.FormatInt(x, 10)
case []string:
s = strings.Join(x, ",")
// Remove empty start
s = strings.TrimPrefix(s, ",")
case url.Values:
i := 0
for key, v := range x {
s += key + ":" + strings.Join(v, ",")
if i < len(x)-1 {
s += ";"
}
i++
}
case []interface{}:
tmpSlice := make([]string, 0, len(x))
for _, val := range x {
if rec := valToStr(val); rec != "" {
tmpSlice = append(tmpSlice, url.QueryEscape(rec))
}
}
s = strings.Join(tmpSlice, ",")
default:
log.Error("Context variable type is not supported: ", reflect.TypeOf(v))
}
return s
}
// URLRewriteMiddleware Will rewrite an inbund URL to a matching outbound one, it can also handle dynamic variable substitution
type URLRewriteMiddleware struct {
BaseMiddleware
}
func (m *URLRewriteMiddleware) Name() string {
return "URLRewriteMiddleware"
}
func (m *URLRewriteMiddleware) InitTriggerRx() {
// Generate regexp for each special match parameter
for verKey := range m.Spec.VersionData.Versions {
for pathKey := range m.Spec.VersionData.Versions[verKey].ExtendedPaths.URLRewrite {
rewrite := m.Spec.VersionData.Versions[verKey].ExtendedPaths.URLRewrite[pathKey]
for trKey := range rewrite.Triggers {
tr := rewrite.Triggers[trKey]
for key, h := range tr.Options.HeaderMatches {
h.Init()
tr.Options.HeaderMatches[key] = h
}
for key, q := range tr.Options.QueryValMatches {
q.Init()
tr.Options.QueryValMatches[key] = q
}
for key, h := range tr.Options.SessionMetaMatches {
h.Init()
tr.Options.SessionMetaMatches[key] = h
}
for key, h := range tr.Options.PathPartMatches {
h.Init()
tr.Options.PathPartMatches[key] = h
}
if tr.Options.PayloadMatches.MatchPattern != "" {
tr.Options.PayloadMatches.Init()
}
rewrite.Triggers[trKey] = tr
}
m.Spec.VersionData.Versions[verKey].ExtendedPaths.URLRewrite[pathKey] = rewrite
}
}
}
func (m *URLRewriteMiddleware) EnabledForSpec() bool {
for _, version := range m.Spec.VersionData.Versions {
if len(version.ExtendedPaths.URLRewrite) > 0 {
m.Spec.URLRewriteEnabled = true
m.InitTriggerRx()
return true
}
}
return false
}
func (m *URLRewriteMiddleware) CheckHostRewrite(oldPath, newTarget string, r *http.Request) {
oldAsURL, _ := url.Parse(oldPath)
newAsURL, _ := url.Parse(newTarget)
if newAsURL.Scheme != "tyk" && oldAsURL.Host != newAsURL.Host {
log.Debug("Detected a host rewrite in pattern!")
setCtxValue(r, RetainHost, true)
}
}
// ProcessRequest will run any checks on the request on the way through the system, return an error to have the chain fail
func (m *URLRewriteMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Request, _ interface{}) (error, int) {
_, versionPaths, _, _ := m.Spec.Version(r)
found, meta := m.Spec.CheckSpecMatchesStatus(r, versionPaths, URLRewrite)
if !found {
return nil, http.StatusOK
}
ctxSetOrigRequestURL(r, r.URL)
log.Debug("Rewriter active")
umeta := meta.(*apidef.URLRewriteMeta)
log.Debug(r.URL)
oldPath := r.URL.String()
p, err := urlRewrite(umeta, r)
if err != nil {
log.Error(err)
return err, http.StatusInternalServerError
}
m.CheckHostRewrite(oldPath, p, r)
newURL, err := url.Parse(p)
if err != nil {
log.Error("URL Rewrite failed, could not parse: ", p)
} else {
r.URL = newURL
}
return nil, http.StatusOK
}
func checkHeaderTrigger(r *http.Request, options map[string]apidef.StringRegexMap, any bool, triggernum int) bool {
contextData := ctxGetData(r)
fCount := 0
for mh, mr := range options {
mhCN := textproto.CanonicalMIMEHeaderKey(mh)
vals, ok := r.Header[mhCN]
if ok {
for i, v := range vals {
b := mr.Check(v)
if len(b) > 0 {
kn := "trigger-" + strconv.Itoa(triggernum) + "-" + mhCN + "-" + strconv.Itoa(i)
contextData[kn] = b
fCount++
}
}
}
}
if fCount > 0 {
ctxSetData(r, contextData)
if any {
return true
}
return len(options) <= fCount
}
return false
}
func checkQueryString(r *http.Request, options map[string]apidef.StringRegexMap, any bool, triggernum int) bool {
contextData := ctxGetData(r)
fCount := 0
for mv, mr := range options {
qvals := r.URL.Query()
vals, ok := qvals[mv]
if ok {
for i, v := range vals {
b := mr.Check(v)
if len(b) > 0 {
kn := "trigger-" + strconv.Itoa(triggernum) + "-" + mv + "-" + strconv.Itoa(i)
contextData[kn] = b
fCount++
}
}
}
}
if fCount > 0 {
ctxSetData(r, contextData)
if any {
return true
}
return len(options) <= fCount
}
return false
}
func checkPathParts(r *http.Request, options map[string]apidef.StringRegexMap, any bool, triggernum int) bool {
contextData := ctxGetData(r)
fCount := 0
for mv, mr := range options {
pathParts := strings.Split(r.URL.Path, "/")
for _, part := range pathParts {
b := mr.Check(part)
if len(b) > 0 {
kn := "trigger-" + strconv.Itoa(triggernum) + "-" + mv + "-" + strconv.Itoa(fCount)
contextData[kn] = b
fCount++
}
}
}
if fCount > 0 {
ctxSetData(r, contextData)
if any {
return true
}
return len(options) <= fCount
}
return false
}
func checkSessionTrigger(r *http.Request, sess *user.SessionState, options map[string]apidef.StringRegexMap, any bool, triggernum int) bool {
contextData := ctxGetData(r)
fCount := 0
for mh, mr := range options {
rawVal, ok := sess.MetaData[mh]
if ok {
val, valOk := rawVal.(string)
if valOk {
b := mr.Check(val)
if len(b) > 0 {
kn := "trigger-" + strconv.Itoa(triggernum) + "-" + mh
contextData[kn] = b
fCount++
}
}
}
}
if fCount > 0 {
ctxSetData(r, contextData)
if any {
return true
}
return len(options) <= fCount
}
return false
}
func checkPayload(r *http.Request, options apidef.StringRegexMap, triggernum int) bool {
contextData := ctxGetData(r)
bodyBytes, _ := ioutil.ReadAll(r.Body)
b := options.Check(string(bodyBytes))
if len(b) > 0 {
kn := "trigger-" + strconv.Itoa(triggernum) + "-payload"
contextData[kn] = string(b)
return true
}
return false
}