mox/message/readheaders.go

32 lines
637 B
Go
Raw Normal View History

2023-01-30 16:27:06 +03:00
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
}
}
}