Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check Encodings before calling force_encoding in Addressable::URI #341

Merged
merged 7 commits into from
Aug 18, 2022
Next Next commit
Check to see if @normalized_* variables are already UTF-8 before
calling force_encoding on them

This prevents unnecessary mutation of these variables when already set
and appropriately encoded. Necessary for our use case as we freeze our
Addressable::URI objects to ensure they don't get changed when passed around
  • Loading branch information
ACBullen authored and baseballlover723 committed Aug 31, 2021
commit 2e08cefa2a31283589373675f4bcd9ac15321735
38 changes: 28 additions & 10 deletions lib/addressable/uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,9 @@ def normalized_scheme
end
end
# All normalized values should be UTF-8
@normalized_scheme.force_encoding(Encoding::UTF_8) if @normalized_scheme
if @normalized_scheme && @normalized_scheme.encoding != Encoding::UTF_8
@normalized_scheme.force_encoding(Encoding::UTF_8)
end
@normalized_scheme
end

Expand Down Expand Up @@ -955,7 +957,9 @@ def normalized_user
end
end
# All normalized values should be UTF-8
@normalized_user.force_encoding(Encoding::UTF_8) if @normalized_user
if @normalized_user && @normalized_user.encoding != Encoding::UTF_8
@normalized_user.force_encoding(Encoding::UTF_8)
end
@normalized_user
end

Expand Down Expand Up @@ -1012,7 +1016,9 @@ def normalized_password
end
end
# All normalized values should be UTF-8
if @normalized_password
if @normalized_password && (
@normalized_password.encoding != Encoding::UTF_8
)
@normalized_password.force_encoding(Encoding::UTF_8)
end
@normalized_password
Expand Down Expand Up @@ -1082,7 +1088,9 @@ def normalized_userinfo
end
end
# All normalized values should be UTF-8
if @normalized_userinfo
if @normalized_userinfo && (
@normalized_userinfo.encoding != Encoding::UTF_8
)
@normalized_userinfo.force_encoding(Encoding::UTF_8)
end
@normalized_userinfo
Expand Down Expand Up @@ -1151,7 +1159,7 @@ def normalized_host
end
end
# All normalized values should be UTF-8
if @normalized_host && !@normalized_host.empty?
if @normalized_host && @normalized_host.encoding != Encoding::UTF_8
@normalized_host.force_encoding(Encoding::UTF_8)
end
@normalized_host
Expand Down Expand Up @@ -1271,7 +1279,9 @@ def normalized_authority
authority
end
# All normalized values should be UTF-8
if @normalized_authority
if @normalized_authority && (
@normalized_authority.encoding != Encoding::UTF_8
)
@normalized_authority.force_encoding(Encoding::UTF_8)
end
@normalized_authority
Expand Down Expand Up @@ -1507,7 +1517,9 @@ def normalized_site
site_string
end
# All normalized values should be UTF-8
@normalized_site.force_encoding(Encoding::UTF_8) if @normalized_site
if @normalized_site && @normalized_site.encoding != Encoding::UTF_8
@normalized_site.force_encoding(Encoding::UTF_8)
end
@normalized_site
end

Expand Down Expand Up @@ -1570,7 +1582,9 @@ def normalized_path
result
end
# All normalized values should be UTF-8
@normalized_path.force_encoding(Encoding::UTF_8) if @normalized_path
if @normalized_path && @normalized_path.encoding != Encoding::UTF_8
@normalized_path.force_encoding(Encoding::UTF_8)
end
@normalized_path
end

Expand Down Expand Up @@ -1646,7 +1660,9 @@ def normalized_query(*flags)
component == "" ? nil : component
end
# All normalized values should be UTF-8
@normalized_query.force_encoding(Encoding::UTF_8) if @normalized_query
if @normalized_query && @normalized_query.encoding != Encoding::UTF_8
@normalized_query.force_encoding(Encoding::UTF_8)
end
@normalized_query
end

Expand Down Expand Up @@ -1842,7 +1858,9 @@ def normalized_fragment
component == "" ? nil : component
end
# All normalized values should be UTF-8
if @normalized_fragment
if @normalized_fragment && (
@normalized_fragment.encoding != Encoding::UTF_8
)
@normalized_fragment.force_encoding(Encoding::UTF_8)
end
@normalized_fragment
Expand Down