Parent

Methods

Class Index [+]

Quicksearch

Rack::URLMap

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.

Public Class Methods

new(map = {}) click to toggle source
    # File lib/rack/urlmap.rb, line 15
15:     def initialize(map = {})
16:       remap(map)
17:     end

Public Instance Methods

call(env) click to toggle source
    # File lib/rack/urlmap.rb, line 37
37:     def call(env)
38:       path = env["PATH_INFO"]
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            || (host.nil? && (hHost == sName || hHost == sName+':'+sPort)))
43:         next unless path.to_s =~ match && rest = $1
44:         next unless rest.empty? || rest[0] == //
45:         env.merge!('SCRIPT_NAME' => (script_name + location), 'PATH_INFO' => rest)
46:         return app.call(env)
47:       }
48:       [404, {"Content-Type" => "text/plain", "X-Cascade" => "pass"}, ["Not Found: #{path}"]]
49:     ensure
50:       env.merge! 'PATH_INFO' => path, 'SCRIPT_NAME' => script_name
51:     end
remap(map) click to toggle source
    # File lib/rack/urlmap.rb, line 19
19:     def remap(map)
20:       @mapping = map.map { |location, app|
21:         if location =~ %{\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.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.