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.

Constants

NEGATIVE_INFINITY

Public Class Methods

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

Public Instance Methods

call(env) click to toggle source
    # File lib/rack/urlmap.rb, line 42
42:     def call(env)
43:       path = env["PATH_INFO"]
44:       script_name = env['SCRIPT_NAME']
45:       hHost = env['HTTP_HOST']
46:       sName = env['SERVER_NAME']
47:       sPort = env['SERVER_PORT']
48: 
49:       @mapping.each do |host, location, match, app|
50:         unless hHost == host              || sName == host              || (!host && (hHost == sName || hHost == sName+':'+sPort))
51:           next
52:         end
53: 
54:         next unless m = match.match(path.to_s)
55: 
56:         rest = m[1]
57:         next unless !rest || rest.empty? || rest[0] == //
58: 
59:         env['SCRIPT_NAME'] = (script_name + location)
60:         env['PATH_INFO'] = rest
61: 
62:         return app.call(env)
63:       end
64: 
65:       [404, {"Content-Type" => "text/plain", "X-Cascade" => "pass"}, ["Not Found: #{path}"]]
66: 
67:     ensure
68:       env['PATH_INFO'] = path
69:       env['SCRIPT_NAME'] = script_name
70:     end
remap(map) click to toggle source
    # File lib/rack/urlmap.rb, line 21
21:     def remap(map)
22:       @mapping = map.map { |location, app|
23:         if location =~ %{\Ahttps?://(.*?)(/.*)}
24:           host, location = $1, $2
25:         else
26:           host = nil
27:         end
28: 
29:         unless location[0] == //
30:           raise ArgumentError, "paths need to start with /"
31:         end
32: 
33:         location = location.chomp('/')
34:         match = Regexp.new("^#{Regexp.quote(location).gsub('/', '/+')}(.*)", nil, 'n')
35: 
36:         [host, location, match, app]
37:       }.sort_by do |(host, location, _, _)|
38:         [host ? -host.size : NEGATIVE_INFINITY, -location.size]
39:       end
40:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.