mox/message/readheaders.go
Mechiel Lukkien cb229cb6cf
mox!
2023-01-30 14:27:06 +01:00

31 lines
637 B
Go

package message
import (
"bufio"
"bytes"
"errors"
"io"
)
var crlf2x = []byte("\r\n\r\n")
var ErrHeaderSeparator = errors.New("no header separator found")
// ReadHeaders returns the headers of a message, ending with a single crlf.
// Returns ErrHeaderSeparator if no header separator is found.
func ReadHeaders(msg *bufio.Reader) ([]byte, error) {
buf := []byte{}
for {
line, err := msg.ReadBytes('\n')
if err != io.EOF && err != nil {
return nil, err
}
buf = append(buf, line...)
if bytes.HasSuffix(buf, crlf2x) {
return buf[:len(buf)-2], nil
}
if err == io.EOF {
return nil, ErrHeaderSeparator
}
}
}