merged branch TrackIF/master (PR #7149)

This PR was submitted for the master branch but it was merged into the 2.1 branch instead (closes #7149).

Commits
-------

0c25d41 Expanded fault-tolerance for unusual cookie dates

Discussion
----------

Expanded fault-tolerance for unusual cookie dates

Building on pull #1793, this resolves situations where the Cookie's date field uses a numeric month. Also, expanding on the 7 most typical formats we fall-back to date_create() before throwing an exception.

---------------------------------------------------------------------------

by vicb at 2013-02-21T17:30:28Z

Please add some unit tests for the new formats.

---------------------------------------------------------------------------

by ecaron at 2013-02-21T18:06:46Z

Sorry for neglecting the unit tests, they've been updated (2 matching new common date formats, 1 uncommon date format, and changing the existing bad-date check to be more realistically bad.)

I also changed from strtotime to date_create to match the existing DateTime::createFromFormat check (although in my cookiejar analysis leading to this pull requests, all the cookies I'd encountered had timezones in them.) I'm using date_create vs. constructing a DateTime so I can immediately rely on the return value.

---------------------------------------------------------------------------

by ecaron at 2013-02-21T18:21:03Z

@vicb The two Travis failures are against the master branch unrelated to my changes. Should I retarget this pull against 2.3, or what would you advise to get this pull accepted?

---------------------------------------------------------------------------

by vicb at 2013-02-21T19:40:59Z

The Travis failure come for a bug in PHPUnit (there is a Sf issue for that).

There is no 2.3 branch yet (devs happen in master).

@fabpot will decide wether this should be considered a a fix (and merge to former releases) or an enhancement which will be merged to 2.3.

_(Could you please update the PR header which still refers to strtotime, thanks)_

---------------------------------------------------------------------------

by fabpot at 2013-02-21T21:37:15Z

This should probably go into 2.0. Also, do you have a reference where those 7 formats are explained/described?

---------------------------------------------------------------------------

by ecaron at 2013-02-21T23:10:38Z

@fabpot I couldn't find a reference because the cookies that we're addressing are ones that are behaving outside the spec (at least what I understand from http://curl.haxx.se/rfc/cookie_spec.html), as pull #1793 began to address and this continues. The cases that I've added are ones that I have encountered over the weeks of using BrowserKit and Goutte.
This commit is contained in:
Fabien Potencier 2013-02-22 07:39:07 +01:00
commit 0fb397ccfb
2 changed files with 11 additions and 1 deletions

View File

@ -30,6 +30,8 @@ class Cookie
'D, d M Y H:i:s T',
'D, d-M-y H:i:s T',
'D, d-M-Y H:i:s T',
'D, d-m-y H:i:s T',
'D, d-m-Y H:i:s T',
'D M j G:i:s Y',
'D M d H:i:s Y T',
);
@ -203,6 +205,11 @@ class Cookie
}
}
// attempt a fallback for unusual formatting
if (false !== $date = date_create($dateValue, new \DateTimeZone('GMT'))) {
return $date->getTimestamp();
}
throw new \InvalidArgumentException(sprintf('Could not parse date "%s".', $dateValue));
}

View File

@ -56,9 +56,12 @@ class CookieTest extends \PHPUnit_Framework_TestCase
return array(
array('foo=bar; expires=Fri, 31-Jul-2020 08:49:37 GMT'),
array('foo=bar; expires=Fri, 31 Jul 2020 08:49:37 GMT'),
array('foo=bar; expires=Fri, 31-07-2020 08:49:37 GMT'),
array('foo=bar; expires=Fri, 31-07-20 08:49:37 GMT'),
array('foo=bar; expires=Friday, 31-Jul-20 08:49:37 GMT'),
array('foo=bar; expires=Fri Jul 31 08:49:37 2020'),
array('foo=bar; expires=\'Fri Jul 31 08:49:37 2020\''),
array('foo=bar; expires=Friday July 31st 2020, 08:49:37 GMT'),
);
}
@ -86,7 +89,7 @@ class CookieTest extends \PHPUnit_Framework_TestCase
public function testFromStringThrowsAnExceptionIfCookieDateIsNotValid()
{
$this->setExpectedException('InvalidArgumentException');
Cookie::FromString('foo=bar; expires=foo');
Cookie::FromString('foo=bar; expires=Flursday July 31st 2020, 08:49:37 GMT');
}
public function testFromStringThrowsAnExceptionIfUrlIsNotValid()