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 if calling force_encoding is necessary before doing so in Addressable::URI, replace with encode! where applicable #340

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 28 additions & 16 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 @@ -865,7 +865,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.encode!(Encoding::UTF_8, invalid: :replace, replace: "")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [83/80]

end
@normalized_scheme
end

Expand Down Expand Up @@ -920,7 +922,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.encode!(Encoding::UTF_8, invalid: :replace, replace: "")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [81/80]

end
@normalized_user
end

Expand Down Expand Up @@ -977,8 +981,8 @@ def normalized_password
end
end
# All normalized values should be UTF-8
if @normalized_password
@normalized_password.force_encoding(Encoding::UTF_8)
if @normalized_password && @normalized_password.encoding != Encoding::UTF_8
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [81/80]

@normalized_password.encode!(Encoding::UTF_8, invalid: :replace, replace: "")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [85/80]

end
@normalized_password
end
Expand Down Expand Up @@ -1047,8 +1051,8 @@ def normalized_userinfo
end
end
# All normalized values should be UTF-8
if @normalized_userinfo
@normalized_userinfo.force_encoding(Encoding::UTF_8)
if @normalized_userinfo && @normalized_userinfo.encoding != Encoding::UTF_8
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [81/80]

@normalized_userinfo.encode!(Encoding::UTF_8, invalid: :replace, replace: "")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [85/80]

end
@normalized_userinfo
end
Expand Down Expand Up @@ -1114,7 +1118,9 @@ def normalized_host
end
end
# All normalized values should be UTF-8
@normalized_host.force_encoding(Encoding::UTF_8) if @normalized_host
if @normalized_host && @normalized_host.encoding != Encoding::UTF_8
@normalized_host.encode!(Encoding::UTF_8, invalid: :replace, replace: "")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [81/80]

end
@normalized_host
end

Expand Down Expand Up @@ -1232,8 +1238,8 @@ def normalized_authority
authority
end
# All normalized values should be UTF-8
if @normalized_authority
@normalized_authority.force_encoding(Encoding::UTF_8)
if @normalized_authority && @normalized_authority.encoding != Encoding::UTF_8
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [83/80]

@normalized_authority.encode!(Encoding::UTF_8, invalid: :replace, replace: "")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [86/80]

end
@normalized_authority
end
Expand Down Expand Up @@ -1468,7 +1474,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.encode!(Encoding::UTF_8, invalid: :replace, replace: "")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [81/80]

end
@normalized_site
end

Expand Down Expand Up @@ -1531,7 +1539,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.encode!(Encoding::UTF_8, invalid: :replace, replace: "")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/IndentationWidth: Use 2 (not 3) spaces for indentation.
Metrics/LineLength: Line is too long. [82/80]

end
@normalized_path
end

Expand Down Expand Up @@ -1602,7 +1612,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.encode!(Encoding::UTF_8, invalid: :replace, replace: "")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [82/80]

end
@normalized_query
end

Expand Down Expand Up @@ -1796,8 +1808,8 @@ def normalized_fragment
component == "" ? nil : component
end
# All normalized values should be UTF-8
if @normalized_fragment
@normalized_fragment.force_encoding(Encoding::UTF_8)
if @normalized_fragment && @normalized_fragment.encoding != Encoding::UTF_8
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [81/80]

@normalized_fragment.encode!(Encoding::UTF_8, invalid: :replace, replace: "")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [85/80]

end
@normalized_fragment
end
Expand Down Expand Up @@ -2325,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