Skip to content

Commit

Permalink
Rename HTTP.rfc1123_date to HTTP.format_date
Browse files Browse the repository at this point in the history
The date format used for HTTP applications (such as cookies) according to [RFC 2612](https://tools.ietf.org/html/rfc2616#section-3.3.1)
is *not* an arbitrary [RFC 1123](https://tools.ietf.org/html/rfc1123#page-55) string but has a fixed timezone `GMT`. Therefore it
is not the same as `Time#to_rfc2822` and the method should not be called `rfc1123_date`.
  • Loading branch information
straight-shoota committed Jul 25, 2017
1 parent 4244a91 commit 558d19e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
6 changes: 3 additions & 3 deletions spec/std/http/http_spec.cr
Expand Up @@ -37,10 +37,10 @@ describe HTTP do
parsed_time.to_utc.to_s.should eq("2011-09-10 02:36:00 UTC")
end

describe "generates RFC 1123" do
describe "generates HTTP date" do
it "without time zone" do
time = Time.new(1994, 11, 6, 8, 49, 37, 0, Time::Kind::Utc)
HTTP.rfc1123_date(time).should eq("Sun, 06 Nov 1994 08:49:37 GMT")
HTTP.format_date(time).should eq("Sun, 06 Nov 1994 08:49:37 GMT")
end

it "with local time zone" do
Expand All @@ -49,7 +49,7 @@ describe HTTP do
LibC.tzset
begin
time = Time.new(1994, 11, 6, 8, 49, 37, 0, Time::Kind::Local)
HTTP.rfc1123_date(time).should eq(time.to_utc.to_s("%a, %d %b %Y %H:%M:%S GMT"))
HTTP.format_date(time).should eq(time.to_utc.to_s("%a, %d %b %Y %H:%M:%S GMT"))
ensure
ENV["TZ"] = tz
LibC.tzset
Expand Down
16 changes: 12 additions & 4 deletions src/http/common.cr
Expand Up @@ -228,14 +228,22 @@ module HTTP
nil
end

# Format a Time object as a String using the format specified by [RFC 1123](https://tools.ietf.org/html/rfc1123#page-55).
# Format a `Time` object as a `String` using the format specified as `sane-cookie-date`
# by [RFC 6265](https://tools.ietf.org/html/rfc6265#section-4.1.1) which is
# according to [RFC 2616](https://tools.ietf.org/html/rfc2616#section-3.3.1) a
# [RFC 1123](https://tools.ietf.org/html/rfc1123#page-55) format with explicit
# timezone `GMT` (interpreted as `UTC`).
#
# ```
# HTTP.rfc1123_date(Time.new(2016, 2, 15)) # => "Sun, 14 Feb 2016 21:00:00 GMT"
# HTTP.format_http_date(Time.new(2016, 2, 15)) # => "Sun, 14 Feb 2016 21:00:00 GMT"
# ```
# DEPRECATED: Use `Time#to_rfc2822` instead.
def self.format_date(time : Time) : String
time.to_utc.to_s("%a, %d %b %Y %H:%M:%S GMT")
end

# DEPRECATED: Use `HTTP.format_http_date` instead.
def self.rfc1123_date(time : Time) : String
time.to_rfc2822
format_date(time)
end

# Dequotes an [RFC 2616](https://tools.ietf.org/html/rfc2616#page-17)
Expand Down
2 changes: 1 addition & 1 deletion src/http/cookie.cr
Expand Up @@ -31,7 +31,7 @@ module HTTP
header << "#{URI.escape @name}=#{URI.escape value}"
header << "; domain=#{domain}" if domain
header << "; path=#{path}" if path
header << "; expires=#{expires.to_utc.to_rfc2822}" if expires
header << "; expires=#{HTTP.format_date(expires)}" if expires
header << "; Secure" if @secure
header << "; HttpOnly" if @http_only
header << "; #{@extension}" if @extension
Expand Down

0 comments on commit 558d19e

Please sign in to comment.