mirror of
https://github.com/mjl-/mox.git
synced 2025-07-10 09:14:39 +03:00
mox!
This commit is contained in:
31
message/readheaders.go
Normal file
31
message/readheaders.go
Normal file
@ -0,0 +1,31 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user