Skip to content

Commit 911566c

Browse files
committed
Dup components of a URI to avoid freeze issues.
1 parent 37e08df commit 911566c

1 file changed

Lines changed: 21 additions & 24 deletions

File tree

lib/rdf/model/uri.rb

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,7 @@ def self.parse(str)
169169
def self.normalize_path(path)
170170
output, input = "", path.to_s
171171
if input.encoding != Encoding::ASCII_8BIT
172-
input = input.dup if input.frozen?
173-
input = input.force_encoding(Encoding::ASCII_8BIT)
172+
input = input.dup.force_encoding(Encoding::ASCII_8BIT)
174173
end
175174
until input.empty?
176175
if input.match(RDS_2A)
@@ -228,8 +227,7 @@ def initialize(*args, validate: false, canonicalize: false, **options)
228227
if uri
229228
@value = uri.to_s
230229
if @value.encoding != Encoding::UTF_8
231-
@value = @value.dup if @value.frozen?
232-
@value.force_encoding(Encoding::UTF_8)
230+
@value.dup.force_encoding(Encoding::UTF_8)
233231
@value.freeze
234232
end
235233
else
@@ -842,16 +840,16 @@ def parse(value)
842840
user, password = userinfo.to_s.split(':', 2)
843841
host, port = hostport.to_s.split(':', 2)
844842

845-
parts[:scheme] = (scheme.force_encoding(Encoding::UTF_8) if scheme)
846-
parts[:authority] = (authority.force_encoding(Encoding::UTF_8) if authority)
847-
parts[:userinfo] = (userinfo.force_encoding(Encoding::UTF_8) if userinfo)
848-
parts[:user] = (user.force_encoding(Encoding::UTF_8) if user)
849-
parts[:password] = (password.force_encoding(Encoding::UTF_8) if password)
850-
parts[:host] = (host.force_encoding(Encoding::UTF_8) if host)
843+
parts[:scheme] = (scheme.dup.force_encoding(Encoding::UTF_8) if scheme)
844+
parts[:authority] = (authority.dup.force_encoding(Encoding::UTF_8) if authority)
845+
parts[:userinfo] = (userinfo.dup.force_encoding(Encoding::UTF_8) if userinfo)
846+
parts[:user] = (user.dup.force_encoding(Encoding::UTF_8) if user)
847+
parts[:password] = (password.dup.force_encoding(Encoding::UTF_8) if password)
848+
parts[:host] = (host.dup.force_encoding(Encoding::UTF_8) if host)
851849
parts[:port] = (::URI.decode(port).to_i if port)
852-
parts[:path] = (path.to_s.force_encoding(Encoding::UTF_8) unless path.empty?)
853-
parts[:query] = (query[1..-1].force_encoding(Encoding::UTF_8) if query)
854-
parts[:fragment] = (fragment[1..-1].force_encoding(Encoding::UTF_8) if fragment)
850+
parts[:path] = (path.to_s.dup.force_encoding(Encoding::UTF_8) unless path.empty?)
851+
parts[:query] = (query[1..-1].dup.force_encoding(Encoding::UTF_8) if query)
852+
parts[:fragment] = (fragment[1..-1].dup.force_encoding(Encoding::UTF_8) if fragment)
855853
end
856854

