php-http / message

HTTP Message related tools
http://php-http.org
MIT License
1.3k stars 42 forks source link

Implements a cookie-date parsing utility #76

Closed aaa2000 closed 7 years ago

aaa2000 commented 7 years ago
Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Related tickets mentioned in https://github.com/php-http/client-common/pull/46
Documentation No
License MIT

What's in this PR?

Implementation of a cookie-date parsing algorithm mentionned in https://github.com/php-http/client-common/pull/46. I preferred to use a simpler method from https://github.com/symfony/symfony/blob/master/src/Symfony/Component/BrowserKit/Cookie.php#L199.

Example Usage

$datetime = CookieUtil::parseDate('Friday, 31 Jul 20 08:49:37 GMT');

Checklist

aaa2000 commented 7 years ago

@dbu if we use only date_create, the tests will fail for this format Fri, 31-07-20 08:49:37 GMT.

aaa2000 commented 7 years ago

But this format is not conform to the rfc https://tools.ietf.org/html/rfc6265#section-5.1.1

month = "Jan" | "Feb" | "Mar" | "Apr" | "May" | "Jun" | "Jul" | "Aug" | "Sep" | "Oct" | "Nov" | "Dec"

it has been added to manage unusual cookie dates, see commit https://github.com/symfony/browser-kit/commit/47f146d9406d695ed1ca8256b41542e367442d2c

aaa2000 commented 7 years ago

Rebase done

I added more tests from library mentioned in php-http/client-common#46 https://github.com/salesforce/tough-cookie/blob/master/test/date_test.js except 01 Jan 1600 00:00:00 GMT which should not pass because the date before 1601.

Use only the function date_create all tests pass except Fri, 31-07-20 08:49:37 GMT. Format that does not appear to comply with RFC.

Maybe, CookieUtil::parseDate is not really useful.

aaa2000 commented 7 years ago

tests failed on PHP 5.4 and HHVM

aaa2000 commented 7 years ago

I will fix later

http://php.net/manual/en/class.datetime.php#datetime.changelog

5.4.24  The COOKIE constant was changed to reflect RFC 1036 using a four digit year rather than a two digit year (RFC 850) as prior versions. 
dbu commented 7 years ago

as hhvm works on master, did we just discover a difference between hhvm and php here?

the php 5.4 build fails on the same things, formatting the date: expected "Friday, 31-Jul-2020 08:49...", but got "Friday, 31-Jul-2020 08:49...". - unfortunately its cut off before any differences show up.

it seems to me that format(\DateTime::COOKIE) uses different formats in different versions. as we don't want to test the \DateFormat::format method, maybe just use an explicit formatting string so you can compare with a well known outcome?

aaa2000 commented 7 years ago

Yes it is format(\DateTime::COOKIE) https://3v4l.org/VpvRp, as you said, I will use use an explicit formatting string. I would do it tonight.

aaa2000 commented 7 years ago

throws an exception when the date can't be parsed and commits squashed

dbu commented 7 years ago

thanks a lot @aaa2000 !