detect CloseNotify capability in accesslog and metrics

This commit is contained in:
mpl 2019-12-10 18:18:04 +01:00 committed by Traefiker Bot
parent 1d4f10bead
commit bdf4c6723f
5 changed files with 59 additions and 4 deletions

View file

@ -13,6 +13,20 @@ var (
_ middlewares.Stateful = &captureResponseWriter{}
)
type capturer interface {
http.ResponseWriter
Size() int64
Status() int
}
func newCaptureResponseWriter(rw http.ResponseWriter) capturer {
capt := &captureResponseWriter{rw: rw}
if _, ok := rw.(http.CloseNotifier); !ok {
return capt
}
return captureResponseWriterWithCloseNotify{capt}
}
// captureResponseWriter is a wrapper of type http.ResponseWriter
// that tracks request status and size
type captureResponseWriter struct {
@ -21,6 +35,16 @@ type captureResponseWriter struct {
size int64
}
type captureResponseWriterWithCloseNotify struct {
*captureResponseWriter
}
// CloseNotify returns a channel that receives at most a
// single value (true) when the client connection has gone away.
func (r *captureResponseWriterWithCloseNotify) CloseNotify() <-chan bool {
return r.rw.(http.CloseNotifier).CloseNotify()
}
func (crw *captureResponseWriter) Header() http.Header {
return crw.rw.Header()
}