mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-23 22:27:38 -05:00
Merge pull request #620 from humboldtux/recorder
middleware: Export ResponseRecorder and add a couple getter methods
This commit is contained in:
commit
c12847e5ba
2 changed files with 17 additions and 7 deletions
|
@ -14,7 +14,7 @@ import (
|
||||||
// to be written, however, in which case 200 must be assumed.
|
// to be written, however, in which case 200 must be assumed.
|
||||||
// It is best to have the constructor initialize this type
|
// It is best to have the constructor initialize this type
|
||||||
// with that default status code.
|
// with that default status code.
|
||||||
type responseRecorder struct {
|
type ResponseRecorder struct {
|
||||||
http.ResponseWriter
|
http.ResponseWriter
|
||||||
status int
|
status int
|
||||||
size int
|
size int
|
||||||
|
@ -27,8 +27,8 @@ type responseRecorder struct {
|
||||||
// Because a status is not set unless WriteHeader is called
|
// Because a status is not set unless WriteHeader is called
|
||||||
// explicitly, this constructor initializes with a status code
|
// explicitly, this constructor initializes with a status code
|
||||||
// of 200 to cover the default case.
|
// of 200 to cover the default case.
|
||||||
func NewResponseRecorder(w http.ResponseWriter) *responseRecorder {
|
func NewResponseRecorder(w http.ResponseWriter) *ResponseRecorder {
|
||||||
return &responseRecorder{
|
return &ResponseRecorder{
|
||||||
ResponseWriter: w,
|
ResponseWriter: w,
|
||||||
status: http.StatusOK,
|
status: http.StatusOK,
|
||||||
start: time.Now(),
|
start: time.Now(),
|
||||||
|
@ -37,14 +37,14 @@ func NewResponseRecorder(w http.ResponseWriter) *responseRecorder {
|
||||||
|
|
||||||
// WriteHeader records the status code and calls the
|
// WriteHeader records the status code and calls the
|
||||||
// underlying ResponseWriter's WriteHeader method.
|
// underlying ResponseWriter's WriteHeader method.
|
||||||
func (r *responseRecorder) WriteHeader(status int) {
|
func (r *ResponseRecorder) WriteHeader(status int) {
|
||||||
r.status = status
|
r.status = status
|
||||||
r.ResponseWriter.WriteHeader(status)
|
r.ResponseWriter.WriteHeader(status)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write is a wrapper that records the size of the body
|
// Write is a wrapper that records the size of the body
|
||||||
// that gets written.
|
// that gets written.
|
||||||
func (r *responseRecorder) Write(buf []byte) (int, error) {
|
func (r *ResponseRecorder) Write(buf []byte) (int, error) {
|
||||||
n, err := r.ResponseWriter.Write(buf)
|
n, err := r.ResponseWriter.Write(buf)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
r.size += n
|
r.size += n
|
||||||
|
@ -52,9 +52,19 @@ func (r *responseRecorder) Write(buf []byte) (int, error) {
|
||||||
return n, err
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
// Hijacker is a wrapper of http.Hijacker underearth if any,
|
// Hijacker is a wrapper of http.Hijacker underearth if any,
|
||||||
// otherwise it just returns an error.
|
// otherwise it just returns an error.
|
||||||
func (r *responseRecorder) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
func (r *ResponseRecorder) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
||||||
if hj, ok := r.ResponseWriter.(http.Hijacker); ok {
|
if hj, ok := r.ResponseWriter.(http.Hijacker); ok {
|
||||||
return hj.Hijack()
|
return hj.Hijack()
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@ type replacer struct {
|
||||||
// values into the replacer. rr may be nil if it is not
|
// values into the replacer. rr may be nil if it is not
|
||||||
// available. emptyValue should be the string that is used
|
// available. emptyValue should be the string that is used
|
||||||
// in place of empty string (can still be empty string).
|
// in place of empty string (can still be empty string).
|
||||||
func NewReplacer(r *http.Request, rr *responseRecorder, emptyValue string) Replacer {
|
func NewReplacer(r *http.Request, rr *ResponseRecorder, emptyValue string) Replacer {
|
||||||
rep := replacer{
|
rep := replacer{
|
||||||
replacements: map[string]string{
|
replacements: map[string]string{
|
||||||
"{method}": r.Method,
|
"{method}": r.Method,
|
||||||
|
|
Loading…
Reference in a new issue