~emersion/public-inbox

go-message: textproto: Support headers without body v1 APPLIED

shiroyasha: 1
 textproto: Support headers without body

 2 files changed, 32 insertions(+), 0 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~emersion/public-inbox/patches/9396/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH go-message] textproto: Support headers without body Export this patch

---
I'm not familiarized with the codebase so I'm not sure if this is the
best solution, but at least it doesn't break any existing tests.

For the test, I just copy-pasted one of the already existing ones and
removed the additional CRLF.

 textproto/header.go      |  3 +++
 textproto/header_test.go | 29 +++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/textproto/header.go b/textproto/header.go
index 3942513..d9b12c2 100644
--- a/textproto/header.go
+++ b/textproto/header.go
@@ -401,6 +401,9 @@ func ReadHeader(r *bufio.Reader) (Header, error) {
	for {
		kv, err := readContinuedLineSlice(r)
		if len(kv) == 0 {
			if err == io.EOF {
				err = nil
			}
			return newHeader(fs), err
		}

diff --git a/textproto/header_test.go b/textproto/header_test.go
index df2cfe9..0dd7c36 100644
--- a/textproto/header_test.go
+++ b/textproto/header_test.go
@@ -205,6 +205,35 @@ func TestReadHeader(t *testing.T) {
	}
}

const testHeaderWithoutBody = "Received: from example.com by example.org\r\n" +
	"Received: from localhost by example.com\r\n" +
	"To: Taki Tachibana <taki.tachibana@example.org>\r\n" +
	"From: Mitsuha Miyamizu <mitsuha.miyamizu@example.com>\r\n"

func TestReadHeaderWithoutBody(t *testing.T) {
	r := bufio.NewReader(strings.NewReader(testHeaderWithoutBody))
	h, err := ReadHeader(r)
	if err != nil {
		t.Fatalf("readHeader() returned error: %v", err)
	}

	l := collectHeaderFields(h.Fields())
	want := []string{
		"Received: from example.com by example.org",
		"Received: from localhost by example.com",
		"To: Taki Tachibana <taki.tachibana@example.org>",
		"From: Mitsuha Miyamizu <mitsuha.miyamizu@example.com>",
	}
	if !reflect.DeepEqual(l, want) {
		t.Errorf("Fields() reported incorrect values: got \n%#v\n but want \n%#v", l, want)
	}

	b := make([]byte, 1)
	if _, err := r.Read(b); err != io.EOF {
		t.Errorf("Read() didn't return EOF: %v", err)
	}
}

const testLFHeader = `From: contact@example.org
To: contact@example.org
Subject: A little message, just for you
--
2.24.1
LGTM, pushed:

To github.com:emersion/go-message
   8eb6e2ed7b81..d6e6e18c7578  master -> master

Thanks!