ruby-changes:50554
From: naruse <ko1@a...>
Date: Thu, 8 Mar 2018 18:38:07 +0900 (JST)
Subject: [ruby-changes:50554] naruse:r62695 (trunk): fix error if the input is mixed Unicode and percent-escapes
naruse 2018-03-08 18:38:01 +0900 (Thu, 08 Mar 2018) New Revision: 62695 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=62695 Log: fix error if the input is mixed Unicode and percent-escapes Reported by kivikakk (Ashe Connor) with tests and doc fix Patch based on mame and fix by naruse [Bug #14586] Modified files: trunk/lib/uri/rfc2396_parser.rb trunk/test/uri/test_parser.rb Index: lib/uri/rfc2396_parser.rb =================================================================== --- lib/uri/rfc2396_parser.rb (revision 62694) +++ lib/uri/rfc2396_parser.rb (revision 62695) @@ -315,13 +315,13 @@ module URI https://github.com/ruby/ruby/blob/trunk/lib/uri/rfc2396_parser.rb#L315 # # :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,7 @@ module URI https://github.com/ruby/ruby/blob/trunk/lib/uri/rfc2396_parser.rb#L329 # Removes escapes from +str+ # def unescape(str, escaped = @regexp[:ESCAPED]) - str.gsub(escaped) { [$&[1, 2].hex].pack('C') }.force_encoding(str.encoding) + str.gsub(escaped) { [$&[1, 2]].pack('H2').force_encoding(str.encoding) } end @@to_s = Kernel.instance_method(:to_s) Index: test/uri/test_parser.rb =================================================================== --- test/uri/test_parser.rb (revision 62694) +++ test/uri/test_parser.rb (revision 62695) @@ -45,4 +45,11 @@ class URI::TestParser < Test::Unit::Test https://github.com/ruby/ruby/blob/trunk/test/uri/test_parser.rb#L45 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 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/