2019-07-01 06:07:58 +08:00
|
|
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-04-01 10:41:29 +08:00
|
|
|
package caddyhttp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2019-06-21 11:49:45 +08:00
|
|
|
"bytes"
|
2019-04-01 10:41:29 +08:00
|
|
|
"fmt"
|
2019-10-16 04:07:10 +08:00
|
|
|
"io"
|
2019-04-01 10:41:29 +08:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ResponseWriterWrapper wraps an underlying ResponseWriter and
|
2019-05-11 11:07:02 +08: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 10:41:29 +08:00
|
|
|
type ResponseWriterWrapper struct {
|
|
|
|
http.ResponseWriter
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hijack implements http.Hijacker. It simply calls the underlying
|
2019-06-15 01:58:28 +08:00
|
|
|
// ResponseWriter's Hijack method if there is one, or returns
|
|
|
|
// ErrNotImplemented otherwise.
|
2019-04-01 10:41:29 +08:00
|
|
|
func (rww *ResponseWriterWrapper) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
|
|
|
if hj, ok := rww.ResponseWriter.(http.Hijacker); ok {
|
|
|
|
return hj.Hijack()
|
|
|
|
}
|
2019-06-15 01:58:28 +08:00
|
|
|
return nil, nil, ErrNotImplemented
|
2019-04-01 10:41:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Flush implements http.Flusher. It simply calls the underlying
|
2019-06-15 01:58:28 +08:00
|
|
|
// ResponseWriter's Flush method if there is one.
|
2019-04-01 10:41:29 +08: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-15 01:58:28 +08:00
|
|
|
// ResponseWriter's Push method if there is one, or returns
|
|
|
|
// ErrNotImplemented otherwise.
|
2019-04-01 10:41:29 +08:00
|
|
|
func (rww *ResponseWriterWrapper) Push(target string, opts *http.PushOptions) error {
|
2019-06-15 01:58:28 +08:00
|
|
|
if pusher, ok := rww.ResponseWriter.(http.Pusher); ok {
|
2019-04-01 10:41:29 +08:00
|
|
|
return pusher.Push(target, opts)
|
|
|
|
}
|
2019-06-15 01:58:28 +08:00
|
|
|
return ErrNotImplemented
|
2019-04-01 10:41:29 +08: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-15 01:58:28 +08:00
|
|
|
// ErrNotImplemented is returned when an underlying
|
|
|
|
// ResponseWriter does not implement the required method.
|
|
|
|
var ErrNotImplemented = fmt.Errorf("method not implemented")
|
|
|
|
|
2019-06-21 11:49:45 +08:00
|
|
|
type responseRecorder struct {
|
|
|
|
*ResponseWriterWrapper
|
2019-06-22 04:36:26 +08:00
|
|
|
wroteHeader bool
|
|
|
|
statusCode int
|
|
|
|
buf *bytes.Buffer
|
2019-10-16 04:07:10 +08:00
|
|
|
shouldBuffer ShouldBufferFunc
|
2019-06-22 04:36:26 +08:00
|
|
|
stream bool
|
2019-10-11 05:36:28 +08:00
|
|
|
size int
|
2019-10-16 04:07:10 +08:00
|
|
|
header http.Header
|
2019-06-21 11:49:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewResponseRecorder returns a new ResponseRecorder that can be
|
2019-10-16 04:07:10 +08:00
|
|
|
// used instead of a standard http.ResponseWriter. The recorder is
|
|
|
|
// useful for middlewares which need to buffer a response and
|
|
|
|
// potentially process its entire body before actually writing the
|
|
|
|
// response to the underlying writer. Of course, buffering the entire
|
|
|
|
// body has a memory overhead, but sometimes there is no way to avoid
|
|
|
|
// buffering the whole response, hence the existence of this type.
|
|
|
|
// Still, if at all practical, handlers should strive to stream
|
2019-06-21 11:49:45 +08:00
|
|
|
// responses by wrapping Write and WriteHeader methods instead of
|
|
|
|
// buffering whole response bodies.
|
|
|
|
//
|
2019-10-16 04:07:10 +08:00
|
|
|
// Buffering is actually optional. The shouldBuffer function will
|
|
|
|
// be called just before the headers are written. If it returns
|
|
|
|
// true, the headers and body will be buffered by this recorder
|
|
|
|
// and not written to the underlying writer; if false, the headers
|
|
|
|
// will be written immediately and the body will be streamed out
|
|
|
|
// directly to the underlying writer. If shouldBuffer is nil,
|
|
|
|
// the response will never be buffered and will always be streamed
|
|
|
|
// directly to the writer.
|
2019-06-22 04:36:26 +08:00
|
|
|
//
|
2019-10-16 04:07:10 +08:00
|
|
|
// You can know if shouldBuffer returned true by calling Buffered().
|
2019-06-21 11:49:45 +08:00
|
|
|
//
|
2019-10-16 04:07:10 +08:00
|
|
|
// The provided buffer buf should be obtained from a pool for best
|
|
|
|
// performance (see the sync.Pool type).
|
|
|
|
//
|
|
|
|
// Proper usage of a recorder looks like this:
|
|
|
|
//
|
|
|
|
// rec := caddyhttp.NewResponseRecorder(w, buf, shouldBuffer)
|
|
|
|
// err := next.ServeHTTP(rec, req)
|
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
// if !rec.Buffered() {
|
|
|
|
// return nil
|
|
|
|
// }
|
|
|
|
// // process the buffered response here
|
|
|
|
//
|
|
|
|
// After a response has been buffered, remember that any upstream header
|
|
|
|
// manipulations are only manifest in the recorder's Header(), not the
|
|
|
|
// Header() of the underlying ResponseWriter. Thus if you wish to inspect
|
|
|
|
// or change response headers, you either need to use rec.Header(), or
|
|
|
|
// copy rec.Header() into w.Header() first (see caddyhttp.CopyHeader).
|
|
|
|
//
|
|
|
|
// Once you are ready to write the response, there are two ways you can do
|
|
|
|
// it. The easier way is to have the recorder do it:
|
|
|
|
//
|
|
|
|
// rec.WriteResponse()
|
|
|
|
//
|
|
|
|
// This writes the recorded response headers as well as the buffered body.
|
|
|
|
// Or, you may wish to do it yourself, especially if you manipulated the
|
|
|
|
// buffered body. First you will need to copy the recorded headers, then
|
|
|
|
// write the headers with the recorded status code, then write the body
|
|
|
|
// (this example writes the recorder's body buffer, but you might have
|
|
|
|
// your own body to write instead):
|
|
|
|
//
|
|
|
|
// caddyhttp.CopyHeader(w.Header(), rec.Header())
|
|
|
|
// w.WriteHeader(rec.Status())
|
|
|
|
// io.Copy(w, rec.Buffer())
|
|
|
|
//
|
|
|
|
func NewResponseRecorder(w http.ResponseWriter, buf *bytes.Buffer, shouldBuffer ShouldBufferFunc) ResponseRecorder {
|
|
|
|
// copy the current response header into this buffer so
|
|
|
|
// that any header manipulations on the buffered header
|
|
|
|
// are consistent with what would be written out
|
|
|
|
hdr := make(http.Header)
|
|
|
|
CopyHeader(hdr, w.Header())
|
2019-06-21 11:49:45 +08:00
|
|
|
return &responseRecorder{
|
|
|
|
ResponseWriterWrapper: &ResponseWriterWrapper{ResponseWriter: w},
|
|
|
|
buf: buf,
|
2019-06-22 04:36:26 +08:00
|
|
|
shouldBuffer: shouldBuffer,
|
2019-10-16 04:07:10 +08:00
|
|
|
header: hdr,
|
2019-06-21 11:49:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-16 04:07:10 +08:00
|
|
|
func (rr *responseRecorder) Header() http.Header {
|
|
|
|
return rr.header
|
|
|
|
}
|
|
|
|
|
2019-06-21 11:49:45 +08:00
|
|
|
func (rr *responseRecorder) WriteHeader(statusCode int) {
|
|
|
|
if rr.wroteHeader {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rr.statusCode = statusCode
|
|
|
|
rr.wroteHeader = true
|
2019-06-22 04:36:26 +08:00
|
|
|
|
|
|
|
// decide whether we should buffer the response
|
|
|
|
if rr.shouldBuffer == nil {
|
2019-10-11 05:36:28 +08:00
|
|
|
rr.stream = true
|
|
|
|
} else {
|
2019-10-16 04:07:10 +08:00
|
|
|
rr.stream = !rr.shouldBuffer(rr.statusCode, rr.header)
|
2019-06-22 04:36:26 +08:00
|
|
|
}
|
2019-10-16 04:07:10 +08:00
|
|
|
|
|
|
|
// if not buffered, immediately write header
|
2019-06-22 04:36:26 +08:00
|
|
|
if rr.stream {
|
2019-10-16 04:07:10 +08:00
|
|
|
CopyHeader(rr.ResponseWriterWrapper.Header(), rr.header)
|
2019-06-22 04:36:26 +08:00
|
|
|
rr.ResponseWriterWrapper.WriteHeader(rr.statusCode)
|
|
|
|
}
|
2019-06-21 11:49:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rr *responseRecorder) Write(data []byte) (int, error) {
|
|
|
|
rr.WriteHeader(http.StatusOK)
|
2019-10-11 05:36:28 +08:00
|
|
|
var n int
|
|
|
|
var err error
|
2019-06-22 04:36:26 +08:00
|
|
|
if rr.stream {
|
2019-10-11 05:36:28 +08:00
|
|
|
n, err = rr.ResponseWriterWrapper.Write(data)
|
|
|
|
} else {
|
|
|
|
n, err = rr.buf.Write(data)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
rr.size += n
|
2019-06-22 04:36:26 +08:00
|
|
|
}
|
2019-10-11 05:36:28 +08:00
|
|
|
return n, err
|
2019-06-21 11:49:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Status returns the status code that was written, if any.
|
|
|
|
func (rr *responseRecorder) Status() int {
|
|
|
|
return rr.statusCode
|
|
|
|
}
|
|
|
|
|
2019-10-11 05:36:28 +08:00
|
|
|
// Size returns the number of bytes written,
|
|
|
|
// not including the response headers.
|
|
|
|
func (rr *responseRecorder) Size() int {
|
|
|
|
return rr.size
|
|
|
|
}
|
|
|
|
|
2019-06-21 11:49:45 +08:00
|
|
|
// 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-22 04:36:26 +08:00
|
|
|
// Buffered returns whether rr has decided to buffer the response.
|
|
|
|
func (rr *responseRecorder) Buffered() bool {
|
|
|
|
return !rr.stream
|
|
|
|
}
|
|
|
|
|
2019-10-16 04:07:10 +08:00
|
|
|
func (rr *responseRecorder) WriteResponse() error {
|
|
|
|
if rr.stream {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
CopyHeader(rr.ResponseWriterWrapper.Header(), rr.header)
|
|
|
|
rr.ResponseWriterWrapper.WriteHeader(rr.statusCode)
|
|
|
|
_, err := io.Copy(rr.ResponseWriterWrapper, rr.buf)
|
|
|
|
return err
|
|
|
|
}
|
2019-06-21 11:49:45 +08:00
|
|
|
// ResponseRecorder is a http.ResponseWriter that records
|
2019-10-16 04:07:10 +08:00
|
|
|
// responses instead of writing them to the client. See
|
|
|
|
// docs for NewResponseRecorder for proper usage.
|
2019-06-21 11:49:45 +08:00
|
|
|
type ResponseRecorder interface {
|
|
|
|
HTTPInterfaces
|
|
|
|
Status() int
|
|
|
|
Buffer() *bytes.Buffer
|
2019-06-22 04:36:26 +08:00
|
|
|
Buffered() bool
|
2019-10-11 05:36:28 +08:00
|
|
|
Size() int
|
2019-10-16 04:07:10 +08:00
|
|
|
WriteResponse() error
|
2019-06-21 11:49:45 +08:00
|
|
|
}
|
|
|
|
|
2019-10-16 04:07:10 +08:00
|
|
|
// ShouldBufferFunc is a function that returns true if the
|
|
|
|
// response should be buffered, given the pending HTTP status
|
|
|
|
// code and response headers.
|
|
|
|
type ShouldBufferFunc func(status int, header http.Header) bool
|
|
|
|
|
2019-04-01 10:41:29 +08:00
|
|
|
// Interface guards
|
2019-06-21 11:49:45 +08:00
|
|
|
var (
|
|
|
|
_ HTTPInterfaces = (*ResponseWriterWrapper)(nil)
|
|
|
|
_ ResponseRecorder = (*responseRecorder)(nil)
|
|
|
|
)
|