From 57165a3c6d2ebc75e646b4ab0de128d29567451c Mon Sep 17 00:00:00 2001 From: Frederik Bosch Date: Mon, 18 Mar 2024 18:36:18 +0100 Subject: [PATCH 1/2] only first fold when having a long header name --- src/Header/HeaderLine.php | 7 +------ .../MessageBodyCollection/attachment-long-filename.eml | 3 +-- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/Header/HeaderLine.php b/src/Header/HeaderLine.php index e261485c..baff0eab 100644 --- a/src/Header/HeaderLine.php +++ b/src/Header/HeaderLine.php @@ -36,12 +36,7 @@ public function __toString(): string $headerName = (string)$this->header->getName(); $headerValue = (string)$this->header->getValue(); - $firstFoldingAt = \strpos($headerValue, "\r\n"); - if ($firstFoldingAt === false) { - $firstFoldingAt = \strlen($headerValue); - } - - if (\strlen($headerName) + $firstFoldingAt > 76) { + if (\strlen($headerName) > 60) { return \sprintf("%s:\r\n %s", $headerName, $headerValue); } diff --git a/test/Stub/MessageBodyCollection/attachment-long-filename.eml b/test/Stub/MessageBodyCollection/attachment-long-filename.eml index 8553ac60..48e73f42 100644 --- a/test/Stub/MessageBodyCollection/attachment-long-filename.eml +++ b/test/Stub/MessageBodyCollection/attachment-long-filename.eml @@ -11,8 +11,7 @@ Content-Transfer-Encoding: 7bit --GenkgoMailV2Partfd6a87d339fb Content-Type: text/txt; charset=UTF-8 Content-Transfer-Encoding: base64 -Content-Disposition: - attachment; filename="AAAAAAAA-AAAAAAAAAAAA-AAA_AAAA AAAAAAA AAAAA AAAAA AAAAAAAAAAAA +Content-Disposition: attachment; filename="AAAAAAAA-AAAAAAAAAAAA-AAA_AAAA AAAAAAA AAAAA AAAAA AAAAAAAAAAAA AAAAAAAAAAAAAAAAAA.pdf" dGVzdA== From 4216a5cb96c619626d3a8d24f3919d9912f3b3ae Mon Sep 17 00:00:00 2001 From: Frederik Bosch Date: Mon, 18 Mar 2024 19:45:32 +0100 Subject: [PATCH 2/2] calculate length once --- src/Stream/OptimalTransferEncodedPhraseStream.php | 8 +++++--- src/Stream/OptimalTransferEncodedTextStream.php | 6 ++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Stream/OptimalTransferEncodedPhraseStream.php b/src/Stream/OptimalTransferEncodedPhraseStream.php index 1ac38a3c..655fac0e 100644 --- a/src/Stream/OptimalTransferEncodedPhraseStream.php +++ b/src/Stream/OptimalTransferEncodedPhraseStream.php @@ -48,17 +48,19 @@ public function __construct(string $text, int $lineLength = 78, string $lineBrea */ private function calculateOptimalStream(string $text): StreamInterface { - if (\strcspn($text, self::NON_7BIT_CHARS) === \strlen($text)) { + $length = \strlen($text); + + if (\strcspn($text, self::NON_7BIT_CHARS) === $length) { $this->encoding = '7bit'; return new AsciiEncodedStream($text, $this->lineLength, $this->lineBreak); } - if (\strcspn($text, HeaderValueParameter::RFC_822_T_SPECIAL) !== \strlen($text)) { + if (\strcspn($text, HeaderValueParameter::RFC_822_T_SPECIAL) !== $length) { $this->encoding = 'base64'; return Base64EncodedStream::fromString($text, $this->lineLength, $this->lineBreak); } - if (\preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $text) > (\strlen($text) / 3)) { + if (\preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $text) > ($length / 3)) { $this->encoding = 'base64'; return Base64EncodedStream::fromString($text, $this->lineLength, $this->lineBreak); } diff --git a/src/Stream/OptimalTransferEncodedTextStream.php b/src/Stream/OptimalTransferEncodedTextStream.php index 9c5a63e2..1edb6934 100644 --- a/src/Stream/OptimalTransferEncodedTextStream.php +++ b/src/Stream/OptimalTransferEncodedTextStream.php @@ -47,12 +47,14 @@ public function __construct(string $text, int $lineLength = 78, string $lineBrea */ private function calculateOptimalStream(string $text): StreamInterface { - if (\strcspn($text, self::NON_7BIT_CHARS) === \strlen($text)) { + $length = \strlen($text); + + if (\strcspn($text, self::NON_7BIT_CHARS) === $length) { $this->encoding = '7bit'; return new AsciiEncodedStream($text, $this->lineLength, $this->lineBreak); } - if (\preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $text) > (\strlen($text) / 3)) { + if (\preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $text) > ($length / 3)) { $this->encoding = 'base64'; return Base64EncodedStream::fromString($text, $this->lineLength, $this->lineBreak); }