Browse files

Escape windows special file names. Issue #163

  • Loading branch information...
1 parent 511b743 commit 074cc2dd8f9dcd9cd84fb4b107f54a192c5e7596 @drbrain drbrain committed Nov 8, 2011
Showing with 73 additions and 7 deletions.
  1. +29 −0 lib/mechanize/parser.rb
  2. +44 −7 test/test_mechanize_parser.rb
View
29 lib/mechanize/parser.rb
@@ -6,6 +6,31 @@ module Mechanize::Parser
extend Forwardable
+ SPECIAL_FILENAME = %w[
+ AUX
+ COM1
+ COM2
+ COM3
+ COM4
+ COM5
+ COM6
+ COM7
+ COM8
+ COM9
+ CON
+ LPT1
+ LPT2
+ LPT3
+ LPT4
+ LPT5
+ LPT6
+ LPT7
+ LPT8
+ LPT9
+ NUL
+ PRN
+ ]
+
##
# The URI this file was retrieved from
@@ -89,6 +114,10 @@ def extract_filename full_path = @full_path
filename << "?#{@uri.query}" if @uri.query
end
+ if SPECIAL_FILENAME.include? filename then
+ filename = "_#{filename}"
+ end
+
@filename = if full_path then
File.join @uri.host, path, filename
else
View
51 test/test_mechanize_parser.rb
@@ -48,6 +48,16 @@ def test_extract_filename_content_disposition
assert_equal 'genome.jpeg', @parser.extract_filename
end
+ def test_extract_filename_content_disposition_bad
+ @parser.uri = URI 'http://example/foo'
+
+ @parser.response = {
+ 'content-disposition' => 'attachment;; filename=genome.jpeg'
+ }
+
+ assert_equal 'genome.jpeg', @parser.extract_filename
+ end
+
def test_extract_filename_content_disposition_path
@parser.uri = URI 'http://example'
@@ -86,14 +96,41 @@ def test_extract_filename_content_disposition_full_path
assert_equal 'example/genome.jpeg', @parser.extract_filename(true)
end
- def test_extract_filename_content_disposition_bad
- @parser.uri = URI 'http://example/foo'
-
- @parser.response = {
- 'content-disposition' => 'attachment;; filename=genome.jpeg'
- }
+ def test_extract_filename_content_disposition_windows_special
+ @parser.uri = URI 'http://example'
- assert_equal 'genome.jpeg', @parser.extract_filename
+ windows_special = %w[
+ AUX
+ COM1
+ COM2
+ COM3
+ COM4
+ COM5
+ COM6
+ COM7
+ COM8
+ COM9
+ CON
+ LPT1
+ LPT2
+ LPT3
+ LPT4
+ LPT5
+ LPT6
+ LPT7
+ LPT8
+ LPT9
+ NUL
+ PRN
+ ]
+
+ windows_special.each do |special|
+ @parser.response = {
+ 'content-disposition' => "attachment; filename=#{special}"
+ }
+
+ assert_equal "_#{special}", @parser.extract_filename
+ end
end
def test_extract_filename_uri

0 comments on commit 074cc2d

Please sign in to comment.