lib/rack/urlmap.rb in rack-1.2.8 vs lib/rack/urlmap.rb in rack-1.3.0.beta
- old
+ new
@@ -10,15 +10,20 @@
#
# URLMap dispatches in such a way that the longest paths are tried
# first, since they are most specific.
class URLMap
+ NEGATIVE_INFINITY = -1.0 / 0.0
+
def initialize(map = {})
remap(map)
end
def remap(map)
+ longest_path_first = lambda do |(host, location, _, _)|
+ [host ? -host.size : NEGATIVE_INFINITY, -location.size]
+ end
@mapping = map.map { |location, app|
if location =~ %r{\Ahttps?://(.*?)(/.*)}
host, location = $1, $2
else
host = nil
@@ -29,10 +34,10 @@
end
location = location.chomp('/')
match = Regexp.new("^#{Regexp.quote(location).gsub('/', '/+')}(.*)", nil, 'n')
[host, location, match, app]
- }.sort_by { |(h, l, m, a)| [h ? -h.size : (-1.0 / 0.0), -l.size] } # Longest path first
+ }.sort_by(&longest_path_first)
end
def call(env)
path = env["PATH_INFO"]
script_name = env['SCRIPT_NAME']