2018-04-11 11:54:03 +00:00
|
|
|
package errorpages
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2018-04-23 14:20:05 +00:00
|
|
|
"errors"
|
2018-04-23 09:28:04 +00:00
|
|
|
"fmt"
|
2018-04-11 11:54:03 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2018-04-23 09:28:04 +00:00
|
|
|
"net/url"
|
2018-04-11 11:54:03 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/containous/traefik/log"
|
|
|
|
"github.com/containous/traefik/middlewares"
|
|
|
|
"github.com/containous/traefik/types"
|
|
|
|
"github.com/vulcand/oxy/forward"
|
|
|
|
"github.com/vulcand/oxy/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Compile time validation that the response recorder implements http interfaces correctly.
|
|
|
|
var _ middlewares.Stateful = &responseRecorderWithCloseNotify{}
|
|
|
|
|
|
|
|
// Handler is a middleware that provides the custom error pages
|
|
|
|
type Handler struct {
|
|
|
|
BackendName string
|
|
|
|
backendHandler http.Handler
|
|
|
|
httpCodeRanges types.HTTPCodeRanges
|
|
|
|
backendURL string
|
|
|
|
backendQuery string
|
|
|
|
FallbackURL string // Deprecated
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewHandler initializes the utils.ErrorHandler for the custom error pages
|
|
|
|
func NewHandler(errorPage *types.ErrorPage, backendName string) (*Handler, error) {
|
|
|
|
if len(backendName) == 0 {
|
|
|
|
return nil, errors.New("error pages: backend name is mandatory ")
|
|
|
|
}
|
|
|
|
|
|
|
|
httpCodeRanges, err := types.NewHTTPCodeRanges(errorPage.Status)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Handler{
|
|
|
|
BackendName: backendName,
|
|
|
|
httpCodeRanges: httpCodeRanges,
|
|
|
|
backendQuery: errorPage.Query,
|
|
|
|
backendURL: "http://0.0.0.0",
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PostLoad adds backend handler if available
|
|
|
|
func (h *Handler) PostLoad(backendHandler http.Handler) error {
|
|
|
|
if backendHandler == nil {
|
|
|
|
fwd, err := forward.New()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
h.backendHandler = fwd
|
|
|
|
h.backendURL = h.FallbackURL
|
|
|
|
} else {
|
|
|
|
h.backendHandler = backendHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
|
|
|
|
if h.backendHandler == nil {
|
|
|
|
log.Error("Error pages: no backend handler.")
|
|
|
|
next.ServeHTTP(w, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
recorder := newResponseRecorder(w)
|
|
|
|
next.ServeHTTP(recorder, req)
|
|
|
|
|
|
|
|
// check the recorder code against the configured http status code ranges
|
|
|
|
for _, block := range h.httpCodeRanges {
|
|
|
|
if recorder.GetCode() >= block[0] && recorder.GetCode() <= block[1] {
|
|
|
|
log.Errorf("Caught HTTP Status Code %d, returning error page", recorder.GetCode())
|
|
|
|
|
|
|
|
var query string
|
|
|
|
if len(h.backendQuery) > 0 {
|
|
|
|
query = "/" + strings.TrimPrefix(h.backendQuery, "/")
|
|
|
|
query = strings.Replace(query, "{status}", strconv.Itoa(recorder.GetCode()), -1)
|
|
|
|
}
|
|
|
|
|
2018-04-23 09:28:04 +00:00
|
|
|
pageReq, err := newRequest(h.backendURL + query)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
w.WriteHeader(recorder.GetCode())
|
2018-05-18 14:38:03 +00:00
|
|
|
fmt.Fprint(w, http.StatusText(recorder.GetCode()))
|
2018-04-23 09:28:04 +00:00
|
|
|
return
|
2018-04-11 11:54:03 +00:00
|
|
|
}
|
2018-04-23 09:28:04 +00:00
|
|
|
|
2018-05-18 14:38:03 +00:00
|
|
|
recorderErrorPage := newResponseRecorder(w)
|
2018-04-23 09:28:04 +00:00
|
|
|
utils.CopyHeaders(pageReq.Header, req.Header)
|
2018-05-18 14:38:03 +00:00
|
|
|
|
|
|
|
h.backendHandler.ServeHTTP(recorderErrorPage, pageReq.WithContext(req.Context()))
|
|
|
|
|
|
|
|
utils.CopyHeaders(w.Header(), recorderErrorPage.Header())
|
|
|
|
w.WriteHeader(recorder.GetCode())
|
2018-08-06 18:00:03 +00:00
|
|
|
|
|
|
|
if _, err = w.Write(recorderErrorPage.GetBody().Bytes()); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
2018-04-11 11:54:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// did not catch a configured status code so proceed with the request
|
|
|
|
utils.CopyHeaders(w.Header(), recorder.Header())
|
2018-04-23 09:28:04 +00:00
|
|
|
w.WriteHeader(recorder.GetCode())
|
2018-04-11 11:54:03 +00:00
|
|
|
w.Write(recorder.GetBody().Bytes())
|
|
|
|
}
|
|
|
|
|
2018-04-23 09:28:04 +00:00
|
|
|
func newRequest(baseURL string) (*http.Request, error) {
|
|
|
|
u, err := url.Parse(baseURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error pages: error when parse URL: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-10-23 08:10:04 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, u.String(), http.NoBody)
|
2018-04-23 09:28:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error pages: error when create query: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.RequestURI = u.RequestURI()
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
2018-04-11 11:54:03 +00:00
|
|
|
type responseRecorder interface {
|
|
|
|
http.ResponseWriter
|
|
|
|
http.Flusher
|
|
|
|
GetCode() int
|
|
|
|
GetBody() *bytes.Buffer
|
|
|
|
IsStreamingResponseStarted() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// newResponseRecorder returns an initialized responseRecorder.
|
|
|
|
func newResponseRecorder(rw http.ResponseWriter) responseRecorder {
|
|
|
|
recorder := &responseRecorderWithoutCloseNotify{
|
|
|
|
HeaderMap: make(http.Header),
|
|
|
|
Body: new(bytes.Buffer),
|
|
|
|
Code: http.StatusOK,
|
|
|
|
responseWriter: rw,
|
|
|
|
}
|
|
|
|
if _, ok := rw.(http.CloseNotifier); ok {
|
|
|
|
return &responseRecorderWithCloseNotify{recorder}
|
|
|
|
}
|
|
|
|
return recorder
|
|
|
|
}
|
|
|
|
|
|
|
|
// responseRecorderWithoutCloseNotify is an implementation of http.ResponseWriter that
|
|
|
|
// records its mutations for later inspection.
|
|
|
|
type responseRecorderWithoutCloseNotify struct {
|
|
|
|
Code int // the HTTP response code from WriteHeader
|
|
|
|
HeaderMap http.Header // the HTTP response headers
|
|
|
|
Body *bytes.Buffer // if non-nil, the bytes.Buffer to append written data to
|
|
|
|
|
|
|
|
responseWriter http.ResponseWriter
|
|
|
|
err error
|
|
|
|
streamingResponseStarted bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type responseRecorderWithCloseNotify struct {
|
|
|
|
*responseRecorderWithoutCloseNotify
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloseNotify returns a channel that receives at most a
|
|
|
|
// single value (true) when the client connection has gone away.
|
2018-05-28 13:00:04 +00:00
|
|
|
func (r *responseRecorderWithCloseNotify) CloseNotify() <-chan bool {
|
|
|
|
return r.responseWriter.(http.CloseNotifier).CloseNotify()
|
2018-04-11 11:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Header returns the response headers.
|
2018-05-28 13:00:04 +00:00
|
|
|
func (r *responseRecorderWithoutCloseNotify) Header() http.Header {
|
|
|
|
if r.HeaderMap == nil {
|
|
|
|
r.HeaderMap = make(http.Header)
|
2018-04-11 11:54:03 +00:00
|
|
|
}
|
2018-05-28 13:00:04 +00:00
|
|
|
|
|
|
|
return r.HeaderMap
|
2018-04-11 11:54:03 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 13:00:04 +00:00
|
|
|
func (r *responseRecorderWithoutCloseNotify) GetCode() int {
|
|
|
|
return r.Code
|
2018-04-11 11:54:03 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 13:00:04 +00:00
|
|
|
func (r *responseRecorderWithoutCloseNotify) GetBody() *bytes.Buffer {
|
|
|
|
return r.Body
|
2018-04-11 11:54:03 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 13:00:04 +00:00
|
|
|
func (r *responseRecorderWithoutCloseNotify) IsStreamingResponseStarted() bool {
|
|
|
|
return r.streamingResponseStarted
|
2018-04-11 11:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write always succeeds and writes to rw.Body, if not nil.
|
2018-05-28 13:00:04 +00:00
|
|
|
func (r *responseRecorderWithoutCloseNotify) Write(buf []byte) (int, error) {
|
|
|
|
if r.err != nil {
|
|
|
|
return 0, r.err
|
2018-04-11 11:54:03 +00:00
|
|
|
}
|
2018-05-28 13:00:04 +00:00
|
|
|
return r.Body.Write(buf)
|
2018-04-11 11:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteHeader sets rw.Code.
|
2018-05-28 13:00:04 +00:00
|
|
|
func (r *responseRecorderWithoutCloseNotify) WriteHeader(code int) {
|
|
|
|
r.Code = code
|
2018-04-11 11:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hijack hijacks the connection
|
2018-05-28 13:00:04 +00:00
|
|
|
func (r *responseRecorderWithoutCloseNotify) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
|
|
|
return r.responseWriter.(http.Hijacker).Hijack()
|
2018-04-11 11:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Flush sends any buffered data to the client.
|
2018-05-28 13:00:04 +00:00
|
|
|
func (r *responseRecorderWithoutCloseNotify) Flush() {
|
|
|
|
if !r.streamingResponseStarted {
|
|
|
|
utils.CopyHeaders(r.responseWriter.Header(), r.Header())
|
|
|
|
r.responseWriter.WriteHeader(r.Code)
|
|
|
|
r.streamingResponseStarted = true
|
2018-04-11 11:54:03 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 13:00:04 +00:00
|
|
|
_, err := r.responseWriter.Write(r.Body.Bytes())
|
2018-04-11 11:54:03 +00:00
|
|
|
if err != nil {
|
2018-05-28 13:00:04 +00:00
|
|
|
log.Errorf("Error writing response in responseRecorder: %v", err)
|
|
|
|
r.err = err
|
2018-04-11 11:54:03 +00:00
|
|
|
}
|
2018-05-28 13:00:04 +00:00
|
|
|
r.Body.Reset()
|
2018-04-11 11:54:03 +00:00
|
|
|
|
2018-05-28 13:00:04 +00:00
|
|
|
if flusher, ok := r.responseWriter.(http.Flusher); ok {
|
2018-04-11 11:54:03 +00:00
|
|
|
flusher.Flush()
|
|
|
|
}
|
|
|
|
}
|