traefik/vendor/github.com/vulcand/oxy/cbreaker/fallback.go

106 lines
2.8 KiB
Go
Raw Normal View History

2017-02-07 21:33:23 +00:00
package cbreaker
import (
"fmt"
"net/http"
"net/url"
"strconv"
2017-11-22 17:20:03 +00:00
2018-01-22 11:16:03 +00:00
log "github.com/sirupsen/logrus"
2017-11-22 17:20:03 +00:00
"github.com/vulcand/oxy/utils"
2017-02-07 21:33:23 +00:00
)
// Response response model
2017-02-07 21:33:23 +00:00
type Response struct {
StatusCode int
ContentType string
Body []byte
}
// ResponseFallback fallback response handler
2017-02-07 21:33:23 +00:00
type ResponseFallback struct {
r Response
log *log.Logger
2017-02-07 21:33:23 +00:00
}
// NewResponseFallbackWithLogger creates a new ResponseFallback
func NewResponseFallbackWithLogger(r Response, l *log.Logger) (*ResponseFallback, error) {
2017-02-07 21:33:23 +00:00
if r.StatusCode == 0 {
return nil, fmt.Errorf("response code should not be 0")
}
return &ResponseFallback{r: r, log: l}, nil
}
// NewResponseFallback creates a new ResponseFallback
func NewResponseFallback(r Response) (*ResponseFallback, error) {
return NewResponseFallbackWithLogger(r, log.StandardLogger())
2017-02-07 21:33:23 +00:00
}
func (f *ResponseFallback) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if f.log.Level >= log.DebugLevel {
logEntry := f.log.WithField("Request", utils.DumpHttpRequest(req))
2017-11-22 17:20:03 +00:00
logEntry.Debug("vulcand/oxy/fallback/response: begin ServeHttp on request")
defer logEntry.Debug("vulcand/oxy/fallback/response: completed ServeHttp on request")
2017-11-22 17:20:03 +00:00
}
2017-02-07 21:33:23 +00:00
if f.r.ContentType != "" {
w.Header().Set("Content-Type", f.r.ContentType)
}
w.Header().Set("Content-Length", strconv.Itoa(len(f.r.Body)))
w.WriteHeader(f.r.StatusCode)
2017-11-22 17:20:03 +00:00
_, err := w.Write(f.r.Body)
if err != nil {
2018-09-24 08:04:03 +00:00
f.log.Errorf("vulcand/oxy/fallback/response: failed to write response, err: %v", err)
2017-11-22 17:20:03 +00:00
}
2017-02-07 21:33:23 +00:00
}
// Redirect redirect model
2017-02-07 21:33:23 +00:00
type Redirect struct {
2017-11-22 17:20:03 +00:00
URL string
PreservePath bool
2017-02-07 21:33:23 +00:00
}
// RedirectFallback fallback redirect handler
2017-02-07 21:33:23 +00:00
type RedirectFallback struct {
2017-11-22 17:20:03 +00:00
r Redirect
u *url.URL
log *log.Logger
2017-02-07 21:33:23 +00:00
}
// NewRedirectFallbackWithLogger creates a new RedirectFallback
func NewRedirectFallbackWithLogger(r Redirect, l *log.Logger) (*RedirectFallback, error) {
2017-02-07 21:33:23 +00:00
u, err := url.ParseRequestURI(r.URL)
if err != nil {
return nil, err
}
return &RedirectFallback{r: r, u: u, log: l}, nil
}
// NewRedirectFallback creates a new RedirectFallback
func NewRedirectFallback(r Redirect) (*RedirectFallback, error) {
return NewRedirectFallbackWithLogger(r, log.StandardLogger())
2017-02-07 21:33:23 +00:00
}
func (f *RedirectFallback) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if f.log.Level >= log.DebugLevel {
logEntry := f.log.WithField("Request", utils.DumpHttpRequest(req))
2017-11-22 17:20:03 +00:00
logEntry.Debug("vulcand/oxy/fallback/redirect: begin ServeHttp on request")
defer logEntry.Debug("vulcand/oxy/fallback/redirect: completed ServeHttp on request")
2017-11-22 17:20:03 +00:00
}
location := f.u.String()
if f.r.PreservePath {
location += req.URL.Path
}
w.Header().Set("Location", location)
2017-02-07 21:33:23 +00:00
w.WriteHeader(http.StatusFound)
2017-11-22 17:20:03 +00:00
_, err := w.Write([]byte(http.StatusText(http.StatusFound)))
if err != nil {
2018-09-24 08:04:03 +00:00
f.log.Errorf("vulcand/oxy/fallback/redirect: failed to write response, err: %v", err)
2017-11-22 17:20:03 +00:00
}
2017-02-07 21:33:23 +00:00
}