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

Refactor Addressable::URI.normalize_path, fix linter offenses #430

Merged
merged 2 commits into from Sep 25, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 17 additions & 12 deletions lib/addressable/uri.rb
Expand Up @@ -2440,30 +2440,35 @@ def defer_validation
def self.normalize_path(path)
# Section 5.2.4 of RFC 3986

return nil if path.nil?
return if path.nil?
normalized_path = path.dup
begin
mod = nil
loop do
mod ||= normalized_path.gsub!(RULE_2A, SLASH)

pair = normalized_path.match(RULE_2B_2C)
parent, current = pair[1], pair[2] if pair
if pair
parent = pair[1]
current = pair[2]
else
parent = nil
current = nil
end

regexp = "/#{Regexp.escape(parent.to_s)}/\\.\\./|"
regexp += "(/#{Regexp.escape(current.to_s)}/\\.\\.$)"

if pair && ((parent != SELF_REF && parent != PARENT) ||
(current != SELF_REF && current != PARENT))
mod ||= normalized_path.gsub!(
Regexp.new(
"/#{Regexp.escape(parent.to_s)}/\\.\\./|" +
"(/#{Regexp.escape(current.to_s)}/\\.\\.$)"
), SLASH
)
mod ||= normalized_path.gsub!(Regexp.new(regexp), SLASH)
end

mod ||= normalized_path.gsub!(RULE_2D, EMPTY_STR)
# Non-standard, removes prefixed dotted segments from path.
mod ||= normalized_path.gsub!(RULE_PREFIXED_PARENT, SLASH)
end until mod.nil?
break if mod.nil?
end

return normalized_path
normalized_path
end

##
Expand Down