Better logs http status in websocket

This commit is contained in:
emile 2015-10-15 09:08:16 +02:00
parent 539fd5bafc
commit 46d7cc83c9

View file

@ -73,15 +73,16 @@ func NewProxy(target *url.URL) *WebsocketProxy {
// ServeHTTP implements the http.Handler that proxies WebSocket connections. // ServeHTTP implements the http.Handler that proxies WebSocket connections.
func (w *WebsocketProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) { func (w *WebsocketProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if w.Backend == nil { if w.Backend == nil {
log.Println("websocketproxy: backend function is not defined") log.Errorf("Websocketproxy: backend function is not defined")
http.Error(rw, "internal server error (code: 1)", http.StatusInternalServerError) http.Error(rw, "Backend not found", http.StatusInternalServerError)
http.NotFound(rw, req)
return return
} }
backendURL := w.Backend(req) backendURL := w.Backend(req)
if backendURL == nil { if backendURL == nil {
log.Println("websocketproxy: backend URL is nil") log.Errorf("Websocketproxy: backend URL is nil")
http.Error(rw, "internal server error (code: 2)", http.StatusInternalServerError) http.Error(rw, "Backend URL is nil", http.StatusInternalServerError)
return return
} }
@ -131,7 +132,8 @@ func (w *WebsocketProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// http://tools.ietf.org/html/draft-ietf-hybi-websocket-multiplexing-01 // http://tools.ietf.org/html/draft-ietf-hybi-websocket-multiplexing-01
connBackend, resp, err := dialer.Dial(backendURL.String(), nil) connBackend, resp, err := dialer.Dial(backendURL.String(), nil)
if err != nil { if err != nil {
log.Printf("websocketproxy: couldn't dial to remote backend url %s, %s, %+v", backendURL.String(), err, resp) log.Errorf("Websocketproxy: couldn't dial to remote backend url %s, %s, %+v", backendURL.String(), err, resp)
http.NotFound(rw, req)
return return
} }
defer connBackend.Close() defer connBackend.Close()
@ -152,7 +154,8 @@ func (w *WebsocketProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// Also pass the header that we gathered from the Dial handshake. // Also pass the header that we gathered from the Dial handshake.
connPub, err := upgrader.Upgrade(rw, req, upgradeHeader) connPub, err := upgrader.Upgrade(rw, req, upgradeHeader)
if err != nil { if err != nil {
log.Printf("websocketproxy: couldn't upgrade %s\n", err) log.Errorf("Websocketproxy: couldn't upgrade %s", err)
http.NotFound(rw, req)
return return
} }
defer connPub.Close() defer connPub.Close()