diff options
author | Jamis Buck <jamis@37signals.com> | 2006-06-07 16:16:37 +0000 |
---|---|---|
committer | Jamis Buck <jamis@37signals.com> | 2006-06-07 16:16:37 +0000 |
commit | ae7029ddd1fe11890479df119d0f5996c40c80de (patch) | |
tree | 4ca4b96ee306b8c116edff2472938904c1845baf /actionpack/lib/action_controller | |
parent | 4e3543b46749a83e4ccf1e9346b4dfe6d1a03233 (diff) | |
download | rails-ae7029ddd1fe11890479df119d0f5996c40c80de.tar.gz rails-ae7029ddd1fe11890479df119d0f5996c40c80de.tar.bz2 rails-ae7029ddd1fe11890479df119d0f5996c40c80de.zip |
Do some path normalization to prevent the possible_controllers list from containing invalid entries
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4443 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib/action_controller')
-rw-r--r-- | actionpack/lib/action_controller/routing.rb | 36 |
1 files changed, 31 insertions, 5 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 |