-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
316 lines (273 loc) · 7.53 KB
/
handler.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
package traefik_inline_response
import (
"bytes"
"context"
"encoding/json"
"fmt"
"html/template"
"io"
"net/http"
"os"
"regexp"
"strings"
)
// Config is the type that holds the configuration for this plugin.
type Config struct {
Matchers []Matcher `json:"matchers" mapstructure:"matchers"`
Fallback *Fallback `json:"fallback" mapstructure:"fallback"`
Debug bool `json:"debug" mapstructure:"debug"`
}
type Matcher struct {
Path Path `json:"path" mapstructure:"path"`
StatusCode *int `json:"statusCode" mapstructure:"statusCode"`
Resp Response `json:"response" mapstructure:"response"`
}
type Path struct {
Abs *string `json:"abs" mapstructure:"abs"`
Prefix *string `json:"prefix" mapstructure:"prefix"`
Regex *string `json:"regex" mapstructure:"regex"`
}
type Fallback struct {
StatusCode *int `json:"statusCode" mapstructure:"statusCode"`
Resp Response `json:"response" mapstructure:"response"`
}
type Response struct {
Raw *string `json:"data" mapstructure:"raw"`
Template *string `json:"template" mapstructure:"template"`
JSON *map[string]any `json:"json" mapstructure:"json"`
}
const (
pathMatcherModeUnknown = iota
pathMatcherModeAbsolutePath
pathMatcherModePrefix
pathMatcherModeRegex
)
type pathMatcherMode uint8
const (
responseModeUnknown = iota
responseModeEmpty
responseModeRaw
responseModeTemplate
responseModeJSON
)
type responseMode uint8
type handlerRuntime struct {
matchers []*matcherRuntime
fallback *fallbackRuntime
}
type matcherRuntime struct {
path *pathRuntime
statusCode int
resp *responseRuntime
}
type pathRuntime struct {
mode pathMatcherMode
abs *string
prefix *string
regex *regexp.Regexp
}
type responseRuntime struct {
mode responseMode
raw string
templ *template.Template
json string
}
type fallbackRuntime struct {
statusCode int
resp *responseRuntime
}
func CreateConfig() *Config {
return &Config{
// Empty for now. Initialize relevant fields if needed in the future.
}
}
func (c *Config) validate() (*handlerRuntime, error) {
rt := &handlerRuntime{}
for _, m := range c.Matchers {
if m.StatusCode == nil {
return nil, fmt.Errorf("Must specify a status code in the matcher")
}
p, err := validatePath(&m.Path)
if err != nil {
return nil, err
}
r, err := validateResponse(&m.Resp, "matcher")
if err != nil {
return nil, err
}
rt.matchers = append(rt.matchers, &matcherRuntime{
path: p,
statusCode: *m.StatusCode,
resp: r,
})
}
f, err := validateFallback(c.Fallback)
if err != nil {
return nil, err
}
rt.fallback = f
return rt, nil
}
func validatePath(path *Path) (*pathRuntime, error) {
p := &pathRuntime{}
if path.Abs != nil {
if path.Prefix != nil {
return nil, fmt.Errorf("Cannot specify path prefix when absolute path is specified")
}
if path.Regex != nil {
return nil, fmt.Errorf("Cannot specify path regex when absolute path is specified")
}
p.mode = pathMatcherModeAbsolutePath
p.abs = path.Abs
} else if path.Prefix != nil {
if path.Regex != nil {
return nil, fmt.Errorf("Cannot specify path regex when path prefix is specified")
}
p.mode = pathMatcherModePrefix
p.prefix = path.Prefix
} else if path.Regex != nil {
p.mode = pathMatcherModeRegex
regex, err := regexp.Compile(*path.Regex)
if err != nil {
return nil, fmt.Errorf("Invalid regex in matcher path, reason: %w", err)
}
p.mode = pathMatcherModeRegex
p.regex = regex
} else {
return nil, fmt.Errorf("At least one of absoltue path, path prefix or path regex must be specified")
}
return p, nil
}
func validateResponse(resp *Response, loc string) (*responseRuntime, error) {
r := &responseRuntime{}
if resp.Raw != nil {
if resp.Template != nil {
return nil, fmt.Errorf("Cannot specify template in %s response when raw is specified", loc)
}
if resp.JSON != nil {
return nil, fmt.Errorf("Cannot specify json in %s response when raw is specified", loc)
}
r.mode = responseModeRaw
r.raw = *resp.Raw
} else if resp.Template != nil {
if resp.JSON != nil {
return nil, fmt.Errorf("Cannot specify json in %s response when template is specified", loc)
}
templ, err := template.New("traefik-inline-response").Parse(*resp.Template)
if err != nil {
return nil, fmt.Errorf("Invalid template in %s response, reason: %w", loc, err)
}
r.mode = responseModeTemplate
r.templ = templ
} else if resp.JSON != nil {
b, err := json.Marshal(*resp.JSON)
if err != nil {
return nil, fmt.Errorf("Invalid JSON in %s response, reason: %w", loc, err)
}
r.mode = responseModeJSON
r.json = string(b)
} else {
r.mode = responseModeEmpty
}
return r, nil
}
func validateFallback(fallback *Fallback) (*fallbackRuntime, error) {
if fallback == nil {
return nil, nil
}
if fallback.StatusCode == nil {
return nil, fmt.Errorf("Must specify a status code in the fallback")
}
r, err := validateResponse(&fallback.Resp, "fallback")
if err != nil {
return nil, err
}
return &fallbackRuntime{
statusCode: *fallback.StatusCode,
resp: r,
}, nil
}
type Handler struct {
next http.Handler
name string
runtime *handlerRuntime
}
func prettyPrintJSON(x interface{}) string {
jsondata, _ := json.MarshalIndent(x, "", " ")
return string(jsondata)
}
func log(debug bool, format string, args ...interface{}) {
if debug {
os.Stdout.WriteString(fmt.Sprintf("traefik-inline-response - "+format+"\n", args...))
}
}
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
log(config.Debug, "received config = %s", prettyPrintJSON(*config))
rt, err := config.validate()
if err != nil {
return nil, err
}
return &Handler{
next: next,
name: name,
runtime: rt,
}, nil
}
func (h *Handler) ServeHTTP(writer http.ResponseWriter, req *http.Request) {
for _, m := range h.runtime.matchers {
switch m.path.mode {
case pathMatcherModeAbsolutePath:
if req.URL.Path == *m.path.abs {
respondToRequest(req, writer, m.statusCode, m.resp)
return
}
case pathMatcherModePrefix:
if strings.HasPrefix(req.URL.Path, *m.path.prefix) {
respondToRequest(req, writer, m.statusCode, m.resp)
return
}
case pathMatcherModeRegex:
if m.path.regex.MatchString(req.URL.Path) {
respondToRequest(req, writer, m.statusCode, m.resp)
return
}
default:
respondWithError(writer, "invalid path matcher mode, indicating a bug in the plugin")
return
}
}
if h.runtime.fallback != nil {
respondToRequest(req, writer, h.runtime.fallback.statusCode, h.runtime.fallback.resp)
return
}
h.next.ServeHTTP(writer, req)
}
func respondToRequest(req *http.Request, writer http.ResponseWriter, statusCode int, resp *responseRuntime) {
var err error
switch resp.mode {
case responseModeEmpty:
writer.WriteHeader(statusCode)
case responseModeRaw:
writer.WriteHeader(statusCode)
_, err = io.WriteString(writer, resp.raw)
case responseModeTemplate:
var buf bytes.Buffer
err = resp.templ.Execute(&buf, req)
if err == nil {
writer.WriteHeader(statusCode)
_, err = io.Copy(writer, &buf)
}
case responseModeJSON:
writer.WriteHeader(statusCode)
// TODO: Set the content type header.
_, err = io.WriteString(writer, resp.json)
default:
err = fmt.Errorf("invalid path matcher mode, indicating a bug in the plugin")
}
if err != nil {
respondWithError(writer, fmt.Sprintf("failed while writing the response, reason: %s", err.Error()))
}
}
func respondWithError(writer http.ResponseWriter, err string) {
http.Error(writer, err, http.StatusInternalServerError)
}