Rack::URLMap takes a hash mapping urls or paths to apps, and dispatches accordingly. Support for HTTP/1.1 host names exists if the URLs start with http:// or https://.
URLMap modifies the SCRIPT_NAME and PATH_INFO such that the part relevant for dispatch is in the SCRIPT_NAME, and the rest in the PATH_INFO. This should be taken care of when you need to reconstruct the URL in order to create links.
URLMap dispatches in such a way that the longest paths are tried first, since they are most specific.
(Not documented)
# File lib/rack/urlmap.rb, line 37
37: def call(env)
38: path = env["PATH_INFO"].to_s
39: script_name = env['SCRIPT_NAME']
40: hHost, sName, sPort = env.values_at('HTTP_HOST','SERVER_NAME','SERVER_PORT')
41: @mapping.each { |host, location, match, app|
42: next unless (hHost == host || sName == host \
43: || (host.nil? && (hHost == sName || hHost == sName+':'+sPort)))
44: next unless path =~ match && rest = $1
45: next unless rest.empty? || rest[0] == ?/
46:
47: return app.call(
48: env.merge(
49: 'SCRIPT_NAME' => (script_name + location),
50: 'PATH_INFO' => rest))
51: }
52: [404, {"Content-Type" => "text/plain", "X-Cascade" => "pass"}, ["Not Found: #{path}"]]
53: end
(Not documented)
# File lib/rack/urlmap.rb, line 19
19: def remap(map)
20: @mapping = map.map { |location, app|
21: if location =~ %r{\Ahttps?://(.*?)(/.*)}
22: host, location = $1, $2
23: else
24: host = nil
25: end
26:
27: unless location[0] == ?/
28: raise ArgumentError, "paths need to start with /"
29: end
30: location = location.chomp('/')
31: match = Regexp.new("^#{Regexp.quote(location).gsub('/', '/+')}(.*)", nil, 'n')
32:
33: [host, location, match, app]
34: }.sort_by { |(h, l, m, a)| [h ? -h.size : (-1.0 / 0.0), -l.size] } # Longest path first
35: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.