Skip to content

Commit

Permalink
Ignore ws between rfc2047 parts in q-part #240
Browse files Browse the repository at this point in the history
If two mime-encoded parts are found in a single quoted-part, ignore
any whitespace between them.
  • Loading branch information
zbateson committed Aug 5, 2024
1 parent 3a813b3 commit 48cf726
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/Header/Part/QuotedLiteralPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,38 @@

/**
* A quoted literal header string part. The value of the part is stripped of CR
* and LF characters, but otherwise not transformed or changed in any way.
* and LF characters, and whitespace between two adjacent MimeTokens is removed.
*
* @author Zaahid Bateson
*/
class QuotedLiteralPart extends ContainerPart
{
/**
* Strips spaces found between two adjacent MimeToken parts.
* Other whitespace is returned as-is.
*
* @param HeaderPart[] $parts
* @return HeaderPart[]
*/
protected function filterIgnoredSpaces(array $parts) : array
{
return $parts;
$filtered = \array_reduce(
\array_keys($parts),
function($carry, $key) use ($parts) {
$cur = $parts[$key];
$last = ($carry !== null) ? \end($carry) : null;
$next = (count($parts) > $key + 1) ? $parts[$key + 1] : null;
if ($last !== null && $next !== null && $cur->isSpace && (
$last->canIgnoreSpacesAfter
&& $next->canIgnoreSpacesBefore
&& $last instanceof MimeToken
&& $next instanceof MimeToken
)) {
return $carry;
}
return \array_merge($carry ?? [], [$cur]);
}
);
return $filtered;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,16 @@ public function testWithQuotedHeaderEncodedValue() : void
$ret = $this->consumer->__invoke('"=?US-ASCII?Q?value?="');
$this->assertEquals('value', $ret[0]->getValue());
}

public function testWithQuotedHeaderMultipleEncodedValues() : void
{
$ret = $this->consumer->__invoke('"=?US-ASCII?Q?Kilgore?= =?US-ASCII?Q?Trout?="');
$this->assertEquals('KilgoreTrout', $ret[0]->getValue());
}

public function testWithQuotedHeaderMultipleEncodedValuesAndLinesBetween() : void
{
$ret = $this->consumer->__invoke("\"=?US-ASCII?Q?Kilg?= \r\n =?US-ASCII?Q?or?= =?US-ASCII?Q?e_Trout?=\"");
$this->assertEquals('Kilgore Trout', $ret[0]->getValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,20 @@ public function testConsumeMimeEncodedValue() : void
$this->assertInstanceOf(QuotedLiteralPart::class, $ret[0]);
$this->assertEquals('Kilgore Trout', $ret[0]->getValue());
}

public function testWithQuotedHeaderMultipleEncodedValues() : void
{
$ret = $this->consumer->__invoke('=?US-ASCII?Q?Kilgore?= =?US-ASCII?Q?Trout?=');
$this->assertNotEmpty($ret);
$this->assertCount(1, $ret);
$this->assertEquals('KilgoreTrout', $ret[0]->getValue());
}

public function testWithQuotedHeaderMultipleEncodedValuesAndLinesBetween() : void
{
$ret = $this->consumer->__invoke("=?US-ASCII?Q?Kilg?= \r\n =?US-ASCII?Q?or?= =?US-ASCII?Q?e_Trout?=");
$this->assertNotEmpty($ret);
$this->assertCount(1, $ret);
$this->assertEquals('Kilgore Trout', $ret[0]->getValue());
}
}

0 comments on commit 48cf726

Please sign in to comment.