77 "fmt"
88 "io"
99 "io/ioutil"
10+ "log"
1011 "net/http"
12+ "os"
1113 "time"
1214)
1315
@@ -31,6 +33,7 @@ type Modsecurity struct {
3133 next http.Handler
3234 modSecurityUrl string
3335 name string
36+ logger * log.Logger
3437}
3538
3639// New created a new Modsecurity plugin.
@@ -43,6 +46,7 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
4346 modSecurityUrl : config .ModSecurityUrl ,
4447 next : next ,
4548 name : name ,
49+ logger : log .New (os .Stdout , "" , log .LstdFlags ),
4650 }, nil
4751}
4852
@@ -58,7 +62,8 @@ func (a *Modsecurity) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
5862 // in the request.
5963 body , err := ioutil .ReadAll (req .Body )
6064 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 )
6267 return
6368 }
6469
@@ -71,7 +76,8 @@ func (a *Modsecurity) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
7176 proxyReq , err := http .NewRequest (req .Method , url , bytes .NewReader (body ))
7277
7378 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 )
7581 return
7682 }
7783
@@ -84,7 +90,8 @@ func (a *Modsecurity) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
8490
8591 resp , err := httpClient .Do (proxyReq )
8692 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 )
8895 return
8996 }
9097 defer resp .Body .Close ()
0 commit comments