diff options
author | Eliot Sykes <eliotsykes@gmail.com> | 2015-05-04 20:55:23 +0100 |
---|---|---|
committer | Eliot Sykes <eliotsykes@gmail.com> | 2015-05-28 09:41:00 +0100 |
commit | 3ff39494cdea67502dbd6465358eca3e14a84d6b (patch) | |
tree | 89e83c974e2e515de9aa12fcc09176255fa905ee /actionpack | |
parent | 7cc9754209c0ae00d70bdd629fa4a81a1c74cc6f (diff) | |
download | rails-3ff39494cdea67502dbd6465358eca3e14a84d6b.tar.gz rails-3ff39494cdea67502dbd6465358eca3e14a84d6b.tar.bz2 rails-3ff39494cdea67502dbd6465358eca3e14a84d6b.zip |
config.static_index configures directory index "index.html" filename
Set `config.static_index` to serve a static directory index file not
named `index`. For example, to serve `main.html` instead of `index.html`
for directory requests, set `config.static_index` to `"main"`.
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/CHANGELOG.md | 8 | ||||
-rw-r--r-- | actionpack/lib/action_dispatch/middleware/static.rb | 9 | ||||
-rw-r--r-- | actionpack/test/dispatch/static_test.rb | 14 | ||||
-rw-r--r-- | actionpack/test/fixtures/public/foo/other-index.html | 1 | ||||
-rw-r--r-- | actionpack/test/fixtures/public/other-index.html | 1 | ||||
-rw-r--r-- | actionpack/test/fixtures/公共/foo/other-index.html | 1 | ||||
-rw-r--r-- | actionpack/test/fixtures/公共/other-index.html | 1 |
7 files changed, 31 insertions, 4 deletions
diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index a9233014e4..4117bb0f8e 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,3 +1,11 @@ +* `FileHandler` and `Static` middleware initializers accept `index` argument + to configure the directory index file name. Defaults to `index` (as in + `index.html`). + + See #20017. + + *Eliot Sykes* + * Fix `rake routes` not showing the right format when nesting multiple routes. diff --git a/actionpack/lib/action_dispatch/middleware/static.rb b/actionpack/lib/action_dispatch/middleware/static.rb index bc5ef1abc9..f20f6ca865 100644 --- a/actionpack/lib/action_dispatch/middleware/static.rb +++ b/actionpack/lib/action_dispatch/middleware/static.rb @@ -13,11 +13,12 @@ module ActionDispatch # located at `public/assets/application.js` if the file exists. If the file # does not exist, a 404 "File not Found" response will be returned. class FileHandler - def initialize(root, cache_control) + def initialize(root, cache_control, index) @root = root.chomp('/') @compiled_root = /^#{Regexp.escape(root)}/ headers = cache_control && { 'Cache-Control' => cache_control } @file_server = ::Rack::File.new(@root, headers) + @index = index end @@ -32,7 +33,7 @@ module ActionDispatch return false unless path.valid_encoding? path = Rack::Utils.clean_path_info path - paths = [path, "#{path}#{ext}", "#{path}/index#{ext}"] + paths = [path, "#{path}#{ext}", "#{path}/#{@index}#{ext}"] if match = paths.detect { |p| path = File.join(@root, p.force_encoding('UTF-8')) @@ -104,9 +105,9 @@ module ActionDispatch # produce a directory traversal using this middleware. Only 'GET' and 'HEAD' # requests will result in a file being returned. class Static - def initialize(app, path, cache_control=nil) + def initialize(app, path, cache_control=nil, index="index") @app = app - @file_handler = FileHandler.new(path, cache_control) + @file_handler = FileHandler.new(path, cache_control, index) end def call(env) diff --git a/actionpack/test/dispatch/static_test.rb b/actionpack/test/dispatch/static_test.rb index 93e5c85a97..e729cc44f9 100644 --- a/actionpack/test/dispatch/static_test.rb +++ b/actionpack/test/dispatch/static_test.rb @@ -57,6 +57,7 @@ module StaticTests def test_serves_static_index_file_in_directory assert_html "/foo/index.html", get("/foo/index.html") + assert_html "/foo/index.html", get("/foo/index") assert_html "/foo/index.html", get("/foo/") assert_html "/foo/index.html", get("/foo") end @@ -260,6 +261,19 @@ class StaticTest < ActiveSupport::TestCase } assert_equal(DummyApp.call(nil), @app.call(env)) end + + def test_non_default_static_index + @app = ActionDispatch::Static.new(DummyApp, @root, "public, max-age=60", "other-index") + assert_html "/other-index.html", get("/other-index.html") + assert_html "/other-index.html", get("/other-index") + assert_html "/other-index.html", get("/") + assert_html "/other-index.html", get("") + assert_html "/foo/other-index.html", get("/foo/other-index.html") + assert_html "/foo/other-index.html", get("/foo/other-index") + assert_html "/foo/other-index.html", get("/foo/") + assert_html "/foo/other-index.html", get("/foo") + end + end class StaticEncodingTest < StaticTest diff --git a/actionpack/test/fixtures/public/foo/other-index.html b/actionpack/test/fixtures/public/foo/other-index.html new file mode 100644 index 0000000000..51c90c26ea --- /dev/null +++ b/actionpack/test/fixtures/public/foo/other-index.html @@ -0,0 +1 @@ +/foo/other-index.html
\ No newline at end of file diff --git a/actionpack/test/fixtures/public/other-index.html b/actionpack/test/fixtures/public/other-index.html new file mode 100644 index 0000000000..0820dfcb6e --- /dev/null +++ b/actionpack/test/fixtures/public/other-index.html @@ -0,0 +1 @@ +/other-index.html
\ No newline at end of file diff --git a/actionpack/test/fixtures/公共/foo/other-index.html b/actionpack/test/fixtures/公共/foo/other-index.html new file mode 100644 index 0000000000..51c90c26ea --- /dev/null +++ b/actionpack/test/fixtures/公共/foo/other-index.html @@ -0,0 +1 @@ +/foo/other-index.html
\ No newline at end of file diff --git a/actionpack/test/fixtures/公共/other-index.html b/actionpack/test/fixtures/公共/other-index.html new file mode 100644 index 0000000000..0820dfcb6e --- /dev/null +++ b/actionpack/test/fixtures/公共/other-index.html @@ -0,0 +1 @@ +/other-index.html
\ No newline at end of file |