forked from mushorg/glutton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeek.go
38 lines (31 loc) · 829 Bytes
/
peek.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package glutton
import (
"bufio"
"net"
)
// BufferedConn provides an interface to peek at a connection
type BufferedConn struct {
r *bufio.Reader
net.Conn // So that most methods are embedded
}
func newBufferedConn(c net.Conn) BufferedConn {
return BufferedConn{bufio.NewReader(c), c}
}
func newBufferedConnSize(c net.Conn, n int) BufferedConn {
return BufferedConn{bufio.NewReaderSize(c, n), c}
}
func (b BufferedConn) peek(n int) ([]byte, error) {
return b.r.Peek(n)
}
func (b BufferedConn) Read(p []byte) (int, error) {
return b.r.Read(p)
}
// Peek reads `length` amount of data from the connection
func (g *Glutton) Peek(conn net.Conn, length int) (snip []byte, bufConn BufferedConn, err error) {
bufConn = newBufferedConn(conn)
snip, err = bufConn.peek(length)
if err != nil {
return
}
return
}