handle parsing message header without header/body separator

the commit before the previous added tests with a message with only 1 header
line. it's a valid message, but Go's mail.ReadMessage doesn't handle it with
go1.20 and earlier. the automated "test with previous go release" caught it.
work around it by adding the expected but absent \r\n to the parse function.
This commit is contained in:
Mechiel Lukkien 2023-08-14 15:40:27 +02:00
parent f96310fdd5
commit f5f953b3ab
No known key found for this signature in database

View file

@ -376,7 +376,18 @@ func parseHeader(r io.Reader) (textproto.MIMEHeader, error) {
// first handles email messages properly, while the second only works for HTTP // first handles email messages properly, while the second only works for HTTP
// headers. // headers.
var zero textproto.MIMEHeader var zero textproto.MIMEHeader
msg, err := mail.ReadMessage(bufio.NewReader(r))
// We read the header and add the optional \r\n header/body separator. If the \r\n
// is missing, parsing with Go <1.21 results in an EOF error.
// todo: directly parse from reader r when Go 1.20 is no longer supported.
buf, err := io.ReadAll(r)
if err != nil {
return zero, err
}
if bytes.HasSuffix(buf, []byte("\r\n")) && !bytes.HasSuffix(buf, []byte("\r\n\r\n")) {
buf = append(buf, "\r\n"...)
}
msg, err := mail.ReadMessage(bytes.NewReader(buf))
if err != nil { if err != nil {
return zero, err return zero, err
} }