tun2: generalize the 502 page generation
This commit is contained in:
parent
e8cf973208
commit
141fbfed76
|
@ -433,6 +433,39 @@ func (s *Server) RemoveConn(connection *Connection) {
|
|||
})
|
||||
}
|
||||
|
||||
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 {
|
||||
switch in {
|
||||
case "HOST":
|
||||
return req.Host
|
||||
case "REQ_ID":
|
||||
return req.Header.Get("X-Request-Id")
|
||||
}
|
||||
|
||||
return "<unknown>"
|
||||
}))
|
||||
reshdr := req.Header
|
||||
reshdr.Set("Content-Type", "text/html; charset=utf-8")
|
||||
|
||||
resp := &http.Response{
|
||||
Status: fmt.Sprintf("%d Bad Gateway", http.StatusBadGateway),
|
||||
StatusCode: http.StatusBadGateway,
|
||||
Body: ioutil.NopCloser(bytes.NewBuffer(resbody)),
|
||||
|
||||
Proto: req.Proto,
|
||||
ProtoMajor: req.ProtoMajor,
|
||||
ProtoMinor: req.ProtoMinor,
|
||||
Header: reshdr,
|
||||
ContentLength: int64(len(resbody)),
|
||||
Close: true,
|
||||
Request: req,
|
||||
}
|
||||
|
||||
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
|
||||
|
@ -448,36 +481,7 @@ func (s *Server) RoundTrip(req *http.Request) (*http.Response, error) {
|
|||
"uri": req.RequestURI,
|
||||
})
|
||||
|
||||
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 {
|
||||
switch in {
|
||||
case "HOST":
|
||||
return req.Host
|
||||
case "REQ_ID":
|
||||
return req.Header.Get("X-Request-Id")
|
||||
}
|
||||
|
||||
return "<unknown>"
|
||||
}))
|
||||
reshdr := req.Header
|
||||
reshdr.Set("Content-Type", "text/html; charset=utf-8")
|
||||
|
||||
resp := &http.Response{
|
||||
Status: fmt.Sprintf("%d Bad Gateway", http.StatusBadGateway),
|
||||
StatusCode: http.StatusBadGateway,
|
||||
Body: ioutil.NopCloser(bytes.NewBuffer(resbody)),
|
||||
|
||||
Proto: req.Proto,
|
||||
ProtoMajor: req.ProtoMajor,
|
||||
ProtoMinor: req.ProtoMinor,
|
||||
Header: reshdr,
|
||||
ContentLength: int64(len(resbody)),
|
||||
Close: true,
|
||||
Request: req,
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
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))]
|
||||
|
|
Loading…
Reference in New Issue