mirror of
https://github.com/caddyserver/caddy.git
synced 2024-11-26 02:09:47 +08:00
Merge pull request #620 from humboldtux/recorder
middleware: Export ResponseRecorder and add a couple getter methods
This commit is contained in:
commit
c12847e5ba
|
@ -14,7 +14,7 @@ import (
|
|||
// 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.
|
||||
type responseRecorder struct {
|
||||
type ResponseRecorder struct {
|
||||
http.ResponseWriter
|
||||
status int
|
||||
size int
|
||||
|
@ -27,8 +27,8 @@ type responseRecorder struct {
|
|||
// 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.
|
||||
func NewResponseRecorder(w http.ResponseWriter) *responseRecorder {
|
||||
return &responseRecorder{
|
||||
func NewResponseRecorder(w http.ResponseWriter) *ResponseRecorder {
|
||||
return &ResponseRecorder{
|
||||
ResponseWriter: w,
|
||||
status: http.StatusOK,
|
||||
start: time.Now(),
|
||||
|
@ -37,14 +37,14 @@ func NewResponseRecorder(w http.ResponseWriter) *responseRecorder {
|
|||
|
||||
// WriteHeader records the status code and calls the
|
||||
// underlying ResponseWriter's WriteHeader method.
|
||||
func (r *responseRecorder) WriteHeader(status int) {
|
||||
func (r *ResponseRecorder) WriteHeader(status int) {
|
||||
r.status = status
|
||||
r.ResponseWriter.WriteHeader(status)
|
||||
}
|
||||
|
||||
// Write is a wrapper that records the size of the body
|
||||
// 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)
|
||||
if err == nil {
|
||||
r.size += n
|
||||
|
@ -52,9 +52,19 @@ func (r *responseRecorder) Write(buf []byte) (int, error) {
|
|||
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,
|
||||
// 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 {
|
||||
return hj.Hijack()
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ type replacer struct {
|
|||
// values into the replacer. rr may be nil if it is not
|
||||
// available. emptyValue should be the string that is used
|
||||
// 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{
|
||||
replacements: map[string]string{
|
||||
"{method}": r.Method,
|
||||
|
|
Loading…
Reference in New Issue
Block a user