From c2b6e7ff346e3373a4e33c946594bb6f08393ad3 Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann Date: Tue, 16 Feb 2021 00:16:35 +0100 Subject: Issue #46: Move and rename writer; add comments --- pkg/rfc822/writer.go | 72 +++++++++++++++++++++++++++++++++++++++++++++++ pkg/rfc822/writer_test.go | 47 +++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 pkg/rfc822/writer.go create mode 100644 pkg/rfc822/writer_test.go (limited to 'pkg/rfc822') diff --git a/pkg/rfc822/writer.go b/pkg/rfc822/writer.go new file mode 100644 index 0000000..07751ea --- /dev/null +++ b/pkg/rfc822/writer.go @@ -0,0 +1,72 @@ +// Package rfc822 provides a writer that ensures the intrinsics of RFC 822. +// +// Rationale +// +// Cyrus IMAP really cares about the hard specifics of RFC 822, namely not allowing single \r and \n. +// +// See also: https://www.cyrusimap.org/imap/reference/faqs/interop-barenewlines.html +// and: https://github.com/Necoro/feed2imap-go/issues/46 +// +// NB: This package currently only cares about the newlines. +package rfc822 + +import "io" + +type rfc822Writer struct { + w io.Writer +} + +var lf = []byte{'\n'} +var cr = []byte{'\r'} + +func (f rfc822Writer) Write(p []byte) (n int, err error) { + crFound := false + start := 0 + + write := func(str []byte) { + var j int + j, err = f.w.Write(str) + n = n + j + } + + for idx, b := range p { + if crFound && b != '\n' { + // insert '\n' + if write(p[start:idx]); err != nil { + return + } + if write(lf); err != nil { + return + } + + start = idx + } else if !crFound && b == '\n' { + // insert '\r' + if write(p[start:idx]); err != nil { + return + } + if write(cr); err != nil { + return + } + + start = idx + } + crFound = b == '\r' + } + + // write the remainder + if write(p[start:]); err != nil { + return + } + + if crFound { // dangling \r + write(lf) + } + + return +} + +// Writer creates a new RFC 822 conform writer. +func Writer(w io.Writer) io.Writer { + return rfc822Writer{w} +} diff --git a/pkg/rfc822/writer_test.go b/pkg/rfc822/writer_test.go new file mode 100644 index 0000000..7beae8d --- /dev/null +++ b/pkg/rfc822/writer_test.go @@ -0,0 +1,47 @@ +package rfc822 + +import ( + "bytes" + "io" + "testing" +) + +func TestRfc822Writer_Write(t *testing.T) { + tests := []struct { + before string + after string + }{ + {"", ""}, + {"foo", "foo"}, + {"foo\r", "foo\r\n"}, + {"foo\n", "foo\r\n"}, + {"foo\r\n", "foo\r\n"}, + {"\r", "\r\n"}, + {"\n", "\r\n"}, + {"\r\n", "\r\n"}, + {"foo\rbar", "foo\r\nbar"}, + {"foo\nbar", "foo\r\nbar"}, + {"foo\r\nbar", "foo\r\nbar"}, + {"\r\r", "\r\n\r\n"}, + {"\n\n", "\r\n\r\n"}, + {"\r\r\n", "\r\n\r\n"}, + {"\n\r", "\r\n\r\n"}, + {"\rbar", "\r\nbar"}, + {"\nbar", "\r\nbar"}, + {"\r\nbar", "\r\nbar"}, + } + for _, tt := range tests { + t.Run(tt.before, func(t *testing.T) { + b := bytes.Buffer{} + w := Writer(&b) + if _, err := io.WriteString(w, tt.before); err != nil { + t.Errorf("Error: %v", err) + return + } + res := b.String() + if tt.after != res { + t.Errorf("Expected: %q, got: %q", tt.after, res) + } + }) + } +} -- cgit v1.2.3-54-g00ecf