Commit 696b46f0 authored by Matt Holt's avatar Matt Holt Committed by GitHub

Merge pull request #1356 from mholt/fix_hijack

proxy: Fixed #1352: invalid use of the HTTP hijacker
parents e5ef285e ae10122f
...@@ -238,7 +238,7 @@ func (rp *ReverseProxy) ServeHTTP(rw http.ResponseWriter, outreq *http.Request, ...@@ -238,7 +238,7 @@ func (rp *ReverseProxy) ServeHTTP(rw http.ResponseWriter, outreq *http.Request,
panic(httpserver.NonHijackerError{Underlying: rw}) panic(httpserver.NonHijackerError{Underlying: rw})
} }
conn, _, err := hj.Hijack() conn, brw, err := hj.Hijack()
if err != nil { if err != nil {
return err return err
} }
...@@ -260,8 +260,23 @@ func (rp *ReverseProxy) ServeHTTP(rw http.ResponseWriter, outreq *http.Request, ...@@ -260,8 +260,23 @@ func (rp *ReverseProxy) ServeHTTP(rw http.ResponseWriter, outreq *http.Request,
} }
defer backendConn.Close() defer backendConn.Close()
go pooledIoCopy(backendConn, conn) // write tcp stream to backend // Proxy backend -> frontend.
pooledIoCopy(conn, backendConn) // read tcp stream from backend 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 { } else {
copyHeader(rw.Header(), res.Header) copyHeader(rw.Header(), res.Header)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment