7
7
"fmt"
8
8
"io"
9
9
"io/ioutil"
10
+ "log"
10
11
"net/http"
12
+ "os"
11
13
"time"
12
14
)
13
15
@@ -31,6 +33,7 @@ type Modsecurity struct {
31
33
next http.Handler
32
34
modSecurityUrl string
33
35
name string
36
+ logger * log.Logger
34
37
}
35
38
36
39
// New created a new Modsecurity plugin.
@@ -43,6 +46,7 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
43
46
modSecurityUrl : config .ModSecurityUrl ,
44
47
next : next ,
45
48
name : name ,
49
+ logger : log .New (os .Stdout , "" , log .LstdFlags ),
46
50
}, nil
47
51
}
48
52
@@ -58,7 +62,8 @@ func (a *Modsecurity) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
58
62
// in the request.
59
63
body , err := ioutil .ReadAll (req .Body )
60
64
if err != nil {
61
- http .Error (rw , err .Error (), http .StatusInternalServerError )
65
+ a .logger .Printf ("fail to read incoming request: %s" , err .Error ())
66
+ http .Error (rw , "" , http .StatusBadGateway )
62
67
return
63
68
}
64
69
@@ -71,7 +76,8 @@ func (a *Modsecurity) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
71
76
proxyReq , err := http .NewRequest (req .Method , url , bytes .NewReader (body ))
72
77
73
78
if err != nil {
74
- http .Error (rw , err .Error (), http .StatusBadRequest )
79
+ a .logger .Printf ("fail to prepare forwarded request: %s" , err .Error ())
80
+ http .Error (rw , "" , http .StatusBadGateway )
75
81
return
76
82
}
77
83
@@ -84,7 +90,8 @@ func (a *Modsecurity) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
84
90
85
91
resp , err := httpClient .Do (proxyReq )
86
92
if err != nil {
87
- http .Error (rw , err .Error (), http .StatusBadGateway )
93
+ a .logger .Printf ("fail to send HTTP request to modsec: %s" , err .Error ())
94
+ http .Error (rw , "" , http .StatusBadGateway )
88
95
return
89
96
}
90
97
defer resp .Body .Close ()
0 commit comments