Skip to content

Commit

Permalink
Adding "Address not found" generic page.
Browse files Browse the repository at this point in the history
  • Loading branch information
xiam committed Aug 19, 2015
1 parent f96aff8 commit 84b9b2d
Show file tree
Hide file tree
Showing 6 changed files with 379 additions and 62 deletions.
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,10 @@ release: require-version require-s3cmd require-gh-token require-wget require-rub

update-resources:
@(which go-bindata >/dev/null) || (echo 'Missing command "go-bindata". Sett https://github.com/jteeuwen/go-bindata.' && exit 1) && \
go-bindata -nomemcopy -nocompress -pkg main -o src/github.com/getlantern/flashlight/resources.go -prefix src/github.com/getlantern/flashlight/ \
src/github.com/getlantern/flashlight/icons src/github.com/getlantern/flashlight/status_pages
go-bindata -nomemcopy -nocompress -pkg main -o src/github.com/getlantern/flashlight/resources.go -prefix \
src/github.com/getlantern/flashlight/ src/github.com/getlantern/flashlight/icons && \
go-bindata -nomemcopy -nocompress -pkg status -o src/github.com/getlantern/flashlight/status/resources.go -prefix \
src/github.com/getlantern/flashlight/status_pages src/github.com/getlantern/flashlight/status_pages

create-tag: require-version
@git tag -a "$$VERSION" -f --annotate -m"Tagged $$VERSION" && \
Expand Down
38 changes: 35 additions & 3 deletions src/github.com/getlantern/flashlight/client/reverseproxy.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package client

import (
"bytes"
"io/ioutil"
"net/http"
"net/http/httputil"
"runtime"
Expand All @@ -9,6 +11,7 @@ import (
"github.com/getlantern/balancer"
"github.com/getlantern/detour"
"github.com/getlantern/flashlight/proxy"
"github.com/getlantern/flashlight/status"
)

// getReverseProxy waits for a message from client.rpCh to arrive and then it
Expand Down Expand Up @@ -52,9 +55,9 @@ func (client *Client) initReverseProxy(bal *balancer.Balancer, dumpHeaders bool)
Director: func(req *http.Request) {
// do nothing
},
Transport: withDumpHeaders(
dumpHeaders,
transport),
Transport: &errorRewritingRoundTripper{
withDumpHeaders(dumpHeaders, transport),
},
// Set a FlushInterval to prevent overly aggressive buffering of
// responses, which helps keep memory usage down
FlushInterval: 250 * time.Millisecond,
Expand Down Expand Up @@ -102,3 +105,32 @@ func (rt *headerDumpingRoundTripper) RoundTrip(req *http.Request) (resp *http.Re
}
return
}

// The errorRewritingRoundTripper writes creates an special *http.Response when
// the roundtripper fails for some reason.
type errorRewritingRoundTripper struct {
orig http.RoundTripper
}

func (er *errorRewritingRoundTripper) RoundTrip(req *http.Request) (resp *http.Response, err error) {
res, err := er.orig.RoundTrip(req)
if err != nil {

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

if err != nil {
log.Debugf("Got error while generating status page: %q", err)
}

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

res.StatusCode = http.StatusServiceUnavailable
return res, nil
}
return res, err
}
26 changes: 0 additions & 26 deletions src/github.com/getlantern/flashlight/resources.go

Large diffs are not rendered by default.

250 changes: 250 additions & 0 deletions src/github.com/getlantern/flashlight/status/resources.go

Large diffs are not rendered by default.

56 changes: 56 additions & 0 deletions src/github.com/getlantern/flashlight/status/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package status

import (
"bytes"
"errors"
"html/template"
"strings"
)

type cannotFindServerT struct {
ServerName string
ErrorMessage string
}

func normalizeError(err error) string {
if err != nil {
content := strings.SplitN(strings.TrimSpace(err.Error()), "\n", 2)
return strings.TrimSpace(content[0])
}
return ""
}

// CannotFindServer creates and returns a generic "cannot find server" error.
func CannotFindServer(server string, errMessage error) ([]byte, error) {
var err error
var buf []byte
var tmpl *template.Template

if errMessage == nil {
errMessage = errors.New("Unknown error.")
}

buf, err = Asset("generic_error.html")

if err != nil {
return nil, err
}

tmpl, err = template.New("status_error").Parse(string(buf))
if err != nil {
return nil, err
}

data := cannotFindServerT{
ServerName: server,
ErrorMessage: normalizeError(errMessage),
}

out := bytes.NewBuffer(nil)

if err = tmpl.Execute(out, data); err != nil {
return nil, err
}

return out.Bytes(), nil
}
Loading

0 comments on commit 84b9b2d

Please sign in to comment.