2015-01-13 14:43:45 -05:00
|
|
|
package middleware
|
|
|
|
|
2015-04-23 14:35:56 -05:00
|
|
|
import (
|
2015-06-11 22:42:28 -05:00
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"net"
|
2015-04-23 14:35:56 -05:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
2015-01-13 14:43:45 -05:00
|
|
|
|
|
|
|
// responseRecorder is a type of ResponseWriter that captures
|
|
|
|
// the status code written to it and also the size of the body
|
|
|
|
// written in the response. A status code does not have
|
|
|
|
// to be written, however, in which case 200 must be assumed.
|
|
|
|
// It is best to have the constructor initialize this type
|
|
|
|
// with that default status code.
|
2016-02-20 16:52:42 -05:00
|
|
|
type ResponseRecorder struct {
|
2015-01-13 14:43:45 -05:00
|
|
|
http.ResponseWriter
|
|
|
|
status int
|
|
|
|
size int
|
2015-04-23 14:35:56 -05:00
|
|
|
start time.Time
|
2015-01-13 14:43:45 -05:00
|
|
|
}
|
|
|
|
|
2015-01-30 00:06:53 -05:00
|
|
|
// NewResponseRecorder makes and returns a new responseRecorder,
|
2015-01-13 14:43:45 -05:00
|
|
|
// which captures the HTTP Status code from the ResponseWriter
|
|
|
|
// and also the length of the response body written through it.
|
|
|
|
// Because a status is not set unless WriteHeader is called
|
|
|
|
// explicitly, this constructor initializes with a status code
|
|
|
|
// of 200 to cover the default case.
|
2016-02-20 16:52:42 -05:00
|
|
|
func NewResponseRecorder(w http.ResponseWriter) *ResponseRecorder {
|
|
|
|
return &ResponseRecorder{
|
2015-01-13 14:43:45 -05:00
|
|
|
ResponseWriter: w,
|
|
|
|
status: http.StatusOK,
|
2015-04-23 14:35:56 -05:00
|
|
|
start: time.Now(),
|
2015-01-13 14:43:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteHeader records the status code and calls the
|
|
|
|
// underlying ResponseWriter's WriteHeader method.
|
2016-02-20 16:52:42 -05:00
|
|
|
func (r *ResponseRecorder) WriteHeader(status int) {
|
2015-01-13 14:43:45 -05:00
|
|
|
r.status = status
|
|
|
|
r.ResponseWriter.WriteHeader(status)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write is a wrapper that records the size of the body
|
|
|
|
// that gets written.
|
2016-02-20 16:52:42 -05:00
|
|
|
func (r *ResponseRecorder) Write(buf []byte) (int, error) {
|
2015-01-13 14:43:45 -05:00
|
|
|
n, err := r.ResponseWriter.Write(buf)
|
|
|
|
if err == nil {
|
|
|
|
r.size += n
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
2015-06-11 22:42:28 -05:00
|
|
|
|
2016-02-20 16:52:42 -05:00
|
|
|
// Size is a Getter to size property
|
|
|
|
func (r *ResponseRecorder) Size() int {
|
|
|
|
return r.size
|
|
|
|
}
|
|
|
|
|
|
|
|
// Status is a Getter to status property
|
|
|
|
func (r *ResponseRecorder) Status() int {
|
|
|
|
return r.status
|
|
|
|
}
|
|
|
|
|
2016-02-24 14:23:15 -05:00
|
|
|
// Hijack implements http.Hijacker. It simply wraps the underlying
|
|
|
|
// ResponseWriter's Hijack method if there is one, or returns an error.
|
2016-02-20 16:52:42 -05:00
|
|
|
func (r *ResponseRecorder) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
2015-06-11 22:42:28 -05:00
|
|
|
if hj, ok := r.ResponseWriter.(http.Hijacker); ok {
|
|
|
|
return hj.Hijack()
|
|
|
|
}
|
2016-02-24 14:23:15 -05:00
|
|
|
return nil, nil, errors.New("not a Hijacker")
|
2015-06-11 22:42:28 -05:00
|
|
|
}
|