diff --git a/lib/uri/rfc2396_parser.rb b/lib/uri/rfc2396_parser.rb index b9e7b2b26e..e8e645d66c 100644 --- a/lib/uri/rfc2396_parser.rb +++ b/lib/uri/rfc2396_parser.rb @@ -315,13 +315,13 @@ def escape(str, unsafe = @regexp[:UNSAFE]) # # :call-seq: # unescape( str ) - # unescape( str, unsafe ) + # unescape( str, escaped ) # # == Args # # +str+:: # String to remove escapes from - # +unsafe+:: + # +escaped+:: # Regexp to apply. Defaults to self.regexp[:ESCAPED] # # == Description @@ -329,7 +329,8 @@ def escape(str, unsafe = @regexp[:UNSAFE]) # Removes escapes from +str+ # def unescape(str, escaped = @regexp[:ESCAPED]) - str.gsub(escaped) { [$&[1, 2].hex].pack('C') }.force_encoding(str.encoding) + enc = str.encoding + str.dup.force_encoding(Encoding::ASCII_8BIT).gsub(escaped) { [$&[1, 2].hex].pack('C') }.force_encoding(enc) end @@to_s = Kernel.instance_method(:to_s) diff --git a/test/uri/test_parser.rb b/test/uri/test_parser.rb index 757ac86c74..b9cf4b7e1a 100644 --- a/test/uri/test_parser.rb +++ b/test/uri/test_parser.rb @@ -45,4 +45,11 @@ def test_raise_bad_uri_for_integer URI.parse(1) end end + + def test_unescape + p1 = URI::Parser.new + assert_equal("\xe3\x83\x90", p1.unescape("\xe3\x83\x90")) + assert_equal("\xe3\x83\x90", p1.unescape('%e3%83%90')) + assert_equal("\xe3\x83\x90\xe3\x83\x90", p1.unescape("\xe3\x83\x90%e3%83%90")) + end end