Skip to content

Commit

Permalink
Only fold directly after header name when having long header (#108)
Browse files Browse the repository at this point in the history
* only first fold when having a long header name

* calculate length once
  • Loading branch information
frederikbosch authored Mar 18, 2024
1 parent d3fe29f commit be1aa12
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 13 deletions.
7 changes: 1 addition & 6 deletions src/Header/HeaderLine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
8 changes: 5 additions & 3 deletions src/Stream/OptimalTransferEncodedPhraseStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 4 additions & 2 deletions src/Stream/OptimalTransferEncodedTextStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
3 changes: 1 addition & 2 deletions test/Stub/MessageBodyCollection/attachment-long-filename.eml
Original file line number Diff line number Diff line change
Expand Up @@ -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==
Expand Down

0 comments on commit be1aa12

Please sign in to comment.