parse_cookie_date is a little utility to parse the wide variety of dates formats that cookies can contain.
C++
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.gitignore
README
main.cpp
parse_cookie_date.pro

README

parse_cookie_date is a little utility to parse the wide variety of dates formats that cookies can contain.

The RFC describing the date format the http cookies contain specify that only three date formats are accepted and otherwise the cookie is invalid.  Unfortunately browsers have supported a wider and wider range of "formats".

For example a cookie can pass the expiration date of "30 02 20" and because 2 must be the month and 30 can't be the day it is "Wed Feb 20 00:00:00 2030"

This type of logic grew and grew until you ended up with the code that is found in FireFox (and no doubt elsewhere) today.  A lot of code was added to FireFox's source be be able to determine if a numbers is the month, day or year.  If statement upon if statement ending with "} // else what the hell is this."

The code in this utility is a small, readable, and maintainable parser that is able to parse the wide variety of strings that are passed as cookie dates.

The code works by first parsing the string gathering what it can determine with absolute confidence (example: "jan" == The month January).  After that it takes the remaining numbers and marks what each number could be (example: 19 could be a year or day).  Then it removes any marks that would result in an invalid date.  (example: 31 could be a day, but not if the other two numbers are 11 and 6).  It then set the known values and iteratively reduce the set until all values are known.  Lastly fall back to the standardized order to fill in the values.

I am proud of the solution as it is so much more simpler than other solutions I have seen and especially the elegance of how leap years are handled.

Some more examples:

// April only has 30 days
31 30 4 1:1        => Wed Apr 30 01:01:00 2031

// Nov and June don't have 31 days
// Year is known, then fall back to FireFox defaults of filling in month and day
31 11 01           => Sat Nov 1 00:00:00 2031

// And More:
2 29 1             => Thu Feb 1 00:00:00 2029
31 11 20 1:1       => Thu Nov 20 01:01:00 2031
31 21 9            => Sun Sep 21 00:00:00 2031

Along with the standards such as:
Saturday, 01-Jan-00 23:12:40 GMT

This code was originally wrote for (and is part of) the Qt networking code and in the Qt source code can be found in src/network/access/qnetworkcookie.cpp.   Extensive unit tests and fuzz testing were also written and submitted with the original contribution and can be found in tests/auto/qnetworkcookiejar/tst_qnetworkcookiejar.cpp.

--------
BUILDING
--------
qmake
make

./parse_cookie_date "Jan 1 1980 7:20:01 EST"