Skip to content

Commit

Permalink
Fix proper error handling for early end in HTTP_DATE parser (#15232)
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota authored Dec 1, 2024
1 parent 62638f4 commit 4cf5748
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
62 changes: 34 additions & 28 deletions spec/std/http/http_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,45 @@ private def http_quote_string(string)
end

describe HTTP do
it "parses RFC 1123" do
time = Time.utc(1994, 11, 6, 8, 49, 37)
HTTP.parse_time("Sun, 06 Nov 1994 08:49:37 GMT").should eq(time)
end
describe ".parse_time" do
it "parses RFC 1123" do
time = Time.utc(1994, 11, 6, 8, 49, 37)
HTTP.parse_time("Sun, 06 Nov 1994 08:49:37 GMT").should eq(time)
end

it "parses RFC 1123 without day name" do
time = Time.utc(1994, 11, 6, 8, 49, 37)
HTTP.parse_time("06 Nov 1994 08:49:37 GMT").should eq(time)
end
it "parses RFC 1123 without day name" do
time = Time.utc(1994, 11, 6, 8, 49, 37)
HTTP.parse_time("06 Nov 1994 08:49:37 GMT").should eq(time)
end

it "parses RFC 1036" do
time = Time.utc(1994, 11, 6, 8, 49, 37)
HTTP.parse_time("Sunday, 06-Nov-94 08:49:37 GMT").should eq(time)
end
it "parses RFC 1036" do
time = Time.utc(1994, 11, 6, 8, 49, 37)
HTTP.parse_time("Sunday, 06-Nov-94 08:49:37 GMT").should eq(time)
end

it "parses ANSI C" do
time = Time.utc(1994, 11, 6, 8, 49, 37)
HTTP.parse_time("Sun Nov 6 08:49:37 1994").should eq(time)
time2 = Time.utc(1994, 11, 16, 8, 49, 37)
HTTP.parse_time("Sun Nov 16 08:49:37 1994").should eq(time2)
end
it "parses ANSI C" do
time = Time.utc(1994, 11, 6, 8, 49, 37)
HTTP.parse_time("Sun Nov 6 08:49:37 1994").should eq(time)
time2 = Time.utc(1994, 11, 16, 8, 49, 37)
HTTP.parse_time("Sun Nov 16 08:49:37 1994").should eq(time2)
end

it "parses and is UTC (#2744)" do
date = "Mon, 09 Sep 2011 23:36:00 GMT"
parsed_time = HTTP.parse_time(date).not_nil!
parsed_time.utc?.should be_true
end
it "parses and is UTC (#2744)" do
date = "Mon, 09 Sep 2011 23:36:00 GMT"
parsed_time = HTTP.parse_time(date).not_nil!
parsed_time.utc?.should be_true
end

it "parses and is local (#2744)" do
date = "Mon, 09 Sep 2011 23:36:00 -0300"
parsed_time = HTTP.parse_time(date).not_nil!
parsed_time.offset.should eq -3 * 3600
parsed_time.to_utc.to_s.should eq("2011-09-10 02:36:00 UTC")
it "parses and is local (#2744)" do
date = "Mon, 09 Sep 2011 23:36:00 -0300"
parsed_time = HTTP.parse_time(date).not_nil!
parsed_time.offset.should eq -3 * 3600
parsed_time.to_utc.to_s.should eq("2011-09-10 02:36:00 UTC")
end

it "handles errors" do
HTTP.parse_time("Thu").should be_nil
end
end

describe "generates HTTP date" do
Expand Down
1 change: 1 addition & 0 deletions src/time/format/custom/http_date.cr
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ struct Time::Format
ansi_c_format = current_char != ','
next_char unless ansi_c_format

raise "Invalid date format" unless current_char.ascii_whitespace?
whitespace

ansi_c_format
Expand Down

0 comments on commit 4cf5748

Please sign in to comment.