-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathhtransformation.go
152 lines (125 loc) · 3.76 KB
/
htransformation.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
package htransformation
import (
"bufio"
"context"
"fmt"
"net"
"net/http"
"github.com/tomMoulard/htransformation/pkg/handler/deleter"
"github.com/tomMoulard/htransformation/pkg/handler/join"
"github.com/tomMoulard/htransformation/pkg/handler/rename"
"github.com/tomMoulard/htransformation/pkg/handler/rewrite"
"github.com/tomMoulard/htransformation/pkg/handler/set"
"github.com/tomMoulard/htransformation/pkg/types"
)
// HeadersTransformation holds the necessary components of a Traefik plugin.
type HeadersTransformation struct {
name string
next http.Handler
reqHandlers []types.Handler
respHandlers []types.Handler
}
// Config holds configuration to be passed to the plugin.
type Config struct {
Rules []types.Rule
}
// CreateConfig populates the Config data object.
func CreateConfig() *Config {
return &Config{
Rules: []types.Rule{},
}
}
// New instantiates and returns the required components used to handle an HTTP request.
func New(_ context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
handlerBuilder := map[types.RuleType]func(types.Rule) (types.Handler, error){
types.Delete: deleter.New,
types.Join: join.New,
types.Rename: rename.New,
types.RewriteValueRule: rewrite.New,
types.Set: set.New,
}
reqHandlers := make([]types.Handler, 0, len(config.Rules))
respHandlers := make([]types.Handler, 0, len(config.Rules))
for _, rule := range config.Rules {
newHandler, ok := handlerBuilder[rule.Type]
if !ok {
return nil, fmt.Errorf("%w: %s", types.ErrInvalidRuleType, rule.Name)
}
h, err := newHandler(rule)
if err != nil {
return nil, fmt.Errorf("%w: %s", err, rule.Name)
}
if err := h.Validate(); err != nil {
return nil, fmt.Errorf("%w: %s", err, rule.Name)
}
if rule.SetOnResponse {
respHandlers = append(respHandlers, h)
} else {
reqHandlers = append(reqHandlers, h)
}
}
return &HeadersTransformation{
name: name,
next: next,
reqHandlers: reqHandlers,
respHandlers: respHandlers,
}, nil
}
// Iterate over every header to match the ones specified in the config and
// return nothing if regexp failed.
func (u *HeadersTransformation) ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) {
for _, handler := range u.reqHandlers {
handler.Handle(responseWriter, request)
}
wrappedResponseWriter := newWrappedResponseWriter(responseWriter, func(rw http.ResponseWriter) {
for _, handler := range u.respHandlers {
handler.Handle(rw, request)
}
})
u.next.ServeHTTP(wrappedResponseWriter, request)
}
type wrappedResponseWriter struct {
rw http.ResponseWriter
handler func(http.ResponseWriter)
headerSent bool
}
func newWrappedResponseWriter(rw http.ResponseWriter, handler func(http.ResponseWriter)) http.ResponseWriter {
return &wrappedResponseWriter{
rw: rw,
handler: handler,
headerSent: false,
}
}
func (wrw *wrappedResponseWriter) handleResponseHeader() {
if wrw.headerSent {
return
}
wrw.headerSent = true
wrw.handler(wrw.rw)
}
func (wrw *wrappedResponseWriter) Header() http.Header {
return wrw.rw.Header()
}
func (wrw *wrappedResponseWriter) Write(p []byte) (int, error) {
wrw.handleResponseHeader()
n, err := wrw.rw.Write(p)
if err != nil {
return 0, fmt.Errorf("write response: %w", err)
}
return n, nil
}
func (wrw *wrappedResponseWriter) WriteHeader(statusCode int) {
wrw.handleResponseHeader()
wrw.rw.WriteHeader(statusCode)
}
func (wrw *wrappedResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
hijacker, ok := wrw.rw.(http.Hijacker)
if !ok {
return nil, nil, types.ErrNotHTTPHijacker
}
conn, rw, err := hijacker.Hijack()
if err != nil {
return nil, nil, fmt.Errorf("hijack: %w", err)
}
return conn, rw, nil
}