blob: dadabb02e5adfd14550cd0ace6730fc94fecea82 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# frozen_string_literal: true
require "uri"
str = "\xE6\x97\xA5"
parser = URI::Parser.new
needs_monkeypatch =
begin
str + str != parser.unescape(str + parser.escape(str).force_encoding(Encoding::UTF_8))
rescue Encoding::CompatibilityError
true
end
if needs_monkeypatch
require "active_support/core_ext/module/redefine_method"
URI::Parser.class_eval do
silence_redefinition_of_method :unescape
def unescape(str, escaped = /%[a-fA-F\d]{2}/)
# TODO: Are we actually sure that ASCII == UTF-8?
# YK: My initial experiments say yes, but let's be sure please
enc = str.encoding
enc = Encoding::UTF_8 if enc == Encoding::US_ASCII
str.dup.force_encoding(Encoding::ASCII_8BIT).gsub(escaped) { |match| [match[1, 2].hex].pack("C") }.force_encoding(enc)
end
end
end
module URI
class << self
def parser
@parser ||= URI::Parser.new
end
end
end
|