Skip to content

Commit

Permalink
Use encode! instead of force_encoding where applicable
Browse files Browse the repository at this point in the history
Calling force_encoding doesn't actually change anything about the string,
it just changes what the string considers its encoding to be, allowing you
to end up with strings with invalid encodings. There are some invocations
in Addressable::URI where this seems intentional (followed by some g_sub'ing)
but at least at the output stage it seems like the strings should be converted
and the non-utf-8 bytes/characters removed
  • Loading branch information
ACBullen committed Apr 18, 2019
1 parent 686dbbb commit bfc9a44
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions lib/addressable/uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ def self.normalize_component(component, character_class=
rescue ArgumentError
encoded = self.encode_component(unencoded)
end
encoded.force_encoding(Encoding::UTF_8)
encoded.encode!(Encoding::UTF_8, invalid: :replace, replace: "")
return encoded
end

Expand Down Expand Up @@ -866,7 +866,7 @@ def normalized_scheme
end
# All normalized values should be UTF-8
if @normalized_scheme && @normalized_scheme.encoding != Encoding::UTF_8
@normalized_scheme.force_encoding(Encoding::UTF_8)
@normalized_scheme.encode!(Encoding::UTF_8, invalid: :replace, replace: "")
end
@normalized_scheme
end
Expand Down Expand Up @@ -923,7 +923,7 @@ def normalized_user
end
# All normalized values should be UTF-8
if @normalized_user && @normalized_user.encoding != Encoding::UTF_8
@normalized_user.force_encoding(Encoding::UTF_8)
@normalized_user.encode!(Encoding::UTF_8, invalid: :replace, replace: "")
end
@normalized_user
end
Expand Down Expand Up @@ -982,7 +982,7 @@ def normalized_password
end
# All normalized values should be UTF-8
if @normalized_password && @normalized_password.encoding != Encoding::UTF_8
@normalized_password.force_encoding(Encoding::UTF_8)
@normalized_password.encode!(Encoding::UTF_8, invalid: :replace, replace: "")
end
@normalized_password
end
Expand Down Expand Up @@ -1052,7 +1052,7 @@ def normalized_userinfo
end
# All normalized values should be UTF-8
if @normalized_userinfo && @normalized_userinfo.encoding != Encoding::UTF_8
@normalized_userinfo.force_encoding(Encoding::UTF_8)
@normalized_userinfo.encode!(Encoding::UTF_8, invalid: :replace, replace: "")
end
@normalized_userinfo
end
Expand Down Expand Up @@ -1119,7 +1119,7 @@ def normalized_host
end
# All normalized values should be UTF-8
if @normalized_host && @normalized_host.encoding != Encoding::UTF_8
@normalized_host.force_encoding(Encoding::UTF_8)
@normalized_host.encode!(Encoding::UTF_8, invalid: :replace, replace: "")
end
@normalized_host
end
Expand Down Expand Up @@ -1239,7 +1239,7 @@ def normalized_authority
end
# All normalized values should be UTF-8
if @normalized_authority && @normalized_authority.encoding != Encoding::UTF_8
@normalized_authority.force_encoding(Encoding::UTF_8)
@normalized_authority.encode!(Encoding::UTF_8, invalid: :replace, replace: "")
end
@normalized_authority
end
Expand Down Expand Up @@ -1475,7 +1475,7 @@ def normalized_site
end
# All normalized values should be UTF-8
if @normalized_site && @normalized_site.encoding != Encoding::UTF_8
@normalized_site.force_encoding(Encoding::UTF_8)
@normalized_site.encode!(Encoding::UTF_8, invalid: :replace, replace: "")
end
@normalized_site
end
Expand Down Expand Up @@ -1540,7 +1540,7 @@ def normalized_path
end
# All normalized values should be UTF-8
if @normalized_path && @normalized_path.encoding != Encoding::UTF_8
@normalized_path.force_encoding(Encoding::UTF_8)
@normalized_path.encode!(Encoding::UTF_8, invalid: :replace, replace: "")
end
@normalized_path
end
Expand Down Expand Up @@ -1613,7 +1613,7 @@ def normalized_query(*flags)
end
# All normalized values should be UTF-8
if @normalized_query && @normalized_query.encoding != Encoding::UTF_8
@normalized_query.force_encoding(Encoding::UTF_8)
@normalized_query.encode!(Encoding::UTF_8, invalid: :replace, replace: "")
end
@normalized_query
end
Expand Down Expand Up @@ -1809,7 +1809,7 @@ def normalized_fragment
end
# All normalized values should be UTF-8
if @normalized_fragment && @normalized_fragment.encoding != Encoding::UTF_8
@normalized_fragment.force_encoding(Encoding::UTF_8)
@normalized_fragment.encode!(Encoding::UTF_8, invalid: :replace, replace: "")
end
@normalized_fragment
end
Expand Down Expand Up @@ -2337,7 +2337,7 @@ def to_s
uri_string << self.path.to_s
uri_string << "?#{self.query}" if self.query != nil
uri_string << "##{self.fragment}" if self.fragment != nil
uri_string.force_encoding(Encoding::UTF_8)
uri_string.encode!(Encoding::UTF_8, invalid: :replace, replace: "")
uri_string
end
end
Expand Down

0 comments on commit bfc9a44

Please sign in to comment.