From 14104c336553b5c1b26f463dff78764f813099d7 Mon Sep 17 00:00:00 2001 From: Paolo Barbolini Date: Thu, 28 Mar 2024 03:25:35 +0100 Subject: [PATCH] fix(rfc2047): improve output in some cases --- src/headers/rfc2047.rs | 10 ++++++++-- src/headers/writer.rs | 8 ++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/headers/rfc2047.rs b/src/headers/rfc2047.rs index fab9e4b..b089b7c 100644 --- a/src/headers/rfc2047.rs +++ b/src/headers/rfc2047.rs @@ -39,10 +39,16 @@ pub fn encode(mut s: &str, w: &mut EmailWriter<'_>) -> fmt::Result { let mut word = utils::truncate_to_char_boundary(s, unencoded_remaining_line_len.min(s.len())); if word.is_empty() { - if wrote { + if wrote || w.has_spaces() { // No space remaining on this line, go to a new one w.new_line()?; - w.space(); + if !w.has_spaces() { + // The last write before this call to `encode` most + // likely wasn't rfc2047 so we must write a "soft" + // space to let the decoder know we're still within the + // same header + w.space(); + } continue; } diff --git a/src/headers/writer.rs b/src/headers/writer.rs index eb1a67d..1917e85 100644 --- a/src/headers/writer.rs +++ b/src/headers/writer.rs @@ -58,6 +58,10 @@ impl<'a> EmailWriter<'a> { self.spaces = 0; } + pub(super) fn has_spaces(&mut self) -> bool { + self.spaces >= 1 + } + /// Get the length in bytes of the last line written to the inner writer. pub fn line_len(&self) -> usize { self.line_len @@ -329,8 +333,8 @@ mod tests { assert_eq!( s, concat!( - "Subject: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA BBBBBBBBBBBBB =?utf-8?b?cw==?=\r\n", - " =?utf-8?b?w6lsZWN0aW9u?=", + "Subject: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA BBBBBBBBBBBBB\r\n", + " =?utf-8?b?c8OpbGVjdGlvbg==?=", ) ); }