mirror of
https://github.com/mjl-/mox.git
synced 2025-06-28 11:38:16 +03:00

to compress the entire IMAP connection. tested with thunderbird, meli, k9, ios mail. the initial implementation had interoperability issues with some of these clients: if they write the deflate stream and flush in "partial mode", the go stdlib flate reader does not return any data (until there is an explicit zero-length "sync flush" block, or until the history/sliding window is full), blocking progress, resulting in clients closing the seemingly stuck connection after considering the connection timed out. this includes a coy of the flate package with a new reader that returns partially flushed blocks earlier. this also adds imap trace logging to imapclient.Conn, which was useful for debugging.
46 lines
978 B
Go
46 lines
978 B
Go
package imapserver
|
|
|
|
import (
|
|
"bufio"
|
|
"io"
|
|
"net"
|
|
)
|
|
|
|
// prefixConn is a net.Conn with a buffer from which the first reads are satisfied.
|
|
// used for STARTTLS where already did a buffered read of initial TLS data.
|
|
type prefixConn struct {
|
|
prefix []byte
|
|
net.Conn
|
|
}
|
|
|
|
func (c *prefixConn) Read(buf []byte) (int, error) {
|
|
if len(c.prefix) > 0 {
|
|
n := len(buf)
|
|
if n > len(c.prefix) {
|
|
n = len(c.prefix)
|
|
}
|
|
copy(buf[:n], c.prefix[:n])
|
|
c.prefix = c.prefix[n:]
|
|
if len(c.prefix) == 0 {
|
|
c.prefix = nil
|
|
}
|
|
return n, nil
|
|
}
|
|
return c.Conn.Read(buf)
|
|
}
|
|
|
|
// xprefixConn returns either the original net.Conn passed as parameter, or returns
|
|
// a *prefixConn returning the buffered data available in br followed data from the
|
|
// net.Conn passed in.
|
|
func xprefixConn(c net.Conn, br *bufio.Reader) net.Conn {
|
|
n := br.Buffered()
|
|
if n == 0 {
|
|
return c
|
|
}
|
|
|
|
buf := make([]byte, n)
|
|
_, err := io.ReadFull(c, buf)
|
|
xcheckf(err, "get buffered data")
|
|
return &prefixConn{buf, c}
|
|
}
|