net/mail: fix EOF error while reading header-only message

Check if any header found in case of EOF to recognize header-only
messages and if so, return a Message with the found headers
and a body from the reader which is already empty.

Fixes #33823.

Change-Id: I2f0396b08e9be4e6c89c212ce62b9c87b5f63123
GitHub-Last-Rev: 356a942083
GitHub-Pull-Request: golang/go#47898
Reviewed-on: https://go-review.googlesource.com/c/go/+/344269
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
0xc0d 2022-11-22 00:14:11 +00:00 committed by Gopher Robot
parent c994067e5b
commit 42137704fc
2 changed files with 14 additions and 1 deletions

View file

@ -54,7 +54,7 @@ func ReadMessage(r io.Reader) (msg *Message, err error) {
tp := textproto.NewReader(bufio.NewReader(r))
hdr, err := tp.ReadMIMEHeader()
if err != nil {
if err != nil && (err != io.EOF || len(hdr) == 0) {
return nil, err
}

View file

@ -39,6 +39,19 @@ So, "Hello".
},
body: "This is a message just to say hello.\nSo, \"Hello\".\n",
},
{
// RFC 5965, Appendix B.1, a part of the multipart message (a header-only sub message)
in: `Feedback-Type: abuse
User-Agent: SomeGenerator/1.0
Version: 1
`,
header: Header{
"Feedback-Type": []string{"abuse"},
"User-Agent": []string{"SomeGenerator/1.0"},
"Version": []string{"1"},
},
body: "",
},
}
func TestParsing(t *testing.T) {