Skip to content

Commit

Permalink
wrap error in http content for non-browser requests
Browse files Browse the repository at this point in the history
  • Loading branch information
fffw committed Dec 1, 2015
1 parent db203b6 commit 912a02b
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/github.com/getlantern/flashlight/client/reverseproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,29 +131,31 @@ type errorRewritingRoundTripper struct {
func (er *errorRewritingRoundTripper) RoundTrip(req *http.Request) (resp *http.Response, err error) {
res, err := er.orig.RoundTrip(req)
if err != nil {
agent := req.Header.Get("User-Agent")
var htmlerr []byte

// Virtually all browsers mark themselves as Mozilla compatible
// while Opera has a user selectable option.
// Ref http://webaim.org/blog/user-agent-string-history/
// We only render user friendly error page to browsers.
agent := req.Header.Get("User-Agent")
isBrowser := strings.HasPrefix(agent, "Mozilla") || strings.HasPrefix(agent, "Opera")
if !isBrowser {
return res, err
}

// It is likely we will have lots of different errors to handle but for now
// we will only return a ErrorAccessingPage error. This prevents the user
// from getting just a blank screen.
htmlerr, err := status.ErrorAccessingPage(req.Host, err)

if err != nil {
log.Debugf("Got error while generating status page: %q", err)
if isBrowser {
// It is likely we will have lots of different errors to handle but for now
// we will only return a ErrorAccessingPage error. This prevents the user
// from getting just a blank screen.
htmlerr, err = status.ErrorAccessingPage(req.Host, err)
if err != nil {
log.Debugf("Got error while generating status page: %q", err)
}
} else {
// For non-browser applications, wrap the error message in http content,
// or http.ReverseProxy will response 500 Internal Server Error instead.
htmlerr = []byte(err.Error())
}

res = &http.Response{
Body: ioutil.NopCloser(bytes.NewBuffer(htmlerr)),
}

res.StatusCode = http.StatusServiceUnavailable
return res, nil
}
Expand Down

0 comments on commit 912a02b

Please sign in to comment.