Skip to content

Commit 49af042

Browse files
committed
Improve set-cookie parsing
1 parent db509fc commit 49af042

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

src/Igorw/CgiHttpKernel/CgiHttpKernel.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,30 +106,37 @@ private function getCookies(array $headerList)
106106

107107
private function cookieFromResponseHeaderValue($value)
108108
{
109-
preg_match_all('/(?P<names>[^=]+)=(?P<values>[^\;]*)(;\s)?/im', $value, $cookieParts);
109+
$cookieParts = preg_split('/;\s?/', $value);
110110
$cookieMap = array();
111-
foreach ($cookieParts['names'] as $key => $name) {
112-
$cookieMap[$name] = $cookieParts['values'][$key];
111+
foreach ($cookieParts as $part) {
112+
preg_match('/(\w+)(?:=(.*)|)/', $part, $capture);
113+
$name = $capture[1];
114+
$value = isset($capture[2]) ? $capture[2] : '';
115+
116+
$cookieMap[$name] = $value;
113117
}
114118

115119
$firstKey = key($cookieMap);
116120

121+
$cookieMap = array_merge($cookieMap, array(
122+
'secure' => isset($cookieMap['secure']),
123+
'httponly' => isset($cookieMap['httponly']),
124+
));
125+
117126
$cookieMap = array_merge(array(
118-
'expire' => 0,
127+
'expires' => 0,
119128
'path' => '/',
120129
'domain' => null,
121-
'secure' => false,
122-
'httpOnly' => true,
123130
), $cookieMap);
124131

125132
return new Cookie(
126133
$firstKey,
127134
$cookieMap[$firstKey],
128-
$cookieMap['expire'],
135+
$cookieMap['expires'],
129136
$cookieMap['path'],
130137
$cookieMap['domain'],
131138
$cookieMap['secure'],
132-
$cookieMap['httpOnly']
139+
$cookieMap['httponly']
133140
);
134141
}
135142

tests/Igorw/CgiHttpKernel/CgiHttpKernelTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,31 @@ public function isShouldParseMultipleCookiesFromResponse()
158158
$this->assertSame('quux', $cookies[1]->getValue());
159159
}
160160

161+
/** @test */
162+
public function isShouldParseEmptyCookieValue()
163+
{
164+
$request = Request::create('/cookie-set-empty.php');
165+
$response = $this->kernel->handle($request);
166+
167+
$cookies = $response->headers->getCookies();
168+
$this->assertSame('foo', $cookies[0]->getName());
169+
$this->assertSame('', $cookies[0]->getValue());
170+
}
171+
172+
/** @test */
173+
public function isShouldParseFullCookieValue()
174+
{
175+
$request = Request::create('/cookie-set-full.php');
176+
$response = $this->kernel->handle($request);
177+
178+
$cookies = $response->headers->getCookies();
179+
$this->assertSame('foo', $cookies[0]->getName());
180+
$this->assertSame('bar', $cookies[0]->getValue());
181+
$this->assertSame(1353842823, $cookies[0]->getExpiresTime());
182+
$this->assertSame('/baz', $cookies[0]->getPath());
183+
$this->assertSame('example.com', $cookies[0]->getDomain());
184+
}
185+
161186
/** @test */
162187
public function itShouldSetHttpAuth()
163188
{
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
setcookie('foo');
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
setcookie('foo', 'bar', 1353842823, '/baz', 'example.com', true, true);

0 commit comments

Comments
 (0)