Skip to content

Commit

Permalink
fix error if the input is mixed Unicode and percent-escapes
Browse files Browse the repository at this point in the history
Reported by kivikakk (Ashe Connor) with tests and doc fix
Patch based on mame and fix by naruse
[Bug #14586]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62695 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
nurse committed Mar 8, 2018
1 parent 0a2fb18 commit 6db869c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/uri/rfc2396_parser.rb
Expand Up @@ -315,21 +315,21 @@ 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
#
# 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)
Expand Down
7 changes: 7 additions & 0 deletions test/uri/test_parser.rb
Expand Up @@ -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

0 comments on commit 6db869c

Please sign in to comment.