tun2: generalize the 502 page generation

This commit is contained in:
Cadey Ratio 2017-09-30 11:08:41 -07:00
parent e8cf973208
commit 141fbfed76
No known key found for this signature in database
GPG Key ID: D607EE27C2E7F89A
1 changed files with 35 additions and 31 deletions

View File

@ -433,21 +433,7 @@ func (s *Server) RemoveConn(connection *Connection) {
})
}
// RoundTrip sends a HTTP request to a backend and then returns its response.
func (s *Server) RoundTrip(req *http.Request) (*http.Response, error) {
var conns []*Connection
val, ok := s.domains.Get(req.Host)
if ok {
conns, ok = val.([]*Connection)
if !ok {
ln.Error(ErrNoSuchBackend, ln.F{
"action": "no_backend_connected",
"remote": req.RemoteAddr,
"host": req.Host,
"uri": req.RequestURI,
})
func gen502Page(req *http.Request) *http.Response {
template := `<html><head><title>no backends connected</title></head><body><h1>no backends connected</h1><p>Please ensure a backend is running for ${HOST}. This is request ID ${REQ_ID}.</p></body></html>`
resbody := []byte(os.Expand(template, func(in string) string {
@ -477,7 +463,25 @@ func (s *Server) RoundTrip(req *http.Request) (*http.Response, error) {
Request: req,
}
return resp, nil
return resp
}
// RoundTrip sends a HTTP request to a backend and then returns its response.
func (s *Server) RoundTrip(req *http.Request) (*http.Response, error) {
var conns []*Connection
val, ok := s.domains.Get(req.Host)
if ok {
conns, ok = val.([]*Connection)
if !ok {
ln.Error(ErrNoSuchBackend, ln.F{
"action": "no_backend_connected",
"remote": req.RemoteAddr,
"host": req.Host,
"uri": req.RequestURI,
})
return gen502Page(req), nil
}
}
@ -496,7 +500,7 @@ func (s *Server) RoundTrip(req *http.Request) (*http.Response, error) {
"uri": req.RequestURI,
})
return nil, ErrNoSuchBackend
return gen502Page(req), nil
}
c := goodConns[rand.Intn(len(goodConns))]