forked from motiv-labs/janus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
76 lines (63 loc) · 2.17 KB
/
middleware.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
package responsetransformer
import (
"net/http"
"github.com/hellofresh/janus/pkg/proxy"
)
type headerFn func(headerName string, headerValue string)
// Options represents the available options to transform
type Options struct {
Headers map[string]string `json:"headers"`
}
// Config represent the configuration of the modify headers middleware
type Config struct {
Add Options `json:"add"`
Append Options `json:"append"`
Remove Options `json:"remove"`
Replace Options `json:"replace"`
}
// NewResponseTransformer creates a new instance of RequestTransformer
func NewResponseTransformer(config Config) proxy.OutLink {
return func(req *http.Request, res *http.Response) (*http.Response, error) {
transform(config.Remove.Headers, removeHeaders(res))
transform(config.Replace.Headers, replaceHeaders(res))
transform(config.Add.Headers, addHeaders(res))
transform(config.Append.Headers, appendHeaders(res))
return res, nil
}
}
// If and only if the header is not already set, set a new header with the given value. Ignored if the header is already set.
func addHeaders(res *http.Response) headerFn {
return func(headerName string, headerValue string) {
if res.Header.Get(headerName) == "" {
res.Header.Add(headerName, headerValue)
}
}
}
// If the header is not set, set it with the given value. If it is already set, a new header with the same name and the new value will be set.
func appendHeaders(res *http.Response) headerFn {
return func(headerName string, headerValue string) {
res.Header.Add(headerName, headerValue)
}
}
// Unset the headers with the given name.
func removeHeaders(res *http.Response) headerFn {
return func(headerName string, headerValue string) {
res.Header.Del(headerName)
}
}
// If and only if the header is already set, replace its old value with the new one. Ignored if the header is not already set.
func replaceHeaders(res *http.Response) headerFn {
return func(headerName string, headerValue string) {
if res.Header.Get(headerName) != "" {
res.Header.Set(headerName, headerValue)
}
}
}
func transform(values map[string]string, fn headerFn) {
if len(values) <= 0 {
return
}
for name, value := range values {
fn(name, value)
}
}