-
Notifications
You must be signed in to change notification settings - Fork 0
/
replacestatus.go
75 lines (57 loc) · 1.67 KB
/
replacestatus.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
package traefik_replace_response_code
import (
"context"
"log"
"net/http"
"net/http/httptest"
)
// Config the plugin configuration.
type Config struct {
InputCode int `json:"inputCode,omitempty"`
OutputCode int `json:"outputCode,omitempty"`
RemoveBody bool `json:"removeBody,omitempty"`
}
func CreateConfig() *Config {
return &Config{
InputCode: 429,
OutputCode: 202,
RemoveBody: false,
}
}
type StatusCodeReplacer struct {
next http.Handler
inputCode int
outputCode int
removeBody bool
}
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
log.Printf("Configuring plugin replace-response-code with inputCode: %d, outputCode: %d, removeBody: %s", config.InputCode, config.OutputCode, config.RemoveBody)
return &StatusCodeReplacer{
inputCode: config.InputCode,
outputCode: config.OutputCode,
removeBody: config.RemoveBody,
next: next,
}, nil
}
func (a *StatusCodeReplacer) replacer() http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
//recorder is used to delegate call. Its response will be used to create the correct ResponseWriter status code.
recorder := httptest.NewRecorder()
a.next.ServeHTTP(recorder, req)
if recorder.Code == a.inputCode {
rw.WriteHeader(a.outputCode)
if !a.removeBody {
_, _ = rw.Write(recorder.Body.Bytes())
}
}else{
rw.WriteHeader(recorder.Code)
_, _ = rw.Write(recorder.Body.Bytes())
}
for name, values := range recorder.Header(){
rw.Header()[name] = values
}
})
}
func (a *StatusCodeReplacer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
a.replacer().ServeHTTP(rw,req)
}