mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #20017 from eliotsykes/configurable-static-index-filename
config.static_index configures directory Index "index.html" filename
This commit is contained in:
commit
73aab036ee
12 changed files with 65 additions and 8 deletions
|
@ -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*
|
||||||
|
|
||||||
* Deprecate `:nothing` option for `render` method.
|
* Deprecate `:nothing` option for `render` method.
|
||||||
|
|
||||||
*Mehmet Emin İNAÇ*
|
*Mehmet Emin İNAÇ*
|
||||||
|
|
|
@ -13,11 +13,12 @@ module ActionDispatch
|
||||||
# located at `public/assets/application.js` if the file exists. If the file
|
# 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.
|
# does not exist, a 404 "File not Found" response will be returned.
|
||||||
class FileHandler
|
class FileHandler
|
||||||
def initialize(root, cache_control)
|
def initialize(root, cache_control, index)
|
||||||
@root = root.chomp('/')
|
@root = root.chomp('/')
|
||||||
@compiled_root = /^#{Regexp.escape(root)}/
|
@compiled_root = /^#{Regexp.escape(root)}/
|
||||||
headers = cache_control && { 'Cache-Control' => cache_control }
|
headers = cache_control && { 'Cache-Control' => cache_control }
|
||||||
@file_server = ::Rack::File.new(@root, headers)
|
@file_server = ::Rack::File.new(@root, headers)
|
||||||
|
@index = index
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -32,7 +33,7 @@ module ActionDispatch
|
||||||
return false unless path.valid_encoding?
|
return false unless path.valid_encoding?
|
||||||
path = Rack::Utils.clean_path_info path
|
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|
|
if match = paths.detect { |p|
|
||||||
path = File.join(@root, p.force_encoding('UTF-8'))
|
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'
|
# produce a directory traversal using this middleware. Only 'GET' and 'HEAD'
|
||||||
# requests will result in a file being returned.
|
# requests will result in a file being returned.
|
||||||
class Static
|
class Static
|
||||||
def initialize(app, path, cache_control=nil)
|
def initialize(app, path, cache_control=nil, index="index")
|
||||||
@app = app
|
@app = app
|
||||||
@file_handler = FileHandler.new(path, cache_control)
|
@file_handler = FileHandler.new(path, cache_control, index)
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(env)
|
def call(env)
|
||||||
|
|
|
@ -57,6 +57,7 @@ module StaticTests
|
||||||
|
|
||||||
def test_serves_static_index_file_in_directory
|
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.html")
|
||||||
|
assert_html "/foo/index.html", get("/foo/index")
|
||||||
assert_html "/foo/index.html", get("/foo/")
|
assert_html "/foo/index.html", get("/foo/")
|
||||||
assert_html "/foo/index.html", get("/foo")
|
assert_html "/foo/index.html", get("/foo")
|
||||||
end
|
end
|
||||||
|
@ -260,6 +261,19 @@ class StaticTest < ActiveSupport::TestCase
|
||||||
}
|
}
|
||||||
assert_equal(DummyApp.call(nil), @app.call(env))
|
assert_equal(DummyApp.call(nil), @app.call(env))
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
class StaticEncodingTest < StaticTest
|
class StaticEncodingTest < StaticTest
|
||||||
|
|
1
actionpack/test/fixtures/public/foo/other-index.html
vendored
Normal file
1
actionpack/test/fixtures/public/foo/other-index.html
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
/foo/other-index.html
|
1
actionpack/test/fixtures/public/other-index.html
vendored
Normal file
1
actionpack/test/fixtures/public/other-index.html
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
/other-index.html
|
1
actionpack/test/fixtures/公共/foo/other-index.html
vendored
Normal file
1
actionpack/test/fixtures/公共/foo/other-index.html
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
/foo/other-index.html
|
1
actionpack/test/fixtures/公共/other-index.html
vendored
Normal file
1
actionpack/test/fixtures/公共/other-index.html
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
/other-index.html
|
|
@ -202,7 +202,7 @@ The full set of methods that can be used in this block are as follows:
|
||||||
Every Rails application comes with a standard set of middleware which it uses in this order in the development environment:
|
Every Rails application comes with a standard set of middleware which it uses in this order in the development environment:
|
||||||
|
|
||||||
* `ActionDispatch::SSL` forces every request to be under HTTPS protocol. Will be available if `config.force_ssl` is set to `true`. Options passed to this can be configured by using `config.ssl_options`.
|
* `ActionDispatch::SSL` forces every request to be under HTTPS protocol. Will be available if `config.force_ssl` is set to `true`. Options passed to this can be configured by using `config.ssl_options`.
|
||||||
* `ActionDispatch::Static` is used to serve static assets. Disabled if `config.serve_static_files` is `false`.
|
* `ActionDispatch::Static` is used to serve static assets. Disabled if `config.serve_static_files` is `false`. Set `config.static_index` if you need to serve a static directory index file that is not named `index`. For example, to serve `main.html` instead of `index.html` for directory requests, set `config.static_index` to `"main"`.
|
||||||
* `Rack::Lock` wraps the app in mutex so it can only be called by a single thread at a time. Only enabled when `config.cache_classes` is `false`.
|
* `Rack::Lock` wraps the app in mutex so it can only be called by a single thread at a time. Only enabled when `config.cache_classes` is `false`.
|
||||||
* `ActiveSupport::Cache::Strategy::LocalCache` serves as a basic memory backed cache. This cache is not thread safe and is intended only for serving as a temporary memory cache for a single thread.
|
* `ActiveSupport::Cache::Strategy::LocalCache` serves as a basic memory backed cache. This cache is not thread safe and is intended only for serving as a temporary memory cache for a single thread.
|
||||||
* `Rack::Runtime` sets an `X-Runtime` header, containing the time (in seconds) taken to execute the request.
|
* `Rack::Runtime` sets an `X-Runtime` header, containing the time (in seconds) taken to execute the request.
|
||||||
|
|
|
@ -1,3 +1,11 @@
|
||||||
|
* `config.static_index` configures directory `index.html` filename
|
||||||
|
|
||||||
|
Set `config.static_index` to serve a static directory index file not named
|
||||||
|
`index`. E.g. to serve `main.html` instead of `index.html` for directory
|
||||||
|
requests, set `config.static_index` to `"main"`.
|
||||||
|
|
||||||
|
*Eliot Sykes*
|
||||||
|
|
||||||
* `bin/setup` uses built-in rake tasks (`log:clear`, `tmp:clear`).
|
* `bin/setup` uses built-in rake tasks (`log:clear`, `tmp:clear`).
|
||||||
|
|
||||||
*Mohnish Thallavajhula*
|
*Mohnish Thallavajhula*
|
||||||
|
|
|
@ -11,8 +11,8 @@ module Rails
|
||||||
:eager_load, :exceptions_app, :file_watcher, :filter_parameters,
|
:eager_load, :exceptions_app, :file_watcher, :filter_parameters,
|
||||||
:force_ssl, :helpers_paths, :logger, :log_formatter, :log_tags,
|
:force_ssl, :helpers_paths, :logger, :log_formatter, :log_tags,
|
||||||
:railties_order, :relative_url_root, :secret_key_base, :secret_token,
|
:railties_order, :relative_url_root, :secret_key_base, :secret_token,
|
||||||
:serve_static_files, :ssl_options, :static_cache_control, :session_options,
|
:serve_static_files, :ssl_options, :static_cache_control, :static_index,
|
||||||
:time_zone, :reload_classes_only_on_change,
|
:session_options, :time_zone, :reload_classes_only_on_change,
|
||||||
:beginning_of_week, :filter_redirect, :x
|
:beginning_of_week, :filter_redirect, :x
|
||||||
|
|
||||||
attr_writer :log_level
|
attr_writer :log_level
|
||||||
|
@ -28,6 +28,7 @@ module Rails
|
||||||
@helpers_paths = []
|
@helpers_paths = []
|
||||||
@serve_static_files = true
|
@serve_static_files = true
|
||||||
@static_cache_control = nil
|
@static_cache_control = nil
|
||||||
|
@static_index = "index"
|
||||||
@force_ssl = false
|
@force_ssl = false
|
||||||
@ssl_options = {}
|
@ssl_options = {}
|
||||||
@session_store = :cookie_store
|
@session_store = :cookie_store
|
||||||
|
|
|
@ -18,7 +18,7 @@ module Rails
|
||||||
middleware.use ::Rack::Sendfile, config.action_dispatch.x_sendfile_header
|
middleware.use ::Rack::Sendfile, config.action_dispatch.x_sendfile_header
|
||||||
|
|
||||||
if config.serve_static_files
|
if config.serve_static_files
|
||||||
middleware.use ::ActionDispatch::Static, paths["public"].first, config.static_cache_control
|
middleware.use ::ActionDispatch::Static, paths["public"].first, config.static_cache_control, config.static_index
|
||||||
end
|
end
|
||||||
|
|
||||||
if rack_cache = load_rack_cache
|
if rack_cache = load_rack_cache
|
||||||
|
|
|
@ -26,5 +26,26 @@ module ApplicationTests
|
||||||
|
|
||||||
assert_not last_response.headers.has_key?('Cache-Control'), "Cache-Control should not be set"
|
assert_not last_response.headers.has_key?('Cache-Control'), "Cache-Control should not be set"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "static_index defaults to 'index'" do
|
||||||
|
app_file "public/index.html", "/index.html"
|
||||||
|
|
||||||
|
require "#{app_path}/config/environment"
|
||||||
|
|
||||||
|
get '/'
|
||||||
|
|
||||||
|
assert_equal "/index.html\n", last_response.body
|
||||||
|
end
|
||||||
|
|
||||||
|
test "static_index configurable" do
|
||||||
|
app_file "public/other-index.html", "/other-index.html"
|
||||||
|
add_to_config "config.static_index = 'other-index'"
|
||||||
|
|
||||||
|
require "#{app_path}/config/environment"
|
||||||
|
|
||||||
|
get '/'
|
||||||
|
|
||||||
|
assert_equal "/other-index.html\n", last_response.body
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue