1package message
2
3import (
4 "bufio"
5 "bytes"
6 "errors"
7 "io"
8)
9
10var crlf2x = []byte("\r\n\r\n")
11
12var ErrHeaderSeparator = errors.New("no header separator found")
13
14// ReadHeaders returns the headers of a message, ending with a single crlf.
15// Returns ErrHeaderSeparator if no header separator is found.
16func ReadHeaders(msg *bufio.Reader) ([]byte, error) {
17 buf := []byte{}
18 for {
19 line, err := msg.ReadBytes('\n')
20 if err != io.EOF && err != nil {
21 return nil, err
22 }
23 buf = append(buf, line...)
24 if bytes.HasSuffix(buf, crlf2x) {
25 return buf[:len(buf)-2], nil
26 }
27 if err == io.EOF {
28 return nil, ErrHeaderSeparator
29 }
30 }
31}
32