forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mw_modify_headers.go
100 lines (84 loc) · 2.65 KB
/
mw_modify_headers.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
package main
import (
"net/http"
"strings"
"github.com/TykTechnologies/tyk/apidef"
)
// TransformMiddleware is a middleware that will apply a template to a request body to transform it's contents ready for an upstream API
type TransformHeaders struct {
*BaseMiddleware
}
const (
metaLabel = "$tyk_meta."
contextLabel = "$tyk_context."
)
func (t *TransformHeaders) GetName() string {
return "TransformHeaders"
}
func (t *TransformHeaders) IsEnabledForSpec() bool {
for _, version := range t.Spec.VersionData.Versions {
if len(version.ExtendedPaths.TransformHeader) > 0 ||
len(version.GlobalHeaders) > 0 ||
len(version.GlobalHeadersRemove) > 0 {
return true
}
}
return false
}
// iterateAddHeaders is a helper functino that will iterate of a map and inject the key and value as a header in the request.
// if the key and value contain a tyk session variable reference, then it will try to inject the value
func (t *TransformHeaders) iterateAddHeaders(kv map[string]string, r *http.Request) {
// Get session data
session := ctxGetSession(r)
contextData := ctxGetData(r)
// Iterate and manage key array injection
for nKey, nVal := range kv {
if strings.Contains(nVal, metaLabel) {
// Using meta_data key
if session != nil {
metaKey := strings.Replace(nVal, metaLabel, "", 1)
metaVal, ok := session.MetaData[metaKey]
if ok {
r.Header.Set(nKey, metaVal)
} else {
log.Warning("Session Meta Data not found for key in map: ", metaKey)
}
}
} else if strings.Contains(nVal, contextLabel) {
// Using context key
if contextData != nil {
metaKey := strings.Replace(nVal, contextLabel, "", 1)
val, ok := contextData[metaKey]
if ok {
r.Header.Set(nKey, valToStr(val))
} else {
log.Warning("Context Data not found for key in map: ", metaKey)
}
}
} else {
r.Header.Set(nKey, nVal)
}
}
}
// ProcessRequest will run any checks on the request on the way through the system, return an error to have the chain fail
func (t *TransformHeaders) ProcessRequest(w http.ResponseWriter, r *http.Request, _ interface{}) (error, int) {
vInfo, versionPaths, _, _ := t.Spec.GetVersionData(r)
// Manage global headers first - remove
for _, gdKey := range vInfo.GlobalHeadersRemove {
log.Debug("Removing: ", gdKey)
r.Header.Del(gdKey)
}
// Add
if len(vInfo.GlobalHeaders) > 0 {
t.iterateAddHeaders(vInfo.GlobalHeaders, r)
}
found, meta := t.Spec.CheckSpecMatchesStatus(r, versionPaths, HeaderInjected)
if found {
hmeta := meta.(*apidef.HeaderInjectionMeta)
for _, dKey := range hmeta.DeleteHeaders {
r.Header.Del(dKey)
}
t.iterateAddHeaders(hmeta.AddHeaders, r)
}
return nil, 200
}