1
0
mirror of https://github.com/containous/traefik.git synced 2024-12-23 17:34:13 +03:00

Fix error pages redirect and headers.

This commit is contained in:
Ludovic Fernandez 2018-04-23 11:28:04 +02:00 committed by Traefiker Bot
parent d8e2d464ad
commit 76dcbe3429
2 changed files with 32 additions and 7 deletions

View File

@ -3,8 +3,10 @@ package errorpages
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"fmt"
"net" "net"
"net/http" "net/http"
"net/url"
"strconv" "strconv"
"strings" "strings"
@ -75,8 +77,6 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request, next http.
recorder := newResponseRecorder(w) recorder := newResponseRecorder(w)
next.ServeHTTP(recorder, req) next.ServeHTTP(recorder, req)
w.WriteHeader(recorder.GetCode())
// check the recorder code against the configured http status code ranges // check the recorder code against the configured http status code ranges
for _, block := range h.httpCodeRanges { for _, block := range h.httpCodeRanges {
if recorder.GetCode() >= block[0] && recorder.GetCode() <= block[1] { if recorder.GetCode() >= block[0] && recorder.GetCode() <= block[1] {
@ -88,20 +88,43 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request, next http.
query = strings.Replace(query, "{status}", strconv.Itoa(recorder.GetCode()), -1) query = strings.Replace(query, "{status}", strconv.Itoa(recorder.GetCode()), -1)
} }
if newReq, err := http.NewRequest(http.MethodGet, h.backendURL+query, nil); err != nil { pageReq, err := newRequest(h.backendURL + query)
if err != nil {
log.Error(err)
w.WriteHeader(recorder.GetCode())
w.Write([]byte(http.StatusText(recorder.GetCode()))) w.Write([]byte(http.StatusText(recorder.GetCode())))
} else { return
h.backendHandler.ServeHTTP(w, newReq)
} }
utils.CopyHeaders(pageReq.Header, req.Header)
utils.CopyHeaders(w.Header(), recorder.Header())
w.WriteHeader(recorder.GetCode())
h.backendHandler.ServeHTTP(w, pageReq)
return return
} }
} }
// did not catch a configured status code so proceed with the request // did not catch a configured status code so proceed with the request
utils.CopyHeaders(w.Header(), recorder.Header()) utils.CopyHeaders(w.Header(), recorder.Header())
w.WriteHeader(recorder.GetCode())
w.Write(recorder.GetBody().Bytes()) w.Write(recorder.GetBody().Bytes())
} }
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)
}
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return nil, fmt.Errorf("error pages: error when create query: %v", err)
}
req.RequestURI = u.RequestURI()
return req, nil
}
type responseRecorder interface { type responseRecorder interface {
http.ResponseWriter http.ResponseWriter
http.Flusher http.Flusher

View File

@ -65,7 +65,7 @@ func TestHandler(t *testing.T) {
errorPage: &types.ErrorPage{Backend: "error", Query: "/{status}", Status: []string{"503-503"}}, errorPage: &types.ErrorPage{Backend: "error", Query: "/{status}", Status: []string{"503-503"}},
backendCode: http.StatusServiceUnavailable, backendCode: http.StatusServiceUnavailable,
backendErrorHandler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { backendErrorHandler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.RequestURI() == "/"+strconv.Itoa(503) { if r.RequestURI == "/503" {
fmt.Fprintln(w, "My 503 page.") fmt.Fprintln(w, "My 503 page.")
} else { } else {
fmt.Fprintln(w, "Failed") fmt.Fprintln(w, "Failed")
@ -82,7 +82,7 @@ func TestHandler(t *testing.T) {
errorPage: &types.ErrorPage{Backend: "error", Query: "/{status}", Status: []string{"503"}}, errorPage: &types.ErrorPage{Backend: "error", Query: "/{status}", Status: []string{"503"}},
backendCode: http.StatusServiceUnavailable, backendCode: http.StatusServiceUnavailable,
backendErrorHandler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { backendErrorHandler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.RequestURI() == "/"+strconv.Itoa(503) { if r.RequestURI == "/503" {
fmt.Fprintln(w, "My 503 page.") fmt.Fprintln(w, "My 503 page.")
} else { } else {
fmt.Fprintln(w, "Failed") fmt.Fprintln(w, "Failed")
@ -318,6 +318,7 @@ func TestHandlerOldWayIntegration(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Foo", "bar")
w.WriteHeader(test.backendCode) w.WriteHeader(test.backendCode)
fmt.Fprintln(w, http.StatusText(test.backendCode)) fmt.Fprintln(w, http.StatusText(test.backendCode))
}) })
@ -330,6 +331,7 @@ func TestHandlerOldWayIntegration(t *testing.T) {
n.ServeHTTP(recorder, req) n.ServeHTTP(recorder, req)
test.validate(t, recorder) test.validate(t, recorder)
assert.Equal(t, "bar", recorder.Header().Get("X-Foo"), "missing header")
}) })
} }
} }