forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
res_handler_header_injector.go
73 lines (55 loc) · 1.71 KB
/
res_handler_header_injector.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
package main
import (
"github.com/lonelycode/tykcommon"
"github.com/mitchellh/mapstructure"
"net/http"
)
type HeaderInjectorOptions struct {
AddHeaders map[string]string `mapstructure:"add_headers" bson:"add_headers" json:"add_headers"`
RemoveHeaders []string `mapstructure:"remove_headers" bson:"remove_headers" json:"remove_headers"`
}
type HeaderInjector struct {
Spec *APISpec
config HeaderInjectorOptions
}
func (h HeaderInjector) New(c interface{}, spec *APISpec) (TykResponseHandler, error) {
thisHandler := HeaderInjector{}
thisModuleConfig := HeaderInjectorOptions{}
err := mapstructure.Decode(c, &thisModuleConfig)
if err != nil {
log.Error(err)
return nil, err
}
thisHandler.config = thisModuleConfig
thisHandler.Spec = spec
return thisHandler, nil
}
func (h HeaderInjector) HandleResponse(rw http.ResponseWriter, res *http.Response, req *http.Request, ses *SessionState) error {
// TODO: This should only target specific paths
// Uee the request status validator to see if it's in our response list
var stat RequestStatus
var meta interface{}
var found bool
_, versionPaths, _, _ := h.Spec.GetVersionData(req)
found, meta = h.Spec.CheckSpecMatchesStatus(req.URL.Path, req.Method, versionPaths, HeaderInjectedResponse)
if found {
stat = StatusHeaderInjected
}
if stat == StatusHeaderInjected {
thisMeta := meta.(*tykcommon.HeaderInjectionMeta)
for _, dKey := range thisMeta.DeleteHeaders {
res.Header.Del(dKey)
}
for nKey, nVal := range thisMeta.AddHeaders {
res.Header.Add(nKey, nVal)
}
}
// Global header options
for _, n := range h.config.RemoveHeaders {
res.Header.Del(n)
}
for h, v := range h.config.AddHeaders {
res.Header.Add(h, v)
}
return nil
}