From be1aa1213db82c6e8ff21552eef6891baf06af72 Mon Sep 17 00:00:00 2001 From: Frederik Bosch Date: Mon, 18 Mar 2024 19:49:28 +0100 Subject: [PATCH] Only fold directly after header name when having long header (#108) * only first fold when having a long header name * calculate length once --- src/Header/HeaderLine.php | 7 +------ src/Stream/OptimalTransferEncodedPhraseStream.php | 8 +++++--- src/Stream/OptimalTransferEncodedTextStream.php | 6 ++++-- .../MessageBodyCollection/attachment-long-filename.eml | 3 +-- 4 files changed, 11 insertions(+), 13 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/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); } 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==