lib/rack/urlmap.rb in rack-2.0.9.4 vs lib/rack/urlmap.rb in rack-2.1.0

- old
+ new

@@ -1,5 +1,9 @@ +# frozen_string_literal: true + +require 'set' + 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>. # @@ -18,13 +22,15 @@ def initialize(map = {}) remap(map) end def remap(map) + @known_hosts = Set[] @mapping = map.map { |location, app| if location =~ %r{\Ahttps?://(.*?)(/.*)} host, location = $1, $2 + @known_hosts << host else host = nil end unless location[0] == ?/ @@ -48,14 +54,17 @@ server_port = env[SERVER_PORT] is_same_server = casecmp?(http_host, server_name) || casecmp?(http_host, "#{server_name}:#{server_port}") + is_host_known = @known_hosts.include? http_host + @mapping.each do |host, location, match, app| unless casecmp?(http_host, host) \ || casecmp?(server_name, host) \ - || (!host && is_same_server) + || (!host && is_same_server) \ + || (!host && !is_host_known) # If we don't have a matching host, default to the first without a specified host next end next unless m = match.match(path.to_s) @@ -66,10 +75,10 @@ env[PATH_INFO] = rest return app.call(env) end - [404, {CONTENT_TYPE => "text/plain", "X-Cascade" => "pass"}, ["Not Found: #{path}"]] + [404, { CONTENT_TYPE => "text/plain", "X-Cascade" => "pass" }, ["Not Found: #{path}"]] ensure env[PATH_INFO] = path env[SCRIPT_NAME] = script_name end