Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
ae66fda4
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,发现更多精彩内容 >>
提交
ae66fda4
编写于
8月 24, 2014
作者:
J
Jeremy Kemper
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #16616 from schneems/schneems/jeremy-comments
Address comments on Gzip implementation
上级
cb62f922
8e31fa3b
变更
7
展开全部
隐藏空白更改
内联
并排
Showing
7 changed file
with
54 addition
and
12 deletion
+54
-12
actionpack/lib/action_dispatch/middleware/static.rb
actionpack/lib/action_dispatch/middleware/static.rb
+19
-10
actionpack/test/dispatch/static_test.rb
actionpack/test/dispatch/static_test.rb
+24
-2
actionpack/test/fixtures/public/gzip/foo.zoo
actionpack/test/fixtures/public/gzip/foo.zoo
+4
-0
actionpack/test/fixtures/public/gzip/foo.zoo.gz
actionpack/test/fixtures/public/gzip/foo.zoo.gz
+0
-0
actionpack/test/fixtures/公共/gzip/foo.zoo
actionpack/test/fixtures/公共/gzip/foo.zoo
+0
-0
actionpack/test/fixtures/公共/gzip/foo.zoo.gz
actionpack/test/fixtures/公共/gzip/foo.zoo.gz
+0
-0
guides/source/4_2_release_notes.md
guides/source/4_2_release_notes.md
+7
-0
未找到文件。
actionpack/lib/action_dispatch/middleware/static.rb
浏览文件 @
ae66fda4
...
...
@@ -16,8 +16,7 @@ class FileHandler
def
initialize
(
root
,
cache_control
)
@root
=
root
.
chomp
(
'/'
)
@compiled_root
=
/^
#{
Regexp
.
escape
(
root
)
}
/
headers
=
{}
headers
[
'Cache-Control'
]
=
cache_control
if
cache_control
headers
=
cache_control
&&
{
'Cache-Control'
=>
cache_control
}
@file_server
=
::
Rack
::
File
.
new
(
@root
,
headers
)
end
...
...
@@ -37,10 +36,11 @@ def match?(path)
end
def
call
(
env
)
path
=
env
[
'PATH_INFO'
]
gzip_file_exists
=
gzip_file_exists?
(
path
)
if
gzip_file_exists
&&
gzip_encoding_accepted?
(
env
)
env
[
'PATH_INFO'
]
=
"
#{
path
}
.gz"
path
=
env
[
'PATH_INFO'
]
gzip_path
=
gzip_file_path
(
path
)
if
gzip_path
&&
gzip_encoding_accepted?
(
env
)
env
[
'PATH_INFO'
]
=
gzip_path
status
,
headers
,
body
=
@file_server
.
call
(
env
)
headers
[
'Content-Encoding'
]
=
'gzip'
headers
[
'Content-Type'
]
=
content_type
(
path
)
...
...
@@ -48,8 +48,11 @@ def call(env)
status
,
headers
,
body
=
@file_server
.
call
(
env
)
end
headers
[
'Vary'
]
=
'Accept-Encoding'
if
gzip_file_exists
headers
[
'Vary'
]
=
'Accept-Encoding'
if
gzip_path
return
[
status
,
headers
,
body
]
ensure
env
[
'PATH_INFO'
]
=
path
end
private
...
...
@@ -73,11 +76,17 @@ def content_type(path)
end
def
gzip_encoding_accepted?
(
env
)
env
[
'HTTP_ACCEPT_ENCODING'
]
=~
/\bgzip\b/
env
[
'HTTP_ACCEPT_ENCODING'
]
=~
/\bgzip\b/
i
end
def
gzip_file_exists?
(
path
)
File
.
exist?
(
File
.
join
(
@root
,
"
#{
::
Rack
::
Utils
.
unescape
(
path
)
}
.gz"
))
def
gzip_file_path
(
path
)
can_gzip_mime
=
content_type
(
path
)
=~
/\A(?:text\/|application\/javascript)/
gzip_path
=
"
#{
path
}
.gz"
if
can_gzip_mime
&&
File
.
exist?
(
File
.
join
(
@root
,
::
Rack
::
Utils
.
unescape
(
gzip_path
)))
gzip_path
else
false
end
end
end
...
...
actionpack/test/dispatch/static_test.rb
浏览文件 @
ae66fda4
...
...
@@ -115,8 +115,30 @@ def test_serves_gzip_files_when_header_set
assert_equal
'Accept-Encoding'
,
response
.
headers
[
"Vary"
]
assert_equal
'gzip'
,
response
.
headers
[
"Content-Encoding"
]
response
=
get
(
file_name
,
'HTTP_ACCEPT_ENCODING'
=>
'Gzip'
)
assert_gzip
file_name
,
response
response
=
get
(
file_name
,
'HTTP_ACCEPT_ENCODING'
=>
'GZIP'
)
assert_gzip
file_name
,
response
response
=
get
(
file_name
,
'HTTP_ACCEPT_ENCODING'
=>
''
)
refute_equal
'gzip'
,
response
.
headers
[
"Content-Encoding"
]
assert_not_equal
'gzip'
,
response
.
headers
[
"Content-Encoding"
]
end
def
test_does_not_modify_path_info
file_name
=
"/gzip/application-a71b3024f80aea3181c09774ca17e712.js"
env
=
{
'PATH_INFO'
=>
file_name
,
'HTTP_ACCEPT_ENCODING'
=>
'gzip'
}
@app
.
call
(
env
)
assert_equal
file_name
,
env
[
'PATH_INFO'
]
end
def
test_serves_gzip_with_propper_content_type_fallback
file_name
=
"/gzip/foo.zoo"
response
=
get
(
file_name
,
'HTTP_ACCEPT_ENCODING'
=>
'gzip'
)
assert_gzip
file_name
,
response
default_response
=
get
(
file_name
)
# no gzip
assert_equal
default_response
.
headers
[
'Content-Type'
],
response
.
headers
[
'Content-Type'
]
end
# Windows doesn't allow \ / : * ? " < > | in filenames
...
...
@@ -147,7 +169,7 @@ def assert_gzip(file_name, response)
def
assert_html
(
body
,
response
)
assert_equal
body
,
response
.
body
assert_equal
"text/html"
,
response
.
headers
[
"Content-Type"
]
refute
response
.
headers
.
key?
(
"Vary"
)
assert_nil
response
.
headers
[
"Vary"
]
end
def
get
(
path
,
headers
=
{})
...
...
actionpack/test/fixtures/public/gzip/foo.zoo
0 → 100644
浏览文件 @
ae66fda4
此差异已折叠。
点击以展开。
actionpack/test/fixtures/public/gzip/foo.zoo.gz
0 → 100644
浏览文件 @
ae66fda4
文件已添加
actionpack/test/fixtures/公共/gzip/foo.zoo
0 → 100644
浏览文件 @
ae66fda4
此差异已折叠。
点击以展开。
actionpack/test/fixtures/公共/gzip/foo.zoo.gz
0 → 100644
浏览文件 @
ae66fda4
文件已添加
guides/source/4_2_release_notes.md
浏览文件 @
ae66fda4
...
...
@@ -278,6 +278,13 @@ Please refer to the [Changelog][action-pack] for detailed changes.
*
Added an option to disable logging of CSRF failures.
(
[
Pull Request
](
https://github.com/rails/rails/pull/14280
)
)
*
When the Rails server is set to serve static assets, gzip assets will now be
served if the client supports it and a pre-generated gzip file (.gz) is on disk.
By default the asset pipeline generates
`.gz`
files for all compressible assets.
Serving gzip files minimizes data transfer and speeds up asset requests. Always
[
use a CDN
](
http://guides.rubyonrails.org/asset_pipeline.html#cdns
)
if you are
serving assets from your Rails server in production.
(
[
Pull Request
](
https://github.com/rails/rails/pull/16466
)
)
Action View
-------------
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录