-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
caddyhttp/proxy: invalid use of Hijack in reverseproxy.go #1352
Comments
I'd be fine with fixing this, but unfortunately I don't quite get why it doesn't work already. I was under the impression that the returned Can you explain (if you don't mind) why that's wrong? Or maybe you know where to find some sample code which makes use of the |
You are correct that the returned One way to solve this is to use a wrapped net.Conn that returns the buffered bytes before reading from the connection. type rbufConn struct {
net.Conn
rbuf []byte
}
func (c *rbufConn) Read(p []byte) (int, error) {
if len(c.rbuf) > 0 {
n := copy(p, c.rbuf)
c.rbuf = c.rbuf[n:]
return n, nil
}
return c.Conn.Read(p)
}
func (c *rbufConn) Close() error {
c.rbuf = nil
return c.Conn.Close()
}
// Elsewhere in the code (ignoring error checking):
c, brw, _ := resp.(http.Hijacker).Hijack()
rbuf, _ := brw.Reader.Peek(brw.Reader.Buffered())
c = &rbufConn{conn, rbuf} |
The change LGTM. |
proxy: Fixed #1352: invalid use of the HTTP hijacker
Hello, in preparation for Go1.8, I detected this misuse of the http.Hijacker API.
reverseproxy.go makes a call to Hijack, but ignores the returned
bufio.ReadWriter
and proceeds to directly use the connection. In Go1.8, the probability that data is buffered in thebufio.Reader
is increased, such that there is a higher change that this logic fails. The proper fix is to handle the data in the read buffer (accessed viabrw.Reader.Peek(brw.Reader.Buffered())
) and forward that to thebackendConn
before performing the pair ofio.Copy
calls.See https://golang.org/cl/35232 for more information.
\cc @mholt
The text was updated successfully, but these errors were encountered: