2019-04-01 05:41:29 +03:00
|
|
|
package caddyhttp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2019-06-21 06:49:45 +03:00
|
|
|
"bytes"
|
2019-04-01 05:41:29 +03:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ResponseWriterWrapper wraps an underlying ResponseWriter and
|
2019-05-11 06:07:02 +03:00
|
|
|
// promotes its Pusher/Flusher/Hijacker methods as well. To use
|
|
|
|
// this type, embed a pointer to it within your own struct type
|
|
|
|
// that implements the http.ResponseWriter interface, then call
|
|
|
|
// methods on the embedded value. You can make sure your type
|
|
|
|
// wraps correctly by asserting that it implements the
|
|
|
|
// HTTPInterfaces interface.
|
2019-04-01 05:41:29 +03:00
|
|
|
type ResponseWriterWrapper struct {
|
|
|
|
http.ResponseWriter
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hijack implements http.Hijacker. It simply calls the underlying
|
2019-06-14 20:58:28 +03:00
|
|
|
// ResponseWriter's Hijack method if there is one, or returns
|
|
|
|
// ErrNotImplemented otherwise.
|
2019-04-01 05:41:29 +03:00
|
|
|
func (rww *ResponseWriterWrapper) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
|
|
|
if hj, ok := rww.ResponseWriter.(http.Hijacker); ok {
|
|
|
|
return hj.Hijack()
|
|
|
|
}
|
2019-06-14 20:58:28 +03:00
|
|
|
return nil, nil, ErrNotImplemented
|
2019-04-01 05:41:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Flush implements http.Flusher. It simply calls the underlying
|
2019-06-14 20:58:28 +03:00
|
|
|
// ResponseWriter's Flush method if there is one.
|
2019-04-01 05:41:29 +03:00
|
|
|
func (rww *ResponseWriterWrapper) Flush() {
|
|
|
|
if f, ok := rww.ResponseWriter.(http.Flusher); ok {
|
|
|
|
f.Flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push implements http.Pusher. It simply calls the underlying
|
2019-06-14 20:58:28 +03:00
|
|
|
// ResponseWriter's Push method if there is one, or returns
|
|
|
|
// ErrNotImplemented otherwise.
|
2019-04-01 05:41:29 +03:00
|
|
|
func (rww *ResponseWriterWrapper) Push(target string, opts *http.PushOptions) error {
|
2019-06-14 20:58:28 +03:00
|
|
|
if pusher, ok := rww.ResponseWriter.(http.Pusher); ok {
|
2019-04-01 05:41:29 +03:00
|
|
|
return pusher.Push(target, opts)
|
|
|
|
}
|
2019-06-14 20:58:28 +03:00
|
|
|
return ErrNotImplemented
|
2019-04-01 05:41:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// HTTPInterfaces mix all the interfaces that middleware ResponseWriters need to support.
|
|
|
|
type HTTPInterfaces interface {
|
|
|
|
http.ResponseWriter
|
|
|
|
http.Pusher
|
|
|
|
http.Flusher
|
|
|
|
http.Hijacker
|
|
|
|
}
|
|
|
|
|
2019-06-14 20:58:28 +03:00
|
|
|
// ErrNotImplemented is returned when an underlying
|
|
|
|
// ResponseWriter does not implement the required method.
|
|
|
|
var ErrNotImplemented = fmt.Errorf("method not implemented")
|
|
|
|
|
2019-06-21 06:49:45 +03:00
|
|
|
type responseRecorder struct {
|
|
|
|
*ResponseWriterWrapper
|
2019-06-21 23:36:26 +03:00
|
|
|
wroteHeader bool
|
|
|
|
statusCode int
|
|
|
|
buf *bytes.Buffer
|
|
|
|
shouldBuffer func(status int) bool
|
|
|
|
stream bool
|
2019-06-21 06:49:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewResponseRecorder returns a new ResponseRecorder that can be
|
|
|
|
// used instead of a real http.ResponseWriter. The recorder is useful
|
|
|
|
// for middlewares which need to buffer a responder's response and
|
|
|
|
// process it in its entirety before actually allowing the response to
|
|
|
|
// be written. Of course, this has a performance overhead, but
|
|
|
|
// sometimes there is no way to avoid buffering the whole response.
|
|
|
|
// Still, if at all practical, middlewares should strive to stream
|
|
|
|
// responses by wrapping Write and WriteHeader methods instead of
|
|
|
|
// buffering whole response bodies.
|
|
|
|
//
|
2019-06-21 23:36:26 +03:00
|
|
|
// Recorders optionally buffer the response. When the headers are
|
|
|
|
// to be written, shouldBuffer will be called with the status
|
|
|
|
// code that is being written. The rest of the headers can be read
|
|
|
|
// from w.Header(). If shouldBuffer returns true, the response
|
|
|
|
// will be buffered. You can know the response was buffered if
|
|
|
|
// the Buffered() method returns true. If the response was not
|
|
|
|
// buffered, Buffered() will return false and that means the
|
|
|
|
// response bypassed the recorder and was written directly to the
|
|
|
|
// underlying writer.
|
|
|
|
//
|
2019-06-21 06:49:45 +03:00
|
|
|
// Before calling this function in a middleware handler, make a
|
|
|
|
// new buffer or obtain one from a pool (use the sync.Pool) type.
|
|
|
|
// Using a pool is generally recommended for performance gains;
|
|
|
|
// do profiling to ensure this is the case. If using a pool, be
|
|
|
|
// sure to reset the buffer before using it.
|
|
|
|
//
|
|
|
|
// The returned recorder can be used in place of w when calling
|
|
|
|
// the next handler in the chain. When that handler returns, you
|
|
|
|
// can read the status code from the recorder's Status() method.
|
2019-06-21 23:36:26 +03:00
|
|
|
// The response body fills buf if it was buffered, and the headers
|
|
|
|
// are available via w.Header().
|
|
|
|
func NewResponseRecorder(w http.ResponseWriter, buf *bytes.Buffer, shouldBuffer func(status int) bool) ResponseRecorder {
|
2019-06-21 06:49:45 +03:00
|
|
|
return &responseRecorder{
|
|
|
|
ResponseWriterWrapper: &ResponseWriterWrapper{ResponseWriter: w},
|
|
|
|
buf: buf,
|
2019-06-21 23:36:26 +03:00
|
|
|
shouldBuffer: shouldBuffer,
|
2019-06-21 06:49:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rr *responseRecorder) WriteHeader(statusCode int) {
|
|
|
|
if rr.wroteHeader {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rr.statusCode = statusCode
|
|
|
|
rr.wroteHeader = true
|
2019-06-21 23:36:26 +03:00
|
|
|
|
|
|
|
// decide whether we should buffer the response
|
|
|
|
if rr.shouldBuffer == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rr.stream = !rr.shouldBuffer(rr.statusCode)
|
|
|
|
if rr.stream {
|
|
|
|
rr.ResponseWriterWrapper.WriteHeader(rr.statusCode)
|
|
|
|
}
|
2019-06-21 06:49:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rr *responseRecorder) Write(data []byte) (int, error) {
|
|
|
|
rr.WriteHeader(http.StatusOK)
|
2019-06-21 23:36:26 +03:00
|
|
|
if rr.stream {
|
|
|
|
return rr.ResponseWriterWrapper.Write(data)
|
|
|
|
}
|
2019-06-21 06:49:45 +03:00
|
|
|
return rr.buf.Write(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Status returns the status code that was written, if any.
|
|
|
|
func (rr *responseRecorder) Status() int {
|
|
|
|
return rr.statusCode
|
|
|
|
}
|
|
|
|
|
|
|
|
// Buffer returns the body buffer that rr was created with.
|
|
|
|
// You should still have your original pointer, though.
|
|
|
|
func (rr *responseRecorder) Buffer() *bytes.Buffer {
|
|
|
|
return rr.buf
|
|
|
|
}
|
|
|
|
|
2019-06-21 23:36:26 +03:00
|
|
|
// Buffered returns whether rr has decided to buffer the response.
|
|
|
|
func (rr *responseRecorder) Buffered() bool {
|
|
|
|
return !rr.stream
|
|
|
|
}
|
|
|
|
|
2019-06-21 06:49:45 +03:00
|
|
|
// ResponseRecorder is a http.ResponseWriter that records
|
|
|
|
// responses instead of writing them to the client.
|
|
|
|
type ResponseRecorder interface {
|
|
|
|
HTTPInterfaces
|
|
|
|
Status() int
|
|
|
|
Buffer() *bytes.Buffer
|
2019-06-21 23:36:26 +03:00
|
|
|
Buffered() bool
|
2019-06-21 06:49:45 +03:00
|
|
|
}
|
|
|
|
|
2019-04-01 05:41:29 +03:00
|
|
|
// Interface guards
|
2019-06-21 06:49:45 +03:00
|
|
|
var (
|
|
|
|
_ HTTPInterfaces = (*ResponseWriterWrapper)(nil)
|
|
|
|
_ ResponseRecorder = (*responseRecorder)(nil)
|
|
|
|
)
|