mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-16 21:56:40 -05:00
caddyhttp: Make responseRecorder capable of counting body size
This commit is contained in:
parent
411152016e
commit
5300949e0d
1 changed files with 23 additions and 5 deletions
|
@ -80,6 +80,7 @@ type responseRecorder struct {
|
||||||
buf *bytes.Buffer
|
buf *bytes.Buffer
|
||||||
shouldBuffer func(status int) bool
|
shouldBuffer func(status int) bool
|
||||||
stream bool
|
stream bool
|
||||||
|
size int
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewResponseRecorder returns a new ResponseRecorder that can be
|
// NewResponseRecorder returns a new ResponseRecorder that can be
|
||||||
|
@ -100,7 +101,9 @@ type responseRecorder struct {
|
||||||
// the Buffered() method returns true. If the response was not
|
// the Buffered() method returns true. If the response was not
|
||||||
// buffered, Buffered() will return false and that means the
|
// buffered, Buffered() will return false and that means the
|
||||||
// response bypassed the recorder and was written directly to the
|
// response bypassed the recorder and was written directly to the
|
||||||
// underlying writer.
|
// underlying writer. If shouldBuffer is nil, the response will
|
||||||
|
// never be buffered (it will always be streamed directly), and
|
||||||
|
// buf can also safely be nil.
|
||||||
//
|
//
|
||||||
// Before calling this function in a middleware handler, make a
|
// Before calling this function in a middleware handler, make a
|
||||||
// new buffer or obtain one from a pool (use the sync.Pool) type.
|
// new buffer or obtain one from a pool (use the sync.Pool) type.
|
||||||
|
@ -130,9 +133,10 @@ func (rr *responseRecorder) WriteHeader(statusCode int) {
|
||||||
|
|
||||||
// decide whether we should buffer the response
|
// decide whether we should buffer the response
|
||||||
if rr.shouldBuffer == nil {
|
if rr.shouldBuffer == nil {
|
||||||
return
|
rr.stream = true
|
||||||
|
} else {
|
||||||
|
rr.stream = !rr.shouldBuffer(rr.statusCode)
|
||||||
}
|
}
|
||||||
rr.stream = !rr.shouldBuffer(rr.statusCode)
|
|
||||||
if rr.stream {
|
if rr.stream {
|
||||||
rr.ResponseWriterWrapper.WriteHeader(rr.statusCode)
|
rr.ResponseWriterWrapper.WriteHeader(rr.statusCode)
|
||||||
}
|
}
|
||||||
|
@ -140,10 +144,17 @@ func (rr *responseRecorder) WriteHeader(statusCode int) {
|
||||||
|
|
||||||
func (rr *responseRecorder) Write(data []byte) (int, error) {
|
func (rr *responseRecorder) Write(data []byte) (int, error) {
|
||||||
rr.WriteHeader(http.StatusOK)
|
rr.WriteHeader(http.StatusOK)
|
||||||
|
var n int
|
||||||
|
var err error
|
||||||
if rr.stream {
|
if rr.stream {
|
||||||
return rr.ResponseWriterWrapper.Write(data)
|
n, err = rr.ResponseWriterWrapper.Write(data)
|
||||||
|
} else {
|
||||||
|
n, err = rr.buf.Write(data)
|
||||||
}
|
}
|
||||||
return rr.buf.Write(data)
|
if err == nil {
|
||||||
|
rr.size += n
|
||||||
|
}
|
||||||
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Status returns the status code that was written, if any.
|
// Status returns the status code that was written, if any.
|
||||||
|
@ -151,6 +162,12 @@ func (rr *responseRecorder) Status() int {
|
||||||
return rr.statusCode
|
return rr.statusCode
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Size returns the number of bytes written,
|
||||||
|
// not including the response headers.
|
||||||
|
func (rr *responseRecorder) Size() int {
|
||||||
|
return rr.size
|
||||||
|
}
|
||||||
|
|
||||||
// Buffer returns the body buffer that rr was created with.
|
// Buffer returns the body buffer that rr was created with.
|
||||||
// You should still have your original pointer, though.
|
// You should still have your original pointer, though.
|
||||||
func (rr *responseRecorder) Buffer() *bytes.Buffer {
|
func (rr *responseRecorder) Buffer() *bytes.Buffer {
|
||||||
|
@ -169,6 +186,7 @@ type ResponseRecorder interface {
|
||||||
Status() int
|
Status() int
|
||||||
Buffer() *bytes.Buffer
|
Buffer() *bytes.Buffer
|
||||||
Buffered() bool
|
Buffered() bool
|
||||||
|
Size() int
|
||||||
}
|
}
|
||||||
|
|
||||||
// Interface guards
|
// Interface guards
|
||||||
|
|
Loading…
Reference in a new issue