diff options
author | Seb Bacon <seb.bacon@gmail.com> | 2011-03-09 14:58:30 +0000 |
---|---|---|
committer | Seb Bacon <seb.bacon@gmail.com> | 2011-03-09 14:58:30 +0000 |
commit | b4585af18e9c3a033f6cfe27213f0575af795a66 (patch) | |
tree | 996efa1487ac0d8cb7e4f53ee6478ad625b9d27d /vendor/gems/rack-1.1.0/lib/rack/urlmap.rb | |
parent | 224b8a4ba3a24af91068505c7907724448a4096d (diff) | |
parent | 4cc2cf2a6d935adfd263ea4fd7791a6d84f704da (diff) |
merge from master (post-CSRF changes)
Diffstat (limited to 'vendor/gems/rack-1.1.0/lib/rack/urlmap.rb')
-rw-r--r-- | vendor/gems/rack-1.1.0/lib/rack/urlmap.rb | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/vendor/gems/rack-1.1.0/lib/rack/urlmap.rb b/vendor/gems/rack-1.1.0/lib/rack/urlmap.rb new file mode 100644 index 000000000..b699d35b9 --- /dev/null +++ b/vendor/gems/rack-1.1.0/lib/rack/urlmap.rb @@ -0,0 +1,56 @@ +module Rack + # 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 <tt>http://</tt> or <tt>https://</tt>. + # + # 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. + + class URLMap + def initialize(map = {}) + remap(map) + end + + def remap(map) + @mapping = map.map { |location, app| + if location =~ %r{\Ahttps?://(.*?)(/.*)} + host, location = $1, $2 + else + host = nil + end + + unless location[0] == ?/ + raise ArgumentError, "paths need to start with /" + 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 + end + + def call(env) + path = env["PATH_INFO"].to_s + script_name = env['SCRIPT_NAME'] + hHost, sName, sPort = env.values_at('HTTP_HOST','SERVER_NAME','SERVER_PORT') + @mapping.each { |host, location, match, app| + next unless (hHost == host || sName == host \ + || (host.nil? && (hHost == sName || hHost == sName+':'+sPort))) + next unless path =~ match && rest = $1 + next unless rest.empty? || rest[0] == ?/ + + return app.call( + env.merge( + 'SCRIPT_NAME' => (script_name + location), + 'PATH_INFO' => rest)) + } + [404, {"Content-Type" => "text/plain", "X-Cascade" => "pass"}, ["Not Found: #{path}"]] + end + end +end + |