Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only fold directly after header name when having long header #108

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading