Skip to content

Commit be1aa12

Browse files
Only fold directly after header name when having long header (#108)
* only first fold when having a long header name * calculate length once
1 parent d3fe29f commit be1aa12

File tree

4 files changed

+11
-13
lines changed

4 files changed

+11
-13
lines changed

src/Header/HeaderLine.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,7 @@ public function __toString(): string
3636
$headerName = (string)$this->header->getName();
3737
$headerValue = (string)$this->header->getValue();
3838

39-
$firstFoldingAt = \strpos($headerValue, "\r\n");
40-
if ($firstFoldingAt === false) {
41-
$firstFoldingAt = \strlen($headerValue);
42-
}
43-
44-
if (\strlen($headerName) + $firstFoldingAt > 76) {
39+
if (\strlen($headerName) > 60) {
4540
return \sprintf("%s:\r\n %s", $headerName, $headerValue);
4641
}
4742

src/Stream/OptimalTransferEncodedPhraseStream.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,19 @@ public function __construct(string $text, int $lineLength = 78, string $lineBrea
4848
*/
4949
private function calculateOptimalStream(string $text): StreamInterface
5050
{
51-
if (\strcspn($text, self::NON_7BIT_CHARS) === \strlen($text)) {
51+
$length = \strlen($text);
52+
53+
if (\strcspn($text, self::NON_7BIT_CHARS) === $length) {
5254
$this->encoding = '7bit';
5355
return new AsciiEncodedStream($text, $this->lineLength, $this->lineBreak);
5456
}
5557

56-
if (\strcspn($text, HeaderValueParameter::RFC_822_T_SPECIAL) !== \strlen($text)) {
58+
if (\strcspn($text, HeaderValueParameter::RFC_822_T_SPECIAL) !== $length) {
5759
$this->encoding = 'base64';
5860
return Base64EncodedStream::fromString($text, $this->lineLength, $this->lineBreak);
5961
}
6062

61-
if (\preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $text) > (\strlen($text) / 3)) {
63+
if (\preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $text) > ($length / 3)) {
6264
$this->encoding = 'base64';
6365
return Base64EncodedStream::fromString($text, $this->lineLength, $this->lineBreak);
6466
}

src/Stream/OptimalTransferEncodedTextStream.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@ public function __construct(string $text, int $lineLength = 78, string $lineBrea
4747
*/
4848
private function calculateOptimalStream(string $text): StreamInterface
4949
{
50-
if (\strcspn($text, self::NON_7BIT_CHARS) === \strlen($text)) {
50+
$length = \strlen($text);
51+
52+
if (\strcspn($text, self::NON_7BIT_CHARS) === $length) {
5153
$this->encoding = '7bit';
5254
return new AsciiEncodedStream($text, $this->lineLength, $this->lineBreak);
5355
}
5456

55-
if (\preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $text) > (\strlen($text) / 3)) {
57+
if (\preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $text) > ($length / 3)) {
5658
$this->encoding = 'base64';
5759
return Base64EncodedStream::fromString($text, $this->lineLength, $this->lineBreak);
5860
}

test/Stub/MessageBodyCollection/attachment-long-filename.eml

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ Content-Transfer-Encoding: 7bit
1111
--GenkgoMailV2Partfd6a87d339fb
1212
Content-Type: text/txt; charset=UTF-8
1313
Content-Transfer-Encoding: base64
14-
Content-Disposition:
15-
attachment; filename="AAAAAAAA-AAAAAAAAAAAA-AAA_AAAA AAAAAAA AAAAA AAAAA AAAAAAAAAAAA
14+
Content-Disposition: attachment; filename="AAAAAAAA-AAAAAAAAAAAA-AAA_AAAA AAAAAAA AAAAA AAAAA AAAAAAAAAAAA
1615
AAAAAAAAAAAAAAAAAA.pdf"
1716
1817
dGVzdA==

0 commit comments

Comments
 (0)