When using Hijack() with a http.ResponseWriter
instance
Hijack() (net.Conn, *bufio.ReadWriter, error)
What is the difference between reading from the net.Conn
and the *bufio.ReadWriter
?
When using Hijack() with a http.ResponseWriter
instance
Hijack() (net.Conn, *bufio.ReadWriter, error)
What is the difference between reading from the net.Conn
and the *bufio.ReadWriter
?
net.Conn.Read
and *bufio.ReadWriter.Read
both read from the same connection, but the latter is buffered. The Hijack
method in the standard "net/http" package directly returns the net.Conn
wrapped in a bufio.ReadWriter
, using the same *bufio.Reader
that was already allocated for the http request.
It's possible that there is still data buffered in the bufio.Reader
which you may miss when reading directly from the network connection. If you want to use the net.Conn
directly, you should check if there is buffered data already with Reader.Buffered
, and handle that according to the protocol being used.
In general, you should prefer to use bufio.ReadWriter
, as it will be more efficient for non-optimally sized reads and writes to the network.
The net.Conn
is still needed to handle the Read and Write deadlines, to close the net.Conn
when you're done, and for any other network-specific activities.
Their difference is buffered IO.
net.Conn implements Read() and Write() thus implementing io.Reader and io.Writer. This is why bufio can wrap it up as a buffered ReadWriter and further implement functions on top using those two methods in a buffered manner.
If you only want to use Read() and Write(), you can just stick with net.Conn, otherwise you can take advantage for the buffered ReadWriter methods.
See https://golang.org/pkg/io/#Reader and https://golang.org/pkg/io/#Writer