diff options
author | Piotr Sarnacki <drogus@gmail.com> | 2012-03-03 20:05:45 +0100 |
---|---|---|
committer | Piotr Sarnacki <drogus@gmail.com> | 2012-03-03 20:46:56 +0100 |
commit | 55dd060347bbd89c21b4c0e30ba8fbf4ad792c4e (patch) | |
tree | d5d026a5c04f4ae6ee839a9156d06b64be5a9d95 /railties | |
parent | 4443b3647d9e350713eb20a720997ded62b6848e (diff) | |
download | rails-55dd060347bbd89c21b4c0e30ba8fbf4ad792c4e.tar.gz rails-55dd060347bbd89c21b4c0e30ba8fbf4ad792c4e.tar.bz2 rails-55dd060347bbd89c21b4c0e30ba8fbf4ad792c4e.zip |
Handle files from ActionDispatch::Static with Rack::Sendfile (fixes #5225)
This makes rails behave properly when you serve static assets
and you have X-Sendfile headers enabled. Nevertheless in most
cases you should not rely on that and serve static assets with
a webserver like Apache or Nginx (as you already have it in
place anyway if you use X-Sendfile)
Diffstat (limited to 'railties')
-rw-r--r-- | railties/lib/rails/application.rb | 8 | ||||
-rw-r--r-- | railties/test/application/middleware/sendfile_test.rb | 13 | ||||
-rw-r--r-- | railties/test/application/middleware_test.rb | 2 |
3 files changed, 18 insertions, 5 deletions
diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index c163081bfc..8d64aff430 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -229,6 +229,10 @@ module Rails middleware.use ::Rack::SSL, config.ssl_options end + if config.action_dispatch.x_sendfile_header.present? + middleware.use ::Rack::Sendfile, config.action_dispatch.x_sendfile_header + end + if config.serve_static_assets middleware.use ::ActionDispatch::Static, paths["public"].first, config.static_cache_control end @@ -242,10 +246,6 @@ module Rails middleware.use ::ActionDispatch::DebugExceptions middleware.use ::ActionDispatch::RemoteIp, config.action_dispatch.ip_spoofing_check, config.action_dispatch.trusted_proxies - if config.action_dispatch.x_sendfile_header.present? - middleware.use ::Rack::Sendfile, config.action_dispatch.x_sendfile_header - end - unless config.cache_classes app = self middleware.use ::ActionDispatch::Reloader, lambda { app.reload_dependencies? } diff --git a/railties/test/application/middleware/sendfile_test.rb b/railties/test/application/middleware/sendfile_test.rb index 0591386a87..eb791f5687 100644 --- a/railties/test/application/middleware/sendfile_test.rb +++ b/railties/test/application/middleware/sendfile_test.rb @@ -57,5 +57,18 @@ module ApplicationTests get "/" assert_equal File.expand_path(__FILE__), last_response.headers["X-Lighttpd-Send-File"] end + + test "files handled by ActionDispatch::Static are handled by Rack::Sendfile" do + make_basic_app do |app| + app.config.action_dispatch.x_sendfile_header = 'X-Sendfile' + app.config.serve_static_assets = true + app.paths["public"] = File.join(rails_root, "public") + end + + app_file "public/foo.txt", "foo" + + get "/foo.txt", "HTTP_X_SENDFILE_TYPE" => "X-Sendfile" + assert_equal File.join(rails_root, "public/foo.txt"), last_response.headers["X-Sendfile"] + end end end diff --git a/railties/test/application/middleware_test.rb b/railties/test/application/middleware_test.rb index d0a550d2f0..a1e6066b16 100644 --- a/railties/test/application/middleware_test.rb +++ b/railties/test/application/middleware_test.rb @@ -26,6 +26,7 @@ module ApplicationTests boot! assert_equal [ + "Rack::Sendfile", "ActionDispatch::Static", "Rack::Lock", "ActiveSupport::Cache::Strategy::LocalCache", @@ -36,7 +37,6 @@ module ApplicationTests "ActionDispatch::ShowExceptions", "ActionDispatch::DebugExceptions", "ActionDispatch::RemoteIp", - "Rack::Sendfile", "ActionDispatch::Reloader", "ActionDispatch::Callbacks", "ActiveRecord::ConnectionAdapters::ConnectionManagement", |