Skip to content
This repository has been archived by the owner on Feb 15, 2019. It is now read-only.

Commit

Permalink
Forward upgrade error from backend
Browse files Browse the repository at this point in the history
  • Loading branch information
juliens committed Sep 29, 2017
1 parent 6c94d28 commit 9c422c7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
29 changes: 27 additions & 2 deletions forward/fwd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"net/url"
"os"
"reflect"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -261,8 +262,32 @@ func (f *websocketForwarder) serveHTTP(w http.ResponseWriter, req *http.Request,
}
targetConn, resp, err := dialer.Dial(outReq.URL.String(), outReq.Header)
if err != nil {
ctx.log.Errorf("Error dialing `%v`: %v", outReq.Host, err)
ctx.errHandler.ServeHTTP(w, req, err)
if resp == nil {
ctx.errHandler.ServeHTTP(w, req, err)
} else {
ctx.log.Errorf("Error dialing `%s`: %v with resp: %d %s", outReq.Host, err, resp.StatusCode, resp.Status)
hijacker, ok := w.(http.Hijacker)
if !ok {
ctx.log.Errorf("%s can not be hijack", reflect.TypeOf(w))
ctx.errHandler.ServeHTTP(w, req, err)
return
}

conn, _, err := hijacker.Hijack()
if err != nil {
ctx.log.Errorf("Failed to hijack responseWriter")
ctx.errHandler.ServeHTTP(w, req, err)
return
}
defer conn.Close()

err = resp.Write(conn)
if err != nil {
ctx.log.Errorf("Failed to forward response")
ctx.errHandler.ServeHTTP(w, req, err)
return
}
}
return
}

Expand Down
5 changes: 3 additions & 2 deletions forward/fwd_websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"crypto/tls"
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -290,7 +291,7 @@ func (s *FwdSuite) TestWebsocketUpgradeFailed(c *C) {
br := bufio.NewReader(conn)
resp, err := http.ReadResponse(br, req)

c.Assert(resp.StatusCode, Equals, 500)
c.Assert(resp.StatusCode, Equals, 400)

req, err = http.NewRequest(http.MethodGet, "ws://127.0.0.1/ws2", nil)
req.Header.Add("upgrade", "websocket")
Expand All @@ -299,7 +300,7 @@ func (s *FwdSuite) TestWebsocketUpgradeFailed(c *C) {

br = bufio.NewReader(conn)
resp, err = http.ReadResponse(br, req)
c.Assert(resp.StatusCode, Equals, 200)
c.Assert(err, Equals, io.ErrUnexpectedEOF)
}

func (s *FwdSuite) TestForwardsWebsocketTraffic(c *C) {
Expand Down

0 comments on commit 9c422c7

Please sign in to comment.