diff options
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_controller/routing.rb | 36 | ||||
-rw-r--r-- | actionpack/test/controller/routing_test.rb | 13 |
2 files changed, 43 insertions, 6 deletions
diff --git a/actionpack/lib/action_controller/routing.rb b/actionpack/lib/action_controller/routing.rb index 70c864646d..14921c3ac0 100644 --- a/actionpack/lib/action_controller/routing.rb +++ b/actionpack/lib/action_controller/routing.rb @@ -57,28 +57,54 @@ module ActionController ensure use_controllers! nil end - + + def normalize_paths(paths=$LOAD_PATH) + # do the hokey-pokey of path normalization... + paths = paths.collect do |path| + path = path. + gsub("//", "/"). # replace double / chars with a single + gsub("\\\\", "\\"). # replace double \ chars with a single + gsub(%r{(.)[\\/]$}, '\1') # drop final / or \ if path ends with it + + # eliminate .. paths where possible + re = %r{\w+[/\\]\.\.[/\\]} + path.gsub!(%r{\w+[/\\]\.\.[/\\]}, "") while path.match(re) + path + end + + # start with longest path, first + paths = paths.uniq.sort_by { |path| - path.length } + end + def possible_controllers unless @possible_controllers @possible_controllers = [] paths = $LOAD_PATH.select { |path| File.directory? path } - paths = paths.sort_by { |path| - path.length } - + seen_paths = Hash.new {|h, k| h[k] = true; false} - paths.each do |load_path| + normalize_paths(paths).each do |load_path| Dir["#{load_path}/**/*_controller.rb"].collect do |path| - next if seen_paths[path] + next if seen_paths[path.gsub(%r{^\.[/\\]}, "")] controller_name = path[(load_path.length + 1)..-1] + next unless path_may_be_controller?(controller_name) + controller_name.gsub!(/_controller\.rb\Z/, '') @possible_controllers << controller_name end end + + # remove duplicates + @possible_controllers.uniq! end @possible_controllers end + def path_may_be_controller?(path) + path !~ /(?:rails\/.*\/(?:examples|test))|(?:actionpack\/lib\/action_controller.rb$)|(?:app\/controllers)/o + end + def use_controllers!(controller_names) @possible_controllers = controller_names end diff --git a/actionpack/test/controller/routing_test.rb b/actionpack/test/controller/routing_test.rb index 970bdc4a1a..44e986e3d5 100644 --- a/actionpack/test/controller/routing_test.rb +++ b/actionpack/test/controller/routing_test.rb @@ -1425,5 +1425,16 @@ class RoutingTest < Test::Unit::TestCase assert_equal c, ActionController::Routing.possible_controllers end end - + + def test_normalize_unix_paths + load_paths = %w(. config/../app/controllers config/../app//helpers script/../config/../vendor/rails/actionpack/lib vendor/rails/railties/builtin/rails_info app/models lib script/../config/../foo/bar/../../app/models) + paths = ActionController::Routing.normalize_paths(load_paths) + assert_equal %w(vendor/rails/railties/builtin/rails_info vendor/rails/actionpack/lib app/controllers app/helpers app/models lib .), paths + end + + def test_normalize_windows_paths + load_paths = %w(. config\\..\\app\\controllers config\\..\\app\\\\helpers script\\..\\config\\..\\vendor\\rails\\actionpack\\lib vendor\\rails\\railties\\builtin\\rails_info app\\models lib script\\..\\config\\..\\foo\\bar\\..\\..\\app\\models) + paths = ActionController::Routing.normalize_paths(load_paths) + assert_equal %w(vendor\\rails\\railties\\builtin\\rails_info vendor\\rails\\actionpack\\lib app\\controllers app\\helpers app\\models lib .), paths + end end |