2018-11-14 09:18:03 +00:00
|
|
|
package customerrors
|
2018-04-11 11:54:03 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2018-11-14 09:18:03 +00:00
|
|
|
"context"
|
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"
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
"github.com/opentracing/opentracing-go/ext"
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/config/dynamic"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/middlewares"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/tracing"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/types"
|
2022-11-21 17:36:05 +00:00
|
|
|
"github.com/vulcand/oxy/v2/utils"
|
2018-04-11 11:54:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Compile time validation that the response recorder implements http interfaces correctly.
|
2019-09-12 14:20:05 +00:00
|
|
|
var (
|
2022-11-16 10:38:07 +00:00
|
|
|
_ middlewares.Stateful = &codeModifier{}
|
|
|
|
_ middlewares.Stateful = &codeCatcher{}
|
2019-09-12 14:20:05 +00:00
|
|
|
)
|
2018-04-11 11:54:03 +00:00
|
|
|
|
2021-09-27 15:40:13 +00:00
|
|
|
const typeName = "customError"
|
2018-11-14 09:18:03 +00:00
|
|
|
|
|
|
|
type serviceBuilder interface {
|
2020-09-01 16:16:04 +00:00
|
|
|
BuildHTTP(ctx context.Context, serviceName string) (http.Handler, error)
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 12:48:05 +00:00
|
|
|
// customErrors is a middleware that provides the custom error pages.
|
2018-11-14 09:18:03 +00:00
|
|
|
type customErrors struct {
|
|
|
|
name string
|
|
|
|
next http.Handler
|
2018-04-11 11:54:03 +00:00
|
|
|
backendHandler http.Handler
|
|
|
|
httpCodeRanges types.HTTPCodeRanges
|
|
|
|
backendQuery string
|
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
// New creates a new custom error pages middleware.
|
2019-07-10 07:26:04 +00:00
|
|
|
func New(ctx context.Context, next http.Handler, config dynamic.ErrorPage, serviceBuilder serviceBuilder, name string) (http.Handler, error) {
|
2022-11-21 17:36:05 +00:00
|
|
|
middlewares.GetLogger(ctx, name, typeName).Debug().Msg("Creating middleware")
|
2018-11-14 09:18:03 +00:00
|
|
|
|
|
|
|
httpCodeRanges, err := types.NewHTTPCodeRanges(config.Status)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-04-11 11:54:03 +00:00
|
|
|
}
|
|
|
|
|
2020-09-01 16:16:04 +00:00
|
|
|
backend, err := serviceBuilder.BuildHTTP(ctx, config.Service)
|
2018-04-11 11:54:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
return &customErrors{
|
|
|
|
name: name,
|
|
|
|
next: next,
|
|
|
|
backendHandler: backend,
|
2018-04-11 11:54:03 +00:00
|
|
|
httpCodeRanges: httpCodeRanges,
|
2018-11-14 09:18:03 +00:00
|
|
|
backendQuery: config.Query,
|
2018-04-11 11:54:03 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
func (c *customErrors) GetTracingInformation() (string, ext.SpanKindEnum) {
|
|
|
|
return c.name, tracing.SpanKindNoneEnum
|
2018-04-11 11:54:03 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
func (c *customErrors) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger := middlewares.GetLogger(req.Context(), c.name, typeName)
|
2018-11-14 09:18:03 +00:00
|
|
|
|
|
|
|
if c.backendHandler == nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Error().Msg("Error pages: no backend handler.")
|
2018-11-14 09:18:03 +00:00
|
|
|
tracing.SetErrorWithEvent(req, "Error pages: no backend handler.")
|
|
|
|
c.next.ServeHTTP(rw, req)
|
2018-04-11 11:54:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-12 14:20:05 +00:00
|
|
|
catcher := newCodeCatcher(rw, c.httpCodeRanges)
|
|
|
|
c.next.ServeHTTP(catcher, req)
|
|
|
|
if !catcher.isFilteredCode() {
|
|
|
|
return
|
|
|
|
}
|
2018-04-11 11:54:03 +00:00
|
|
|
|
|
|
|
// check the recorder code against the configured http status code ranges
|
2019-09-12 14:20:05 +00:00
|
|
|
code := catcher.getCode()
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Debug().Msgf("Caught HTTP Status Code %d, returning error page", code)
|
2018-05-18 14:38:03 +00:00
|
|
|
|
2022-04-20 14:42:09 +00:00
|
|
|
var query string
|
|
|
|
if len(c.backendQuery) > 0 {
|
|
|
|
query = "/" + strings.TrimPrefix(c.backendQuery, "/")
|
|
|
|
query = strings.ReplaceAll(query, "{status}", strconv.Itoa(code))
|
2022-05-10 09:00:09 +00:00
|
|
|
query = strings.ReplaceAll(query, "{url}", url.QueryEscape(req.URL.String()))
|
2022-04-20 14:42:09 +00:00
|
|
|
}
|
2021-03-04 08:02:03 +00:00
|
|
|
|
2022-04-20 14:42:09 +00:00
|
|
|
pageReq, err := newRequest("http://" + req.Host + query)
|
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Error().Err(err).Send()
|
2022-04-20 14:42:09 +00:00
|
|
|
http.Error(rw, http.StatusText(code), code)
|
2021-03-04 08:02:03 +00:00
|
|
|
return
|
2018-04-11 11:54:03 +00:00
|
|
|
}
|
2022-04-20 14:42:09 +00:00
|
|
|
|
|
|
|
utils.CopyHeaders(pageReq.Header, req.Header)
|
|
|
|
|
|
|
|
c.backendHandler.ServeHTTP(newCodeModifier(rw, code),
|
|
|
|
pageReq.WithContext(req.Context()))
|
2018-04-11 11:54:03 +00:00
|
|
|
}
|
|
|
|
|
2018-04-23 09:28:04 +00:00
|
|
|
func newRequest(baseURL string) (*http.Request, error) {
|
|
|
|
u, err := url.Parse(baseURL)
|
|
|
|
if err != nil {
|
2020-05-11 10:06:07 +00:00
|
|
|
return nil, fmt.Errorf("error pages: error when parse URL: %w", err)
|
2018-04-23 09:28:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
|
2018-04-23 09:28:04 +00:00
|
|
|
if err != nil {
|
2020-05-11 10:06:07 +00:00
|
|
|
return nil, fmt.Errorf("error pages: error when create query: %w", err)
|
2018-04-23 09:28:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
req.RequestURI = u.RequestURI()
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
2023-06-14 12:48:05 +00:00
|
|
|
// codeCatcher is a response writer that detects as soon as possible
|
|
|
|
// whether the response is a code within the ranges of codes it watches for.
|
|
|
|
// If it is, it simply drops the data from the response.
|
|
|
|
// Otherwise, it forwards it directly to the original client (its responseWriter) without any buffering.
|
2019-09-12 14:20:05 +00:00
|
|
|
type codeCatcher struct {
|
|
|
|
headerMap http.Header
|
|
|
|
code int
|
|
|
|
httpCodeRanges types.HTTPCodeRanges
|
|
|
|
caughtFilteredCode bool
|
|
|
|
responseWriter http.ResponseWriter
|
|
|
|
headersSent bool
|
|
|
|
}
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
func newCodeCatcher(rw http.ResponseWriter, httpCodeRanges types.HTTPCodeRanges) *codeCatcher {
|
|
|
|
return &codeCatcher{
|
2019-09-12 14:20:05 +00:00
|
|
|
headerMap: make(http.Header),
|
|
|
|
code: http.StatusOK, // If backend does not call WriteHeader on us, we consider it's a 200.
|
|
|
|
responseWriter: rw,
|
|
|
|
httpCodeRanges: httpCodeRanges,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cc *codeCatcher) Header() http.Header {
|
2023-06-14 12:48:05 +00:00
|
|
|
if cc.headersSent {
|
|
|
|
return cc.responseWriter.Header()
|
|
|
|
}
|
|
|
|
|
2019-09-12 14:20:05 +00:00
|
|
|
if cc.headerMap == nil {
|
|
|
|
cc.headerMap = make(http.Header)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cc.headerMap
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cc *codeCatcher) getCode() int {
|
|
|
|
return cc.code
|
|
|
|
}
|
|
|
|
|
|
|
|
// isFilteredCode returns whether the codeCatcher received a response code among the ones it is watching,
|
|
|
|
// and for which the response should be deferred to the error handler.
|
|
|
|
func (cc *codeCatcher) isFilteredCode() bool {
|
|
|
|
return cc.caughtFilteredCode
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cc *codeCatcher) Write(buf []byte) (int, error) {
|
|
|
|
// If WriteHeader was already called from the caller, this is a NOOP.
|
|
|
|
// Otherwise, cc.code is actually a 200 here.
|
|
|
|
cc.WriteHeader(cc.code)
|
|
|
|
|
|
|
|
if cc.caughtFilteredCode {
|
2022-04-21 08:42:08 +00:00
|
|
|
// We don't care about the contents of the response,
|
|
|
|
// since we want to serve the ones from the error page,
|
|
|
|
// so we just drop them.
|
2019-09-12 14:20:05 +00:00
|
|
|
return len(buf), nil
|
|
|
|
}
|
|
|
|
return cc.responseWriter.Write(buf)
|
|
|
|
}
|
|
|
|
|
2023-06-14 15:42:44 +00:00
|
|
|
// WriteHeader is, in the specific case of 1xx status codes, a direct call to the wrapped ResponseWriter, without marking headers as sent,
|
|
|
|
// allowing so further calls.
|
2019-09-12 14:20:05 +00:00
|
|
|
func (cc *codeCatcher) WriteHeader(code int) {
|
|
|
|
if cc.headersSent || cc.caughtFilteredCode {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-14 15:42:44 +00:00
|
|
|
// Handling informational headers.
|
|
|
|
if code >= 100 && code <= 199 {
|
|
|
|
// Multiple informational status codes can be used,
|
|
|
|
// so here the copy is not appending the values to not repeat them.
|
|
|
|
for k, v := range cc.Header() {
|
|
|
|
cc.responseWriter.Header()[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
cc.responseWriter.WriteHeader(code)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-12 14:20:05 +00:00
|
|
|
cc.code = code
|
|
|
|
for _, block := range cc.httpCodeRanges {
|
|
|
|
if cc.code >= block[0] && cc.code <= block[1] {
|
|
|
|
cc.caughtFilteredCode = true
|
2022-04-21 08:42:08 +00:00
|
|
|
// it will be up to the caller to send the headers,
|
|
|
|
// so it is out of our hands now.
|
|
|
|
return
|
2019-09-12 14:20:05 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-21 08:42:08 +00:00
|
|
|
|
2023-06-14 15:42:44 +00:00
|
|
|
// The copy is not appending the values,
|
|
|
|
// to not repeat them in case any informational status code has been written.
|
|
|
|
for k, v := range cc.Header() {
|
|
|
|
cc.responseWriter.Header()[k] = v
|
|
|
|
}
|
2019-09-12 14:20:05 +00:00
|
|
|
cc.responseWriter.WriteHeader(cc.code)
|
|
|
|
cc.headersSent = true
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// Hijack hijacks the connection.
|
2019-09-12 14:20:05 +00:00
|
|
|
func (cc *codeCatcher) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
|
|
|
if hj, ok := cc.responseWriter.(http.Hijacker); ok {
|
|
|
|
return hj.Hijack()
|
|
|
|
}
|
|
|
|
return nil, nil, fmt.Errorf("%T is not a http.Hijacker", cc.responseWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush sends any buffered data to the client.
|
|
|
|
func (cc *codeCatcher) Flush() {
|
|
|
|
// If WriteHeader was already called from the caller, this is a NOOP.
|
|
|
|
// Otherwise, cc.code is actually a 200 here.
|
|
|
|
cc.WriteHeader(cc.code)
|
|
|
|
|
2023-01-02 16:00:05 +00:00
|
|
|
// We don't care about the contents of the response,
|
|
|
|
// since we want to serve the ones from the error page,
|
|
|
|
// so we just don't flush.
|
|
|
|
// (e.g., To prevent superfluous WriteHeader on request with a
|
|
|
|
// `Transfert-Encoding: chunked` header).
|
|
|
|
if cc.caughtFilteredCode {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-12 14:20:05 +00:00
|
|
|
if flusher, ok := cc.responseWriter.(http.Flusher); ok {
|
|
|
|
flusher.Flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 14:42:09 +00:00
|
|
|
// codeModifier forwards a response back to the client,
|
|
|
|
// while enforcing a given response code.
|
2022-11-16 10:38:07 +00:00
|
|
|
type codeModifier struct {
|
2022-04-20 14:42:09 +00:00
|
|
|
code int // the code enforced in the response.
|
|
|
|
|
|
|
|
// headerSent is whether the headers have already been sent,
|
|
|
|
// either through Write or WriteHeader.
|
|
|
|
headerSent bool
|
|
|
|
headerMap http.Header // the HTTP response headers from the backend.
|
|
|
|
|
|
|
|
responseWriter http.ResponseWriter
|
2018-04-11 11:54:03 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
// newCodeModifier returns a codeModifier that enforces the given code.
|
|
|
|
func newCodeModifier(rw http.ResponseWriter, code int) *codeModifier {
|
|
|
|
return &codeModifier{
|
|
|
|
headerMap: make(http.Header),
|
|
|
|
code: code,
|
|
|
|
responseWriter: rw,
|
|
|
|
}
|
2018-04-11 11:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Header returns the response headers.
|
2022-11-16 10:38:07 +00:00
|
|
|
func (r *codeModifier) Header() http.Header {
|
2023-06-14 12:48:05 +00:00
|
|
|
if r.headerSent {
|
|
|
|
return r.responseWriter.Header()
|
|
|
|
}
|
|
|
|
|
2022-04-20 14:42:09 +00:00
|
|
|
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
|
|
|
|
2022-04-20 14:42:09 +00:00
|
|
|
return r.headerMap
|
2018-04-11 11:54:03 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 14:42:09 +00:00
|
|
|
// Write calls WriteHeader to send the enforced code,
|
|
|
|
// then writes the data directly to r.responseWriter.
|
2022-11-16 10:38:07 +00:00
|
|
|
func (r *codeModifier) Write(buf []byte) (int, error) {
|
2022-04-20 14:42:09 +00:00
|
|
|
r.WriteHeader(r.code)
|
|
|
|
return r.responseWriter.Write(buf)
|
2018-04-11 11:54:03 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 12:48:05 +00:00
|
|
|
// WriteHeader sends the headers, with the enforced code (the code in argument is always ignored),
|
|
|
|
// if it hasn't already been done.
|
2023-06-14 15:42:44 +00:00
|
|
|
// WriteHeader is, in the specific case of 1xx status codes, a direct call to the wrapped ResponseWriter, without marking headers as sent,
|
|
|
|
// allowing so further calls.
|
2023-06-20 17:06:51 +00:00
|
|
|
func (r *codeModifier) WriteHeader(code int) {
|
2022-04-20 14:42:09 +00:00
|
|
|
if r.headerSent {
|
|
|
|
return
|
2018-04-11 11:54:03 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 15:42:44 +00:00
|
|
|
// Handling informational headers.
|
|
|
|
if code >= 100 && code <= 199 {
|
|
|
|
// Multiple informational status codes can be used,
|
|
|
|
// so here the copy is not appending the values to not repeat them.
|
|
|
|
for k, v := range r.headerMap {
|
|
|
|
r.responseWriter.Header()[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
r.responseWriter.WriteHeader(code)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range r.headerMap {
|
|
|
|
r.responseWriter.Header()[k] = v
|
|
|
|
}
|
2022-04-20 14:42:09 +00:00
|
|
|
r.responseWriter.WriteHeader(r.code)
|
|
|
|
r.headerSent = true
|
2018-04-11 11:54:03 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// Hijack hijacks the connection.
|
2022-11-16 10:38:07 +00:00
|
|
|
func (r *codeModifier) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
2022-04-20 14:42:09 +00:00
|
|
|
hijacker, ok := r.responseWriter.(http.Hijacker)
|
|
|
|
if !ok {
|
|
|
|
return nil, nil, fmt.Errorf("%T is not a http.Hijacker", r.responseWriter)
|
|
|
|
}
|
|
|
|
return hijacker.Hijack()
|
2018-04-11 11:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Flush sends any buffered data to the client.
|
2022-11-16 10:38:07 +00:00
|
|
|
func (r *codeModifier) Flush() {
|
2022-04-21 08:42:08 +00:00
|
|
|
r.WriteHeader(r.code)
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|