1package imapserver
2
3import (
4 "bufio"
5 "io"
6 "net"
7)
8
9// prefixConn is a net.Conn with a buffer from which the first reads are satisfied.
10// used for STARTTLS where already did a buffered read of initial TLS data.
11type prefixConn struct {
12 prefix []byte
13 net.Conn
14}
15
16func (c *prefixConn) Read(buf []byte) (int, error) {
17 if len(c.prefix) > 0 {
18 n := min(len(buf), len(c.prefix))
19 copy(buf[:n], c.prefix[:n])
20 c.prefix = c.prefix[n:]
21 if len(c.prefix) == 0 {
22 c.prefix = nil
23 }
24 return n, nil
25 }
26 return c.Conn.Read(buf)
27}
28
29// xprefixConn returns either the original net.Conn passed as parameter, or returns
30// a *prefixConn returning the buffered data available in br followed data from the
31// net.Conn passed in.
32func xprefixConn(c net.Conn, br *bufio.Reader) net.Conn {
33 n := br.Buffered()
34 if n == 0 {
35 return c
36 }
37
38 buf := make([]byte, n)
39 _, err := io.ReadFull(c, buf)
40 xcheckf(err, "get buffered data")
41 return &prefixConn{buf, c}
42}
43