2023-01-30 16:27:06 +03:00
|
|
|
package message
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Writer is a write-through helper, collecting properties about the written
|
|
|
|
// message.
|
|
|
|
type Writer struct {
|
2023-08-11 15:07:49 +03:00
|
|
|
writer io.Writer
|
|
|
|
|
|
|
|
HaveBody bool // Body is optional. ../rfc/5322:343
|
|
|
|
Has8bit bool // Whether a byte with the high/8bit has been read. So whether this is 8BITMIME instead of 7BIT.
|
|
|
|
Size int64
|
|
|
|
|
|
|
|
tail [3]byte // For detecting header/body-separating crlf.
|
2023-01-30 16:27:06 +03:00
|
|
|
// todo: should be parsing headers here, as we go
|
|
|
|
}
|
|
|
|
|
2023-08-11 15:07:49 +03:00
|
|
|
func NewWriter(w io.Writer) *Writer {
|
|
|
|
// Pretend we already saw \r\n, for handling empty header.
|
|
|
|
return &Writer{writer: w, tail: [3]byte{0, '\r', '\n'}}
|
|
|
|
}
|
|
|
|
|
2023-01-30 16:27:06 +03:00
|
|
|
// Write implements io.Writer.
|
|
|
|
func (w *Writer) Write(buf []byte) (int, error) {
|
2023-08-11 15:07:49 +03:00
|
|
|
if !w.HaveBody && len(buf) > 0 {
|
2023-01-30 16:27:06 +03:00
|
|
|
get := func(i int) byte {
|
|
|
|
if i < 0 {
|
|
|
|
return w.tail[3+i]
|
|
|
|
}
|
|
|
|
return buf[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, b := range buf {
|
|
|
|
if b == '\n' && get(i-3) == '\r' && get(i-2) == '\n' && get(i-1) == '\r' {
|
2023-08-11 15:07:49 +03:00
|
|
|
w.HaveBody = true
|
2023-01-30 16:27:06 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
n := len(buf)
|
|
|
|
if n > 3 {
|
|
|
|
n = 3
|
|
|
|
}
|
|
|
|
copy(w.tail[:], w.tail[n:])
|
|
|
|
copy(w.tail[3-n:], buf[len(buf)-n:])
|
|
|
|
}
|
|
|
|
if !w.Has8bit {
|
|
|
|
for _, b := range buf {
|
|
|
|
if b&0x80 != 0 {
|
|
|
|
w.Has8bit = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-11 15:07:49 +03:00
|
|
|
n, err := w.writer.Write(buf)
|
2023-01-30 16:27:06 +03:00
|
|
|
if n > 0 {
|
|
|
|
w.Size += int64(n)
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|