Skip to content

Commit

Permalink
Merge pull request #16544 from schneems/schneems/death-to-dir-glob
Browse files Browse the repository at this point in the history
Refactor out Dir.glob from ActionDispatch::Static
  • Loading branch information
guilleiguaran committed Aug 27, 2014
2 parents 5bcd5a3 + 0b1a87f commit 728f2eb
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 20 deletions.
25 changes: 5 additions & 20 deletions actionpack/lib/action_dispatch/middleware/static.rb
Expand Up @@ -21,17 +21,13 @@ def initialize(root, cache_control)
end

def match?(path)
path = unescape_path(path)
path = URI.parser.unescape(path)
return false unless path.valid_encoding?

full_path = path.empty? ? @root : File.join(@root, escape_glob_chars(path))
paths = "#{full_path}#{ext}"
paths = [path, "#{path}#{ext}", "#{path}/index#{ext}"]

matches = Dir[paths]
match = matches.detect { |m| File.file?(m) }
if match
match.sub!(@compiled_root, '')
::Rack::Utils.escape(match)
if match = paths.detect {|p| File.file?(File.join(@root, p)) }
return ::Rack::Utils.escape(match)
end
end

Expand All @@ -57,18 +53,7 @@ def call(env)

private
def ext
@ext ||= begin
ext = ::ActionController::Base.default_static_extension
"{,#{ext},/index#{ext}}"
end
end

def unescape_path(path)
URI.parser.unescape(path)
end

def escape_glob_chars(path)
path.gsub(/[*?{}\[\]]/, "\\\\\\&")
::ActionController::Base.default_static_extension
end

def content_type(path)
Expand Down
4 changes: 4 additions & 0 deletions actionpack/test/dispatch/static_test.rb
Expand Up @@ -37,6 +37,10 @@ def test_serves_static_index_file_in_directory
assert_html "/foo/index.html", get("/foo")
end

def test_serves_file_with_same_name_before_index_in_directory
assert_html "/bar.html", get("/bar")
end

def test_served_static_file_with_non_english_filename
jruby_skip "Stop skipping if following bug gets fixed: " \
"http://jira.codehaus.org/browse/JRUBY-7192"
Expand Down
1 change: 1 addition & 0 deletions actionpack/test/fixtures/public/bar.html
@@ -0,0 +1 @@
/bar.html
1 change: 1 addition & 0 deletions actionpack/test/fixtures/public/bar/index.html
@@ -0,0 +1 @@
/bar/index.html
1 change: 1 addition & 0 deletions actionpack/test/fixtures/公共/bar.html
@@ -0,0 +1 @@
/bar.html
1 change: 1 addition & 0 deletions actionpack/test/fixtures/公共/bar/index.html
@@ -0,0 +1 @@
/bar/index.html

0 comments on commit 728f2eb

Please sign in to comment.