Skip to content

Commit

Permalink
Parse IIS Set-Cookie date format (#7405)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sija authored and sdogruyol committed Feb 11, 2019
1 parent eace840 commit a4419bd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
9 changes: 9 additions & 0 deletions spec/std/http/cookie_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ module HTTP
cookie.to_set_cookie_header.should eq("key=value; domain=www.example.com; path=/")
end

it "parses expires iis" do
cookie = parse_set_cookie("key=value; expires=Sun, 06-Nov-1994 08:49:37 GMT")
time = Time.utc(1994, 11, 6, 8, 49, 37)

cookie.name.should eq("key")
cookie.value.should eq("value")
cookie.expires.should eq(time)
end

it "parses expires rfc1123" do
cookie = parse_set_cookie("key=value; expires=Sun, 06 Nov 1994 08:49:37 GMT")
time = Time.utc(1994, 11, 6, 8, 49, 37)
Expand Down
3 changes: 2 additions & 1 deletion src/http/cookie.cr
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ module HTTP
Zone = /(?:UT|GMT|EST|EDT|CST|CDT|MST|MDT|PST|PDT|[+-]?\d{4})/
RFC1036Date = /#{Weekday}, \d{2}-#{Month}-\d{2} #{Time} GMT/
RFC1123Date = /#{Wkday}, \d{1,2} #{Month} \d{2,4} #{Time} #{Zone}/
IISDate = /#{Wkday}, \d{1,2}-#{Month}-\d{2,4} #{Time} GMT/
ANSICDate = /#{Wkday} #{Month} (?:\d{2}| \d) #{Time} \d{4}/
SaneCookieDate = /(?:#{RFC1123Date}|#{RFC1036Date}|#{ANSICDate})/
SaneCookieDate = /(?:#{RFC1123Date}|#{RFC1036Date}|#{IISDate}|#{ANSICDate})/
ExtensionAV = /(?<extension>[^\x00-\x1f\x7f]+)/
HttpOnlyAV = /(?<http_only>HttpOnly)/i
SecureAV = /(?<secure>Secure)/i
Expand Down
8 changes: 6 additions & 2 deletions src/time/format/custom/http_date.cr
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
require "./rfc_2822"

struct Time::Format
# Parse a time string using the formats specified by [RFC 2616](https://tools.ietf.org/html/rfc2616#section-3.3.1).
# Parse a time string using the formats specified by [RFC 2616](https://tools.ietf.org/html/rfc2616#section-3.3.1) and (non-RFC-compliant) [IIS date format](https://docs.microsoft.com/en-us/windows/desktop/wininet/http-cookies#set-cookie-header).
#
# Supported formats:
# * [RFC 1123](https://tools.ietf.org/html/rfc1123#page-55)
# * [RFC 850](https://tools.ietf.org/html/rfc850#section-2.1.4)
# * [IIS date format](https://docs.microsoft.com/en-us/windows/desktop/wininet/http-cookies#set-cookie-header)
# * [asctime](http://en.cppreference.com/w/c/chrono/asctime)
#
# ```
# Time::Format::HTTP_DATE.parse("Sun, 14 Feb 2016 21:00:00 GMT") # => 2016-02-14 21:00:00 UTC
# Time::Format::HTTP_DATE.parse("Sunday, 14-Feb-16 21:00:00 GMT") # => 2016-02-14 21:00:00 UTC
# Time::Format::HTTP_DATE.parse("Sun, 14-Feb-2016 21:00:00 GMT") # => 2016-02-14 21:00:00 UTC
# Time::Format::HTTP_DATE.parse("Sun Feb 14 21:00:00 2016") # => 2016-02-14 21:00:00 UTC
#
# Time::Format::HTTP_DATE.format(Time.utc(2016, 2, 15)) # => "Mon, 15 Feb 2016 00:00:00 GMT"
Expand Down Expand Up @@ -61,7 +63,9 @@ struct Time::Format
char '-'
short_month_name
char '-'
year_modulo_100
# Intentional departure from standard `year_modulo_100` because of IIS
# non-RFC-compliant date format, see https://docs.microsoft.com/en-us/windows/desktop/wininet/http-cookies#set-cookie-header
full_or_short_year
end

whitespace
Expand Down

0 comments on commit a4419bd

Please sign in to comment.