Skip to content

Commit

Permalink
Handle files from ActionDispatch::Static with Rack::Sendfile (fixes #…
Browse files Browse the repository at this point in the history
…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)
  • Loading branch information
drogus committed Mar 3, 2012
1 parent 4443b36 commit 55dd060
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
8 changes: 4 additions & 4 deletions railties/lib/rails/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ def default_middleware_stack
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
Expand All @@ -242,10 +246,6 @@ def default_middleware_stack
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? }
Expand Down
13 changes: 13 additions & 0 deletions railties/test/application/middleware/sendfile_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,18 @@ def index
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
2 changes: 1 addition & 1 deletion railties/test/application/middleware_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def app
boot!

assert_equal [
"Rack::Sendfile",
"ActionDispatch::Static",
"Rack::Lock",
"ActiveSupport::Cache::Strategy::LocalCache",
Expand All @@ -36,7 +37,6 @@ def app
"ActionDispatch::ShowExceptions",
"ActionDispatch::DebugExceptions",
"ActionDispatch::RemoteIp",
"Rack::Sendfile",
"ActionDispatch::Reloader",
"ActionDispatch::Callbacks",
"ActiveRecord::ConnectionAdapters::ConnectionManagement",
Expand Down

0 comments on commit 55dd060

Please sign in to comment.