Project

General

Profile

Bug #14586 ยป rfc2396-uri-encoding.patch

kivikakk (Ashe Connor), 03/08/2018 01:53 AM

View differences:

lib/uri/rfc2396_parser.rb
315 315
    #
316 316
    # :call-seq:
317 317
    #   unescape( str )
318
    #   unescape( str, unsafe )
318
    #   unescape( str, escaped )
319 319
    #
320 320
    # == Args
321 321
    #
322 322
    # +str+::
323 323
    #    String to remove escapes from
324
    # +unsafe+::
324
    # +escaped+::
325 325
    #    Regexp to apply. Defaults to self.regexp[:ESCAPED]
326 326
    #
327 327
    # == Description
......
329 329
    # Removes escapes from +str+
330 330
    #
331 331
    def unescape(str, escaped = @regexp[:ESCAPED])
332
      str.gsub(escaped) { [$&[1, 2].hex].pack('C') }.force_encoding(str.encoding)
332
      enc = str.encoding
333
      str.dup.force_encoding(Encoding::ASCII_8BIT).gsub(escaped) { [$&[1, 2].hex].pack('C') }.force_encoding(enc)
333 334
    end
334 335

  
335 336
    @@to_s = Kernel.instance_method(:to_s)
test/uri/test_parser.rb
45 45
      URI.parse(1)
46 46
    end
47 47
  end
48

  
49
  def test_unescape
50
    p1 = URI::Parser.new
51
    assert_equal("\xe3\x83\x90", p1.unescape("\xe3\x83\x90"))
52
    assert_equal("\xe3\x83\x90", p1.unescape('%e3%83%90'))
53
    assert_equal("\xe3\x83\x90\xe3\x83\x90", p1.unescape("\xe3\x83\x90%e3%83%90"))
54
  end
48 55
end