Skip to content

Commit c6f4ec1

Browse files
committed
Merge branch '5.1' into 5.2
* 5.1: parse cookie values containing the equal sign make some time dependent tests more resilient do not break when loading schemas from network paths on Windows
2 parents 70f673d + 4a30514 commit c6f4ec1

File tree

3 files changed

+62
-29
lines changed

3 files changed

+62
-29
lines changed

HeaderUtils.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,30 +251,43 @@ public static function parseQuery(string $query, bool $ignoreBrackets = false, s
251251
return $query;
252252
}
253253

254-
private static function groupParts(array $matches, string $separators): array
254+
private static function groupParts(array $matches, string $separators, bool $first = true): array
255255
{
256256
$separator = $separators[0];
257257
$partSeparators = substr($separators, 1);
258258

259259
$i = 0;
260260
$partMatches = [];
261+
$previousMatchWasSeparator = false;
261262
foreach ($matches as $match) {
262-
if (isset($match['separator']) && $match['separator'] === $separator) {
263+
if (!$first && $previousMatchWasSeparator && isset($match['separator']) && $match['separator'] === $separator) {
264+
$previousMatchWasSeparator = true;
265+
$partMatches[$i][] = $match;
266+
} elseif (isset($match['separator']) && $match['separator'] === $separator) {
267+
$previousMatchWasSeparator = true;
263268
++$i;
264269
} else {
270+
$previousMatchWasSeparator = false;
265271
$partMatches[$i][] = $match;
266272
}
267273
}
268274

269275
$parts = [];
270276
if ($partSeparators) {
271277
foreach ($partMatches as $matches) {
272-
$parts[] = self::groupParts($matches, $partSeparators);
278+
$parts[] = self::groupParts($matches, $partSeparators, false);
273279
}
274280
} else {
275281
foreach ($partMatches as $matches) {
276282
$parts[] = self::unquote($matches[0][0]);
277283
}
284+
285+
if (!$first && 2 < \count($parts)) {
286+
$parts = [
287+
$parts[0],
288+
implode($separator, \array_slice($parts, 1)),
289+
];
290+
}
278291
}
279292

280293
return $parts;

Tests/CookieTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,12 @@ public function testFromString()
326326

327327
$cookie = Cookie::fromString('foo', true);
328328
$this->assertEquals(Cookie::create('foo', null, 0, '/', null, false, false, false, null), $cookie);
329+
330+
$cookie = Cookie::fromString('foo_cookie=foo=1&bar=2&baz=3; expires=Tue, 22-Sep-2020 06:27:09 GMT; path=/');
331+
$this->assertEquals(Cookie::create('foo_cookie', 'foo=1&bar=2&baz=3', strtotime('Tue, 22-Sep-2020 06:27:09 GMT'), '/', null, false, false, true, null), $cookie);
332+
333+
$cookie = Cookie::fromString('foo_cookie=foo==; expires=Tue, 22-Sep-2020 06:27:09 GMT; path=/');
334+
$this->assertEquals(Cookie::create('foo_cookie', 'foo==', strtotime('Tue, 22-Sep-2020 06:27:09 GMT'), '/', null, false, false, true, null), $cookie);
329335
}
330336

331337
public function testFromStringWithHttpOnly()

Tests/HeaderUtilsTest.php

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,47 @@
1616

1717
class HeaderUtilsTest extends TestCase
1818
{
19-
public function testSplit()
19+
/**
20+
* @dataProvider provideHeaderToSplit
21+
*/
22+
public function testSplit(array $expected, string $header, string $separator)
2023
{
21-
$this->assertSame(['foo=123', 'bar'], HeaderUtils::split('foo=123,bar', ','));
22-
$this->assertSame(['foo=123', 'bar'], HeaderUtils::split('foo=123, bar', ','));
23-
$this->assertSame([['foo=123', 'bar']], HeaderUtils::split('foo=123; bar', ',;'));
24-
$this->assertSame([['foo=123'], ['bar']], HeaderUtils::split('foo=123, bar', ',;'));
25-
$this->assertSame(['foo', '123, bar'], HeaderUtils::split('foo=123, bar', '='));
26-
$this->assertSame(['foo', '123, bar'], HeaderUtils::split(' foo = 123, bar ', '='));
27-
$this->assertSame([['foo', '123'], ['bar']], HeaderUtils::split('foo=123, bar', ',='));
28-
$this->assertSame([[['foo', '123']], [['bar'], ['foo', '456']]], HeaderUtils::split('foo=123, bar; foo=456', ',;='));
29-
$this->assertSame([[['foo', 'a,b;c=d']]], HeaderUtils::split('foo="a,b;c=d"', ',;='));
30-
31-
$this->assertSame(['foo', 'bar'], HeaderUtils::split('foo,,,, bar', ','));
32-
$this->assertSame(['foo', 'bar'], HeaderUtils::split(',foo, bar,', ','));
33-
$this->assertSame(['foo', 'bar'], HeaderUtils::split(' , foo, bar, ', ','));
34-
$this->assertSame(['foo bar'], HeaderUtils::split('foo "bar"', ','));
35-
$this->assertSame(['foo bar'], HeaderUtils::split('"foo" bar', ','));
36-
$this->assertSame(['foo bar'], HeaderUtils::split('"foo" "bar"', ','));
37-
38-
// These are not a valid header values. We test that they parse anyway,
39-
// and that both the valid and invalid parts are returned.
40-
$this->assertSame([], HeaderUtils::split('', ','));
41-
$this->assertSame([], HeaderUtils::split(',,,', ','));
42-
$this->assertSame(['foo', 'bar', 'baz'], HeaderUtils::split('foo, "bar", "baz', ','));
43-
$this->assertSame(['foo', 'bar, baz'], HeaderUtils::split('foo, "bar, baz', ','));
44-
$this->assertSame(['foo', 'bar, baz\\'], HeaderUtils::split('foo, "bar, baz\\', ','));
45-
$this->assertSame(['foo', 'bar, baz\\'], HeaderUtils::split('foo, "bar, baz\\\\', ','));
24+
$this->assertSame($expected, HeaderUtils::split($header, $separator));
25+
}
26+
27+
public function provideHeaderToSplit(): array
28+
{
29+
return [
30+
[['foo=123', 'bar'], 'foo=123,bar', ','],
31+
[['foo=123', 'bar'], 'foo=123, bar', ','],
32+
[[['foo=123', 'bar']], 'foo=123; bar', ',;'],
33+
[[['foo=123'], ['bar']], 'foo=123, bar', ',;'],
34+
[['foo', '123, bar'], 'foo=123, bar', '='],
35+
[['foo', '123, bar'], ' foo = 123, bar ', '='],
36+
[[['foo', '123'], ['bar']], 'foo=123, bar', ',='],
37+
[[[['foo', '123']], [['bar'], ['foo', '456']]], 'foo=123, bar; foo=456', ',;='],
38+
[[[['foo', 'a,b;c=d']]], 'foo="a,b;c=d"', ',;='],
39+
40+
[['foo', 'bar'], 'foo,,,, bar', ','],
41+
[['foo', 'bar'], ',foo, bar,', ','],
42+
[['foo', 'bar'], ' , foo, bar, ', ','],
43+
[['foo bar'], 'foo "bar"', ','],
44+
[['foo bar'], '"foo" bar', ','],
45+
[['foo bar'], '"foo" "bar"', ','],
46+
47+
[[['foo_cookie', 'foo=1&bar=2&baz=3'], ['expires', 'Tue, 22-Sep-2020 06:27:09 GMT'], ['path', '/']], 'foo_cookie=foo=1&bar=2&baz=3; expires=Tue, 22-Sep-2020 06:27:09 GMT; path=/', ';='],
48+
[[['foo_cookie', 'foo=='], ['expires', 'Tue, 22-Sep-2020 06:27:09 GMT'], ['path', '/']], 'foo_cookie=foo==; expires=Tue, 22-Sep-2020 06:27:09 GMT; path=/', ';='],
49+
[[['foo_cookie', 'foo=a=b'], ['expires', 'Tue, 22-Sep-2020 06:27:09 GMT'], ['path', '/']], 'foo_cookie=foo="a=b"; expires=Tue, 22-Sep-2020 06:27:09 GMT; path=/', ';='],
50+
51+
// These are not a valid header values. We test that they parse anyway,
52+
// and that both the valid and invalid parts are returned.
53+
[[], '', ','],
54+
[[], ',,,', ','],
55+
[['foo', 'bar', 'baz'], 'foo, "bar", "baz', ','],
56+
[['foo', 'bar, baz'], 'foo, "bar, baz', ','],
57+
[['foo', 'bar, baz\\'], 'foo, "bar, baz\\', ','],
58+
[['foo', 'bar, baz\\'], 'foo, "bar, baz\\\\', ','],
59+
];
4660
}
4761

4862
public function testCombine()

0 commit comments

Comments
 (0)