mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fix serving index files with rack static file server [#2180 state:resolved]
This commit is contained in:
parent
224a534400
commit
0c9bbf8c9d
5 changed files with 54 additions and 4 deletions
|
@ -13,16 +13,20 @@ module Rails
|
||||||
def call(env)
|
def call(env)
|
||||||
path = env['PATH_INFO'].chomp('/')
|
path = env['PATH_INFO'].chomp('/')
|
||||||
method = env['REQUEST_METHOD']
|
method = env['REQUEST_METHOD']
|
||||||
cached_path = (path.empty? ? 'index' : path) + ::ActionController::Base.page_cache_extension
|
|
||||||
|
|
||||||
if FILE_METHODS.include?(method)
|
if FILE_METHODS.include?(method)
|
||||||
if file_exist?(path)
|
if file_exist?(path)
|
||||||
return @file_server.call(env)
|
return @file_server.call(env)
|
||||||
elsif file_exist?(cached_path)
|
else
|
||||||
|
cached_path = directory_exist?(path) ? "#{path}/index" : path
|
||||||
|
cached_path += ::ActionController::Base.page_cache_extension
|
||||||
|
|
||||||
|
if file_exist?(cached_path)
|
||||||
env['PATH_INFO'] = cached_path
|
env['PATH_INFO'] = cached_path
|
||||||
return @file_server.call(env)
|
return @file_server.call(env)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
@app.call(env)
|
@app.call(env)
|
||||||
end
|
end
|
||||||
|
@ -32,6 +36,11 @@ module Rails
|
||||||
full_path = File.join(@file_server.root, ::Rack::Utils.unescape(path))
|
full_path = File.join(@file_server.root, ::Rack::Utils.unescape(path))
|
||||||
File.file?(full_path) && File.readable?(full_path)
|
File.file?(full_path) && File.readable?(full_path)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def directory_exist?(path)
|
||||||
|
full_path = File.join(@file_server.root, ::Rack::Utils.unescape(path))
|
||||||
|
File.directory?(full_path) && File.readable?(full_path)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
1
railties/test/public/foo/bar.html
Normal file
1
railties/test/public/foo/bar.html
Normal file
|
@ -0,0 +1 @@
|
||||||
|
/foo/bar.html
|
1
railties/test/public/foo/index.html
Normal file
1
railties/test/public/foo/index.html
Normal file
|
@ -0,0 +1 @@
|
||||||
|
/foo/index.html
|
1
railties/test/public/index.html
Normal file
1
railties/test/public/index.html
Normal file
|
@ -0,0 +1 @@
|
||||||
|
/index.html
|
38
railties/test/rack_static_test.rb
Normal file
38
railties/test/rack_static_test.rb
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
require 'abstract_unit'
|
||||||
|
|
||||||
|
require 'action_controller'
|
||||||
|
require 'rails/rack'
|
||||||
|
|
||||||
|
class RackStaticTest < ActiveSupport::TestCase
|
||||||
|
DummyApp = lambda { |env|
|
||||||
|
[200, {"Content-Type" => "text/plain"}, ["Hello, World!"]]
|
||||||
|
}
|
||||||
|
App = Rails::Rack::Static.new(DummyApp)
|
||||||
|
|
||||||
|
test "serves dynamic content" do
|
||||||
|
assert_equal "Hello, World!", get("/nofile")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "serves static index at root" do
|
||||||
|
assert_equal "/index.html", get("/index.html")
|
||||||
|
assert_equal "/index.html", get("/index")
|
||||||
|
assert_equal "/index.html", get("/")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "serves static file in directory" do
|
||||||
|
assert_equal "/foo/bar.html", get("/foo/bar.html")
|
||||||
|
assert_equal "/foo/bar.html", get("/foo/bar/")
|
||||||
|
assert_equal "/foo/bar.html", get("/foo/bar")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "serves static index file in directory" do
|
||||||
|
assert_equal "/foo/index.html", get("/foo/index.html")
|
||||||
|
assert_equal "/foo/index.html", get("/foo/")
|
||||||
|
assert_equal "/foo/index.html", get("/foo")
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def get(path)
|
||||||
|
Rack::MockRequest.new(App).request("GET", path).body
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue