Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
90006acb
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,发现更多精彩内容 >>
提交
90006acb
编写于
8月 20, 2014
作者:
G
Guillermo Iguaran
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #16466 from schneems/schneems/gzip-action-dispatch
Enable gzip compression by default
上级
2e355fe0
cfaaacd9
变更
7
展开全部
隐藏空白更改
内联
并排
Showing
7 changed file
with
72 addition
and
16 deletion
+72
-16
actionpack/CHANGELOG.md
actionpack/CHANGELOG.md
+6
-0
actionpack/lib/action_dispatch/middleware/static.rb
actionpack/lib/action_dispatch/middleware/static.rb
+40
-14
actionpack/test/dispatch/static_test.rb
actionpack/test/dispatch/static_test.rb
+22
-2
actionpack/test/fixtures/public/gzip/application-a71b3024f80aea3181c09774ca17e712.js
...blic/gzip/application-a71b3024f80aea3181c09774ca17e712.js
+4
-0
actionpack/test/fixtures/public/gzip/application-a71b3024f80aea3181c09774ca17e712.js.gz
...c/gzip/application-a71b3024f80aea3181c09774ca17e712.js.gz
+0
-0
actionpack/test/fixtures/公共/gzip/application-a71b3024f80aea3181c09774ca17e712.js
...s/公共/gzip/application-a71b3024f80aea3181c09774ca17e712.js
+0
-0
actionpack/test/fixtures/公共/gzip/application-a71b3024f80aea3181c09774ca17e712.js.gz
...共/gzip/application-a71b3024f80aea3181c09774ca17e712.js.gz
+0
-0
未找到文件。
actionpack/CHANGELOG.md
浏览文件 @
90006acb
*
Requests that hit
`ActionDispatch::Static`
can now take advantage
of gzipped assets on disk. By default a gzip asset will be served if
the client supports gzip and a compressed file is on disk.
*Richard Schneeman*
*
`ActionController::Parameters`
will stop inheriting from
`Hash`
and
`HashWithIndifferentAccess`
in the next major release. If you use any method
that is not available on
`ActionController::Parameters`
you should consider
...
...
actionpack/lib/action_dispatch/middleware/static.rb
浏览文件 @
90006acb
...
...
@@ -16,8 +16,9 @@ class FileHandler
def
initialize
(
root
,
cache_control
)
@root
=
root
.
chomp
(
'/'
)
@compiled_root
=
/^
#{
Regexp
.
escape
(
root
)
}
/
headers
=
cache_control
&&
{
'Cache-Control'
=>
cache_control
}
@file_server
=
::
Rack
::
File
.
new
(
@root
,
headers
)
headers
=
{}
headers
[
'Cache-Control'
]
=
cache_control
if
cache_control
@file_server
=
::
Rack
::
File
.
new
(
@root
,
headers
)
end
def
match?
(
path
)
...
...
@@ -36,23 +37,48 @@ def match?(path)
end
def
call
(
env
)
@file_server
.
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"
status
,
headers
,
body
=
@file_server
.
call
(
env
)
headers
[
'Content-Encoding'
]
=
'gzip'
headers
[
'Content-Type'
]
=
content_type
(
path
)
else
status
,
headers
,
body
=
@file_server
.
call
(
env
)
end
headers
[
'Vary'
]
=
'Accept-Encoding'
if
gzip_file_exists
return
[
status
,
headers
,
body
]
end
def
ext
@ext
||=
begin
ext
=
::
ActionController
::
Base
.
default_static_extension
"{,
#{
ext
}
,/index
#{
ext
}
}"
private
def
ext
@ext
||=
begin
ext
=
::
ActionController
::
Base
.
default_static_extension
"{,
#{
ext
}
,/index
#{
ext
}
}"
end
end
end
def
unescape_path
(
path
)
URI
.
parser
.
unescape
(
path
)
end
def
unescape_path
(
path
)
URI
.
parser
.
unescape
(
path
)
end
def
escape_glob_chars
(
path
)
path
.
gsub
(
/[*?{}\[\]]/
,
"
\\\\\\
&"
)
end
def
escape_glob_chars
(
path
)
path
.
gsub
(
/[*?{}\[\]]/
,
"
\\\\\\
&"
)
end
def
content_type
(
path
)
::
Rack
::
Mime
.
mime_type
(
::
File
.
extname
(
path
),
'text/plain'
)
end
def
gzip_encoding_accepted?
(
env
)
env
[
'HTTP_ACCEPT_ENCODING'
]
=~
/\bgzip\b/
end
def
gzip_file_exists?
(
path
)
File
.
exist?
(
File
.
join
(
@root
,
"
#{
::
Rack
::
Utils
.
unescape
(
path
)
}
.gz"
))
end
end
# This middleware will attempt to return the contents of a file's body from
...
...
actionpack/test/dispatch/static_test.rb
浏览文件 @
90006acb
# encoding: utf-8
require
'abstract_unit'
require
'rbconfig'
require
'zlib'
module
StaticTests
def
test_serves_dynamic_content
...
...
@@ -106,6 +107,18 @@ def test_serves_static_file_with_at_symbol_in_filename
end
end
def
test_serves_gzip_files_when_header_set
file_name
=
"/gzip/application-a71b3024f80aea3181c09774ca17e712.js"
response
=
get
(
file_name
,
'HTTP_ACCEPT_ENCODING'
=>
'gzip'
)
assert_gzip
file_name
,
response
assert_equal
'application/javascript'
,
response
.
headers
[
'Content-Type'
]
assert_equal
'Accept-Encoding'
,
response
.
headers
[
"Vary"
]
assert_equal
'gzip'
,
response
.
headers
[
"Content-Encoding"
]
response
=
get
(
file_name
,
'HTTP_ACCEPT_ENCODING'
=>
''
)
refute_equal
'gzip'
,
response
.
headers
[
"Content-Encoding"
]
end
# Windows doesn't allow \ / : * ? " < > | in filenames
unless
RbConfig
::
CONFIG
[
'host_os'
]
=~
/mswin|mingw/
def
test_serves_static_file_with_colon
...
...
@@ -125,13 +138,20 @@ def test_serves_static_file_with_asterisk
private
def
assert_gzip
(
file_name
,
response
)
expected
=
File
.
read
(
"
#{
FIXTURE_LOAD_PATH
}
/
#{
public_path
}
"
+
file_name
)
actual
=
Zlib
::
GzipReader
.
new
(
StringIO
.
new
(
response
.
body
)).
read
assert_equal
expected
,
actual
end
def
assert_html
(
body
,
response
)
assert_equal
body
,
response
.
body
assert_equal
"text/html"
,
response
.
headers
[
"Content-Type"
]
refute
response
.
headers
.
key?
(
"Vary"
)
end
def
get
(
path
)
Rack
::
MockRequest
.
new
(
@app
).
request
(
"GET"
,
path
)
def
get
(
path
,
headers
=
{}
)
Rack
::
MockRequest
.
new
(
@app
).
request
(
"GET"
,
path
,
headers
)
end
def
with_static_file
(
file
)
...
...
actionpack/test/fixtures/public/gzip/application-a71b3024f80aea3181c09774ca17e712.js
0 → 100644
浏览文件 @
90006acb
此差异已折叠。
点击以展开。
actionpack/test/fixtures/public/gzip/application-a71b3024f80aea3181c09774ca17e712.js.gz
0 → 100644
浏览文件 @
90006acb
文件已添加
actionpack/test/fixtures/公共/gzip/application-a71b3024f80aea3181c09774ca17e712.js
0 → 100644
浏览文件 @
90006acb
此差异已折叠。
点击以展开。
actionpack/test/fixtures/公共/gzip/application-a71b3024f80aea3181c09774ca17e712.js.gz
0 → 100644
浏览文件 @
90006acb
文件已添加
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录