parent
2c976227dd
commit
121c057b90
10 changed files with 411 additions and 9 deletions
77
middlewares/error_pages.go
Normal file
77
middlewares/error_pages.go
Normal file
|
@ -0,0 +1,77 @@
|
|||
package middlewares
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/containous/traefik/log"
|
||||
"github.com/containous/traefik/types"
|
||||
"github.com/vulcand/oxy/forward"
|
||||
"github.com/vulcand/oxy/utils"
|
||||
)
|
||||
|
||||
//ErrorPagesHandler is a middleware that provides the custom error pages
|
||||
type ErrorPagesHandler struct {
|
||||
HTTPCodeRanges [][2]int
|
||||
BackendURL string
|
||||
errorPageForwarder *forward.Forwarder
|
||||
}
|
||||
|
||||
//NewErrorPagesHandler initializes the utils.ErrorHandler for the custom error pages
|
||||
func NewErrorPagesHandler(errorPage types.ErrorPage, backendURL string) (*ErrorPagesHandler, error) {
|
||||
fwd, err := forward.New()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//Break out the http status code ranges into a low int and high int
|
||||
//for ease of use at runtime
|
||||
var blocks [][2]int
|
||||
for _, block := range errorPage.Status {
|
||||
codes := strings.Split(block, "-")
|
||||
//if only a single HTTP code was configured, assume the best and create the correct configuration on the user's behalf
|
||||
if len(codes) == 1 {
|
||||
codes = append(codes, codes[0])
|
||||
}
|
||||
lowCode, err := strconv.Atoi(codes[0])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
highCode, err := strconv.Atoi(codes[1])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
blocks = append(blocks, [2]int{lowCode, highCode})
|
||||
}
|
||||
return &ErrorPagesHandler{
|
||||
HTTPCodeRanges: blocks,
|
||||
BackendURL: backendURL + errorPage.Query,
|
||||
errorPageForwarder: fwd},
|
||||
nil
|
||||
}
|
||||
|
||||
func (ep *ErrorPagesHandler) ServeHTTP(w http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
|
||||
recorder := newRetryResponseRecorder()
|
||||
recorder.responseWriter = w
|
||||
next.ServeHTTP(recorder, req)
|
||||
|
||||
w.WriteHeader(recorder.Code)
|
||||
//check the recorder code against the configured http status code ranges
|
||||
for _, block := range ep.HTTPCodeRanges {
|
||||
if recorder.Code >= block[0] && recorder.Code <= block[1] {
|
||||
log.Errorf("Caught HTTP Status Code %d, returning error page", recorder.Code)
|
||||
finalURL := strings.Replace(ep.BackendURL, "{status}", strconv.Itoa(recorder.Code), -1)
|
||||
if newReq, err := http.NewRequest(http.MethodGet, finalURL, nil); err != nil {
|
||||
w.Write([]byte(http.StatusText(recorder.Code)))
|
||||
} else {
|
||||
ep.errorPageForwarder.ServeHTTP(w, newReq)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
//did not catch a configured status code so proceed with the request
|
||||
utils.CopyHeaders(w.Header(), recorder.Header())
|
||||
w.Write(recorder.Body.Bytes())
|
||||
}
|
140
middlewares/error_pages_test.go
Normal file
140
middlewares/error_pages_test.go
Normal file
|
@ -0,0 +1,140 @@
|
|||
package middlewares
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/codegangsta/negroni"
|
||||
"github.com/containous/traefik/types"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestErrorPage(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintln(w, "Test Server")
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
testErrorPage := &types.ErrorPage{Backend: "error", Query: "/test", Status: []string{"500-501", "503-599"}}
|
||||
testHandler, err := NewErrorPagesHandler(*testErrorPage, ts.URL)
|
||||
|
||||
assert.Equal(t, nil, err, "Should be no error")
|
||||
assert.Equal(t, testHandler.BackendURL, ts.URL+"/test", "Should be equal")
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
req, err := http.NewRequest("GET", ts.URL+"/test", nil)
|
||||
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintln(w, "traefik")
|
||||
})
|
||||
n := negroni.New()
|
||||
n.Use(testHandler)
|
||||
n.UseHandler(handler)
|
||||
|
||||
n.ServeHTTP(recorder, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, recorder.Code, "HTTP statusOK")
|
||||
assert.Contains(t, recorder.Body.String(), "traefik")
|
||||
|
||||
handler500 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(500)
|
||||
fmt.Fprintln(w, "oops")
|
||||
})
|
||||
recorder500 := httptest.NewRecorder()
|
||||
n500 := negroni.New()
|
||||
n500.Use(testHandler)
|
||||
n500.UseHandler(handler500)
|
||||
|
||||
n500.ServeHTTP(recorder500, req)
|
||||
|
||||
assert.Equal(t, http.StatusInternalServerError, recorder500.Code, "HTTP status Internal Server Error")
|
||||
assert.Contains(t, recorder500.Body.String(), "Test Server")
|
||||
assert.NotContains(t, recorder500.Body.String(), "oops", "Should not return the oops page")
|
||||
|
||||
handler502 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(502)
|
||||
fmt.Fprintln(w, "oops")
|
||||
})
|
||||
recorder502 := httptest.NewRecorder()
|
||||
n502 := negroni.New()
|
||||
n502.Use(testHandler)
|
||||
n502.UseHandler(handler502)
|
||||
|
||||
n502.ServeHTTP(recorder502, req)
|
||||
|
||||
assert.Equal(t, http.StatusBadGateway, recorder502.Code, "HTTP status Bad Gateway")
|
||||
assert.Contains(t, recorder502.Body.String(), "oops")
|
||||
assert.NotContains(t, recorder502.Body.String(), "Test Server", "Should return the oops page since we have not configured the 502 code")
|
||||
|
||||
}
|
||||
|
||||
func TestErrorPageQuery(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.RequestURI() == "/"+strconv.Itoa(503) {
|
||||
fmt.Fprintln(w, "503 Test Server")
|
||||
} else {
|
||||
fmt.Fprintln(w, "Failed")
|
||||
}
|
||||
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
testErrorPage := &types.ErrorPage{Backend: "error", Query: "/{status}", Status: []string{"503-503"}}
|
||||
testHandler, err := NewErrorPagesHandler(*testErrorPage, ts.URL)
|
||||
assert.Equal(t, nil, err, "Should be no error")
|
||||
assert.Equal(t, testHandler.BackendURL, ts.URL+"/{status}", "Should be equal")
|
||||
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(503)
|
||||
fmt.Fprintln(w, "oops")
|
||||
})
|
||||
recorder := httptest.NewRecorder()
|
||||
req, err := http.NewRequest("GET", ts.URL+"/test", nil)
|
||||
n := negroni.New()
|
||||
n.Use(testHandler)
|
||||
n.UseHandler(handler)
|
||||
|
||||
n.ServeHTTP(recorder, req)
|
||||
|
||||
assert.Equal(t, http.StatusServiceUnavailable, recorder.Code, "HTTP status Service Unavailable")
|
||||
assert.Contains(t, recorder.Body.String(), "503 Test Server")
|
||||
assert.NotContains(t, recorder.Body.String(), "oops", "Should not return the oops page")
|
||||
|
||||
}
|
||||
|
||||
func TestErrorPageSingleCode(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.RequestURI() == "/"+strconv.Itoa(503) {
|
||||
fmt.Fprintln(w, "503 Test Server")
|
||||
} else {
|
||||
fmt.Fprintln(w, "Failed")
|
||||
}
|
||||
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
testErrorPage := &types.ErrorPage{Backend: "error", Query: "/{status}", Status: []string{"503"}}
|
||||
testHandler, err := NewErrorPagesHandler(*testErrorPage, ts.URL)
|
||||
assert.Equal(t, nil, err, "Should be no error")
|
||||
assert.Equal(t, testHandler.BackendURL, ts.URL+"/{status}", "Should be equal")
|
||||
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(503)
|
||||
fmt.Fprintln(w, "oops")
|
||||
})
|
||||
recorder := httptest.NewRecorder()
|
||||
req, err := http.NewRequest("GET", ts.URL+"/test", nil)
|
||||
n := negroni.New()
|
||||
n.Use(testHandler)
|
||||
n.UseHandler(handler)
|
||||
|
||||
n.ServeHTTP(recorder, req)
|
||||
|
||||
assert.Equal(t, http.StatusServiceUnavailable, recorder.Code, "HTTP status Service Unavailable")
|
||||
assert.Contains(t, recorder.Body.String(), "503 Test Server")
|
||||
assert.NotContains(t, recorder.Body.String(), "oops", "Should not return the oops page")
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue