mirror of
https://github.com/mjl-/mox.git
synced 2024-12-26 00:13:47 +03:00
32 lines
637 B
Go
32 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
|
||
|
}
|
||
|
}
|
||
|
}
|