857855
parts
@@ -869,7 +867,7 @@ def scheme
869867
# @param [String, #to_s] value
870868
# @return [RDF::URI] self
871869
def scheme=(value)
872-
object[:scheme] = (value.to_s.force_encoding(Encoding::UTF_8) if value)
870+
object[:scheme] = (value.to_s.dup.force_encoding(Encoding::UTF_8) if value)
873871
@value = nil
874872
self
875873
end
@@ -893,7 +891,7 @@ def user
893891
# @param [String, #to_s] value
894892
# @return [RDF::URI] self
895893
def user=(value)
896-
object[:user] = (value.to_s.force_encoding(Encoding::UTF_8) if value)
894+
object[:user] = (value.to_s.dup.force_encoding(Encoding::UTF_8) if value)
897895
@object[:userinfo] = format_userinfo("")
898896
@object[:authority] = format_authority
899897
@value = nil
@@ -919,7 +917,7 @@ def password
919917
# @param [String, #to_s] value
920918
# @return [RDF::URI] self
921919
def password=(value)
922-
object[:password] = (value.to_s.force_encoding(Encoding::UTF_8) if value)
920+
object[:password] = (value.to_s.dup.force_encoding(Encoding::UTF_8) if value)
923921
@object[:userinfo] = format_userinfo("")
924922
@object[:authority] = format_authority
925923
@value = nil
@@ -947,7 +945,7 @@ def host
947945
# @param [String, #to_s] value
948946
# @return [RDF::URI] self
949947
def host=(value)
950-
object[:host] = (value.to_s.force_encoding(Encoding::UTF_8) if value)
948+
object[:host] = (value.to_s.dup.force_encoding(Encoding::UTF_8) if value)
951949
@object[:authority] = format_authority
952950
@value = nil
953951
self
@@ -1010,7 +1008,7 @@ def path=(value)
10101008
if value
10111009
# Always lead with a slash
10121010
value = "/#{value}" if host && value.to_s.match?(/^[^\/]/)
1013-
object[:path] = value.to_s.force_encoding(Encoding::UTF_8)
1011+
object[:path] = value.to_s.dup.force_encoding(Encoding::UTF_8)
10141012
else
10151013
object[:path] = nil
10161014
end
@@ -1069,7 +1067,7 @@ def query
10691067
# @param [String, #to_s] value
10701068
# @return [RDF::URI] self
10711069
def query=(value)
1072-
object[:query] = (value.to_s.force_encoding(Encoding::UTF_8) if value)
1070+
object[:query] = (value.to_s.dup.force_encoding(Encoding::UTF_8) if value)
10731071
@value = nil
10741072
self
10751073
end
@@ -1093,7 +1091,7 @@ def fragment
10931091
# @param [String, #to_s] value
10941092
# @return [RDF::URI] self
10951093
def fragment=(value)
1096-
object[:fragment] = (value.to_s.force_encoding(Encoding::UTF_8) if value)
1094+
object[:fragment] = (value.to_s.dup.force_encoding(Encoding::UTF_8) if value)
10971095
@value = nil
10981096
self
10991097
end
@@ -1118,7 +1116,7 @@ def authority
11181116
# @return [RDF::URI] self
11191117
def authority=(value)
11201118
object.delete_if {|k, v| [:user, :password, :host, :port, :userinfo].include?(k)}
1121-
object[:authority] = (value.to_s.force_encoding(Encoding::UTF_8) if value)
1119+
object[:authority] = (value.to_s.dup.force_encoding(Encoding::UTF_8) if value)
11221120
user; password; userinfo; host; port
11231121
@value = nil
11241122
self
@@ -1148,7 +1146,7 @@ def userinfo
11481146
# @return [RDF::URI] self
11491147
def userinfo=(value)
11501148
object.delete_if {|k, v| [:user, :password, :authority].include?(k)}
1151-
object[:userinfo] = (value.to_s.force_encoding(Encoding::UTF_8) if value)
1149+
object[:userinfo] = (value.to_s.dup.force_encoding(Encoding::UTF_8) if value)
11521150
user; password; authority
11531151
@value = nil
11541152
self
@@ -1294,8 +1292,7 @@ def self._load(data)
12941292
# @return [String]
12951293
def normalize_segment(value, expr, downcase = false)
12961294
if value
1297-
value = value.dup if value.frozen?
1298-
value = value.force_encoding(Encoding::UTF_8)
1295+
value = value.dup.force_encoding(Encoding::UTF_8)
12991296
decoded = ::URI.decode(value)
13001297
decoded.downcase! if downcase
13011298
::URI.encode(decoded, /[^(?:#{expr})]/)

0 commit comments

Comments
 (0)