Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fallback to ISO 8601 date format in ContentDispositionParser. #554

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Page::Link#uri now handles non-ASCII `href`s. (#569) @terryyin
* FileConnection supports Windows drive letters (#483)
* Credential headers 'Authorization' and 'Cookie' are deleted on cross-origin redirects. (#538) @kyoshidajp
* ContentDispositionParser handles ISO8601 date headers, to be robust with websites that ignore RFC2183. (#554) @reitermarkus

* Bug fix
* POST headers 'Content-Length', 'Content-MD5', and 'Content-Type' are deleted in a case-insensitive manner on redirects. Previously these headers were treated as case-sensitive.
Expand Down
13 changes: 12 additions & 1 deletion lib/mechanize/http/content_disposition_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Mechanize::HTTP
# * Missing disposition-type
# * Multiple semicolons
# * Whitespace around semicolons
# * Dates in ISO 8601 format

class Mechanize::HTTP::ContentDispositionParser

Expand Down Expand Up @@ -94,7 +95,17 @@ def parse_parameters
when /^filename$/ then
rfc_2045_value
when /^(creation|modification|read)-date$/ then
Time.rfc822 rfc_2045_quoted_string
date = rfc_2045_quoted_string

begin
Time.rfc822 date
rescue ArgumentError
begin
Time.iso8601 date
rescue ArgumentError
nil
end
end
when /^size$/ then
rfc_2045_value.to_i(10)
else
Expand Down
27 changes: 27 additions & 0 deletions test/test_mechanize_http_content_disposition_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,33 @@ def test_parse
assert_equal expected, content_disposition.parameters
end

def test_parse_date_iso8601_fallback
now = Time.at Time.now.to_i

content_disposition = @parser.parse \
'attachment;' \
'filename=value;' \
"creation-date=\"#{now.iso8601}\";" \
"modification-date=\"#{(now + 1).iso8601}\""

assert_equal 'attachment', content_disposition.type
assert_equal 'value', content_disposition.filename
assert_equal now, content_disposition.creation_date
assert_equal((now + 1), content_disposition.modification_date)
end

def test_parse_date_invalid
now = Time.at Time.now.to_i

content_disposition = @parser.parse \
'attachment;' \
'filename=value;' \
"creation-date=\"#{now.to_s}\";" \
"modification-date=\"#{(now + 1).to_s}\""

assert_nil content_disposition
end

def test_parse_header
content_disposition = @parser.parse \
'content-disposition: attachment;filename=value', true
Expand Down