Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
9d05430c
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
9d05430c
编写于
10月 13, 2015
作者:
J
Jeremy Daer
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #19135 from yuki24/access-control-support
Add basic support for access control headers to ActionDispatch::Static
上级
8e7a3b05
52260581
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
106 addition
and
22 deletion
+106
-22
actionpack/CHANGELOG.md
actionpack/CHANGELOG.md
+19
-0
actionpack/lib/action_dispatch/middleware/static.rb
actionpack/lib/action_dispatch/middleware/static.rb
+14
-8
actionpack/test/dispatch/static_test.rb
actionpack/test/dispatch/static_test.rb
+27
-8
railties/lib/rails/application/configuration.rb
railties/lib/rails/application/configuration.rb
+11
-3
railties/lib/rails/application/default_middleware_stack.rb
railties/lib/rails/application/default_middleware_stack.rb
+4
-1
railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt
...rators/rails/app/templates/config/environments/test.rb.tt
+4
-2
railties/test/application/configuration_test.rb
railties/test/application/configuration_test.rb
+10
-0
railties/test/application/middleware/static_test.rb
railties/test/application/middleware/static_test.rb
+17
-0
未找到文件。
actionpack/CHANGELOG.md
浏览文件 @
9d05430c
*
Deprecate
`config.static_cache_control`
in favor of
`config.public_file_server.headers`
*Yuki Nishijima*
*
Add the ability of returning arbitrary headers to ActionDispatch::Static
Now ActionDispatch::Static can accept HTTP headers so that developers
will have control of returning arbitrary headers like
'Access-Control-Allow-Origin' when a response is delivered. They can be
configured with `#config`:
config.public_file_server.headers = {
"Cache-Control" => "public, max-age=60",
"Access-Control-Allow-Origin" => "http://rubyonrails.org"
}
*Yuki Nishijima*
*
Allow multiple
`root`
routes in same scope level. Example:
```ruby
...
...
actionpack/lib/action_dispatch/middleware/static.rb
浏览文件 @
9d05430c
...
...
@@ -3,8 +3,8 @@
module
ActionDispatch
# This middleware returns a file's contents from disk in the body response.
# When initialized, it can accept
an optional 'Cache-Control' header, which
# w
ill be set w
hen a response containing a file's contents is delivered.
# When initialized, it can accept
optional HTTP headers, which will be set
# when a response containing a file's contents is delivered.
#
# This middleware will render the file specified in `env["PATH_INFO"]`
# where the base path is in the +root+ directory. For example, if the +root+
...
...
@@ -13,12 +13,11 @@ 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
,
index:
'index'
)
def
initialize
(
root
,
index:
'index'
,
headers:
{}
)
@root
=
root
.
chomp
(
'/'
)
@compiled_root
=
/^
#{
Regexp
.
escape
(
root
)
}
/
headers
=
cache_control
&&
{
'Cache-Control'
=>
cache_control
}
@file_server
=
::
Rack
::
File
.
new
(
@root
,
headers
)
@index
=
index
@file_server
=
::
Rack
::
File
.
new
(
@root
,
headers
)
@index
=
index
end
# Takes a path to a file. If the file is found, has valid encoding, and has
...
...
@@ -108,9 +107,16 @@ def gzip_file_path(path)
# 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
,
index:
'index'
)
def
initialize
(
app
,
path
,
deprecated_cache_control
=
:not_set
,
index:
'index'
,
headers:
{})
if
deprecated_cache_control
!=
:not_set
ActiveSupport
::
Deprecation
.
warn
(
"The `cache_control` argument is deprecated,"
\
"replaced by `headers: { 'Cache-Control' =>
#{
deprecated_cache_control
}
}`, "
\
" and will be removed in Rails 5.1."
)
headers
[
'Cache-Control'
.
freeze
]
=
deprecated_cache_control
end
@app
=
app
@file_handler
=
FileHandler
.
new
(
path
,
cache_control
,
index:
index
)
@file_handler
=
FileHandler
.
new
(
path
,
index:
index
,
headers:
headers
)
end
def
call
(
env
)
...
...
actionpack/test/dispatch/static_test.rb
浏览文件 @
9d05430c
...
...
@@ -2,6 +2,10 @@
require
'zlib'
module
StaticTests
DummyApp
=
lambda
{
|
env
|
[
200
,
{
"Content-Type"
=>
"text/plain"
},
[
"Hello, World!"
]]
}
def
setup
silence_warnings
do
@default_internal_encoding
=
Encoding
.
default_internal
...
...
@@ -37,7 +41,11 @@ def test_handles_urls_with_ascii_8bit_on_win_31j
end
def
test_sets_cache_control
response
=
get
(
"/index.html"
)
app
=
assert_deprecated
do
ActionDispatch
::
Static
.
new
(
DummyApp
,
@root
,
"public, max-age=60"
)
end
response
=
Rack
::
MockRequest
.
new
(
app
).
request
(
"GET"
,
"/index.html"
)
assert_html
"/index.html"
,
response
assert_equal
"public, max-age=60"
,
response
.
headers
[
"Cache-Control"
]
end
...
...
@@ -180,6 +188,21 @@ def test_serves_gzip_files_with_not_modified
assert_equal
nil
,
response
.
headers
[
'Vary'
]
end
def
test_serves_files_with_headers
headers
=
{
"Access-Control-Allow-Origin"
=>
'http://rubyonrails.org'
,
"Cache-Control"
=>
'public, max-age=60'
,
"X-Custom-Header"
=>
"I'm a teapot"
}
app
=
ActionDispatch
::
Static
.
new
(
DummyApp
,
@root
,
headers:
headers
)
response
=
Rack
::
MockRequest
.
new
(
app
).
request
(
"GET"
,
"/foo/bar.html"
)
assert_equal
'http://rubyonrails.org'
,
response
.
headers
[
"Access-Control-Allow-Origin"
]
assert_equal
'public, max-age=60'
,
response
.
headers
[
"Cache-Control"
]
assert_equal
"I'm a teapot"
,
response
.
headers
[
"X-Custom-Header"
]
end
# Windows doesn't allow \ / : * ? " < > | in filenames
unless
RbConfig
::
CONFIG
[
'host_os'
]
=~
/mswin|mingw/
def
test_serves_static_file_with_colon
...
...
@@ -230,14 +253,10 @@ def with_static_file(file)
end
class
StaticTest
<
ActiveSupport
::
TestCase
DummyApp
=
lambda
{
|
env
|
[
200
,
{
"Content-Type"
=>
"text/plain"
},
[
"Hello, World!"
]]
}
def
setup
super
@root
=
"
#{
FIXTURE_LOAD_PATH
}
/public"
@app
=
ActionDispatch
::
Static
.
new
(
DummyApp
,
@root
,
"public, max-age=60"
)
@app
=
ActionDispatch
::
Static
.
new
(
DummyApp
,
@root
,
headers:
{
'Cache-Control'
=>
"public, max-age=60"
}
)
end
def
public_path
...
...
@@ -263,7 +282,7 @@ def test_custom_handler_called_when_file_is_outside_root
end
def
test_non_default_static_index
@app
=
ActionDispatch
::
Static
.
new
(
DummyApp
,
@root
,
"public, max-age=60"
,
index:
"other-index"
)
@app
=
ActionDispatch
::
Static
.
new
(
DummyApp
,
@root
,
index:
"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
(
"/"
)
...
...
@@ -280,7 +299,7 @@ class StaticEncodingTest < StaticTest
def
setup
super
@root
=
"
#{
FIXTURE_LOAD_PATH
}
/公共"
@app
=
ActionDispatch
::
Static
.
new
(
DummyApp
,
@root
,
"public, max-age=60"
)
@app
=
ActionDispatch
::
Static
.
new
(
DummyApp
,
@root
,
headers:
{
'Cache-Control'
=>
"public, max-age=60"
}
)
end
def
public_path
...
...
railties/lib/rails/application/configuration.rb
浏览文件 @
9d05430c
...
...
@@ -11,12 +11,12 @@ class Configuration < ::Rails::Engine::Configuration
:eager_load
,
:exceptions_app
,
:file_watcher
,
:filter_parameters
,
:force_ssl
,
:helpers_paths
,
:logger
,
:log_formatter
,
:log_tags
,
:railties_order
,
:relative_url_root
,
:secret_key_base
,
:secret_token
,
:serve_static_files
,
:ssl_options
,
:static_
cache_control
,
:static_index
,
:serve_static_files
,
:ssl_options
,
:static_
index
,
:public_file_server
,
:session_options
,
:time_zone
,
:reload_classes_only_on_change
,
:beginning_of_week
,
:filter_redirect
,
:x
attr_writer
:log_level
attr_reader
:encoding
,
:api_only
attr_reader
:encoding
,
:api_only
,
:static_cache_control
def
initialize
(
*
)
super
...
...
@@ -27,8 +27,8 @@ def initialize(*)
@filter_redirect
=
[]
@helpers_paths
=
[]
@serve_static_files
=
true
@static_cache_control
=
nil
@static_index
=
"index"
@public_file_server
=
ActiveSupport
::
OrderedOptions
.
new
@force_ssl
=
false
@ssl_options
=
{}
@session_store
=
:cookie_store
...
...
@@ -52,6 +52,14 @@ def initialize(*)
@x
=
Custom
.
new
end
def
static_cache_control
=
(
value
)
ActiveSupport
::
Deprecation
.
warn
(
"static_cache_control is deprecated and will be removed in Rails 5.1. "
\
"Please use `config.public_file_server.headers = {'Cache-Control' =>
#{
value
}
} "
\
"instead."
)
@static_cache_control
=
value
end
def
encoding
=
(
value
)
@encoding
=
value
silence_warnings
do
...
...
railties/lib/rails/application/default_middleware_stack.rb
浏览文件 @
9d05430c
...
...
@@ -18,7 +18,10 @@ def build_stack
middleware
.
use
::
Rack
::
Sendfile
,
config
.
action_dispatch
.
x_sendfile_header
if
config
.
serve_static_files
middleware
.
use
::
ActionDispatch
::
Static
,
paths
[
"public"
].
first
,
config
.
static_cache_control
,
index:
config
.
static_index
headers
=
config
.
public_file_server
.
headers
||
{}
headers
[
'Cache-Control'
.
freeze
]
=
config
.
static_cache_control
if
config
.
static_cache_control
middleware
.
use
::
ActionDispatch
::
Static
,
paths
[
"public"
].
first
,
index:
config
.
static_index
,
headers:
headers
end
if
rack_cache
=
load_rack_cache
...
...
railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt
浏览文件 @
9d05430c
...
...
@@ -13,8 +13,10 @@ Rails.application.configure do
config.eager_load = false
# Configure static file server for tests with Cache-Control for performance.
config.serve_static_files = true
config.static_cache_control = 'public, max-age=3600'
config.serve_static_files = true
config.public_file_server.headers = {
'Cache-Control' => 'public, max-age=3600'
}
# Show full error reports and disable caching.
config.consider_all_requests_local = true
...
...
railties/test/application/configuration_test.rb
浏览文件 @
9d05430c
...
...
@@ -339,6 +339,16 @@ def assert_utf8
end
end
test
"config.static_cache_control is deprecated"
do
make_basic_app
do
|
application
|
assert_deprecated
do
application
.
config
.
static_cache_control
=
"public, max-age=60"
end
assert_equal
application
.
config
.
static_cache_control
,
"public, max-age=60"
end
end
test
"Use key_generator when secret_key_base is set"
do
make_basic_app
do
|
application
|
application
.
secrets
.
secret_key_base
=
'b3c631c314c0bbca50c1b2843150fe33'
...
...
railties/test/application/middleware/static_test.rb
浏览文件 @
9d05430c
...
...
@@ -27,6 +27,23 @@ def teardown
assert_not
last_response
.
headers
.
has_key?
(
'Cache-Control'
),
"Cache-Control should not be set"
end
test
"headers for static files are configurable"
do
app_file
"public/about.html"
,
'static'
add_to_config
<<-
CONFIG
config.public_file_server.headers = {
"Access-Control-Allow-Origin" => "http://rubyonrails.org",
"Cache-Control" => "public, max-age=60"
}
CONFIG
require
"
#{
app_path
}
/config/environment"
get
'/about.html'
assert_equal
'http://rubyonrails.org'
,
last_response
.
headers
[
"Access-Control-Allow-Origin"
]
assert_equal
'public, max-age=60'
,
last_response
.
headers
[
"Cache-Control"
]
end
test
"static_index defaults to 'index'"
do
app_file
"public/index.html"
,
"/index.html"
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录