From d0a97a3f4abdbdd1dd7ebcfd938c5e4d73140871 Mon Sep 17 00:00:00 2001 From: Jakub Date: Wed, 26 May 2021 14:25:34 +0200 Subject: [PATCH] GODT-1044: fix header lines parsing --- pkg/message/header.go | 6 +++++- pkg/message/header_test.go | 15 ++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/pkg/message/header.go b/pkg/message/header.go index c4cf9659..a911e827 100644 --- a/pkg/message/header.go +++ b/pkg/message/header.go @@ -35,11 +35,15 @@ func HeaderLines(header []byte) [][]byte { ) forEachLine(bufio.NewReader(bytes.NewReader(header)), func(line []byte) { + l := bytes.SplitN(line, []byte(`: `), 2) + isLineContinuation := quote%2 != 0 || // no quotes opened + len(l) != 2 || // it doesn't have colon + (len(l) == 2 && !bytes.Equal(bytes.TrimSpace(l[0]), l[0])) // has white space in front of header field switch { case len(bytes.TrimSpace(line)) == 0: lines = append(lines, line) - case quote%2 != 0, len(bytes.SplitN(line, []byte(`: `), 2)) != 2: + case isLineContinuation: if len(lines) > 0 { lines[len(lines)-1] = append(lines[len(lines)-1], line...) } else { diff --git a/pkg/message/header_test.go b/pkg/message/header_test.go index 89968740..0b9e911c 100644 --- a/pkg/message/header_test.go +++ b/pkg/message/header_test.go @@ -24,14 +24,19 @@ import ( ) func TestHeaderLines(t *testing.T) { - const header = "To: somebody\r\nFrom: somebody else\r\nSubject: this is\r\n\ta multiline field\r\n\r\n" - - assert.Equal(t, [][]byte{ + want := [][]byte{ []byte("To: somebody\r\n"), []byte("From: somebody else\r\n"), - []byte("Subject: this is\r\n\ta multiline field\r\n"), + []byte("Subject: RE: this is\r\n\ta multiline field: with colon\r\n\tor: many: more: colons\r\n"), + []byte("X-Special: \r\n\tNothing on the first line\r\n\tbut has something on the other lines\r\n"), []byte("\r\n"), - }, HeaderLines([]byte(header))) + } + var header []byte + for _, line := range want { + header = append(header, line...) + } + + assert.Equal(t, want, HeaderLines(header)) } func TestHeaderLinesMultilineFilename(t *testing.T) {