Skip to content

Commit

Permalink
proxy: Fixed #1352: invalid use of the HTTP hijacker
Browse files Browse the repository at this point in the history
  • Loading branch information
lhecker committed Jan 17, 2017
1 parent 8464020 commit ae10122
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions caddyhttp/proxy/reverseproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func (rp *ReverseProxy) ServeHTTP(rw http.ResponseWriter, outreq *http.Request,
panic(httpserver.NonHijackerError{Underlying: rw})
}

conn, _, err := hj.Hijack()
conn, brw, err := hj.Hijack()
if err != nil {
return err
}
Expand All @@ -260,8 +260,23 @@ func (rp *ReverseProxy) ServeHTTP(rw http.ResponseWriter, outreq *http.Request,
}
defer backendConn.Close()

go pooledIoCopy(backendConn, conn) // write tcp stream to backend
pooledIoCopy(conn, backendConn) // read tcp stream from backend
// Proxy backend -> frontend.
go pooledIoCopy(conn, backendConn)

// Proxy frontend -> backend.
//
// NOTE: Hijack() sometimes returns buffered up bytes in brw which
// would be lost if we didn't read them out manually below.
if brw != nil {
if n := brw.Reader.Buffered(); n > 0 {
rbuf, err := brw.Reader.Peek(n)
if err != nil {
return err
}
backendConn.Write(rbuf)
}
}
pooledIoCopy(backendConn, conn)
} else {
copyHeader(rw.Header(), res.Header)

Expand Down

0 comments on commit ae10122

Please sign in to comment.