2017-05-09 12:02:44 +00:00
|
|
|
package accesslog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2017-06-08 19:14:30 +00:00
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
"github.com/containous/traefik/old/middlewares"
|
2017-05-09 12:02:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-06-08 19:14:30 +00:00
|
|
|
_ middlewares.Stateful = &captureResponseWriter{}
|
2017-05-09 12:02:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// captureResponseWriter is a wrapper of type http.ResponseWriter
|
|
|
|
// that tracks request status and size
|
|
|
|
type captureResponseWriter struct {
|
|
|
|
rw http.ResponseWriter
|
|
|
|
status int
|
|
|
|
size int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (crw *captureResponseWriter) Header() http.Header {
|
|
|
|
return crw.rw.Header()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (crw *captureResponseWriter) Write(b []byte) (int, error) {
|
|
|
|
if crw.status == 0 {
|
|
|
|
crw.status = http.StatusOK
|
|
|
|
}
|
|
|
|
size, err := crw.rw.Write(b)
|
|
|
|
crw.size += int64(size)
|
|
|
|
return size, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (crw *captureResponseWriter) WriteHeader(s int) {
|
|
|
|
crw.rw.WriteHeader(s)
|
|
|
|
crw.status = s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (crw *captureResponseWriter) Flush() {
|
|
|
|
if f, ok := crw.rw.(http.Flusher); ok {
|
|
|
|
f.Flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (crw *captureResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
|
|
|
if h, ok := crw.rw.(http.Hijacker); ok {
|
|
|
|
return h.Hijack()
|
|
|
|
}
|
2018-07-03 08:02:03 +00:00
|
|
|
return nil, nil, fmt.Errorf("not a hijacker: %T", crw.rw)
|
2017-05-09 12:02:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (crw *captureResponseWriter) CloseNotify() <-chan bool {
|
|
|
|
if c, ok := crw.rw.(http.CloseNotifier); ok {
|
|
|
|
return c.CloseNotify()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (crw *captureResponseWriter) Status() int {
|
|
|
|
return crw.status
|
|
|
|
}
|
|
|
|
|
|
|
|
func (crw *captureResponseWriter) Size() int64 {
|
|
|
|
return crw.size
|
|
|
|
}
|