Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#### Features

* [#2532](https://github.com/ruby-grape/grape/pull/2532): Update RuboCop 1.71.2 - [@ericproulx](https://github.com/ericproulx).
* [#2536](https://github.com/ruby-grape/grape/pull/2536): Update normalize_path like Rails - [@ericproulx](https://github.com/ericproulx).
* Your contribution here.

#### Fixes
Expand Down
17 changes: 14 additions & 3 deletions lib/grape/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,22 @@ class Router
attr_reader :map, :compiled

def self.normalize_path(path)
return +'/' unless path

# Fast path for the overwhelming majority of paths that don't need to be normalized
return path.dup if path == '/' || (path.start_with?('/') && !path.end_with?('/') && !path.match?(%r{%|//}))

# Slow path
encoding = path.encoding
path = +"/#{path}"
path.squeeze!('/')
path.sub!(%r{/+\Z}, '')
path = '/' if path == ''
path

unless path == '/'
path.delete_suffix!('/')
path.gsub!(/(%[a-f0-9]{2})/) { ::Regexp.last_match(1).upcase }
end

path.force_encoding(encoding)
end

def initialize
Expand Down