From 5eadb4d73dc5384509efeea3b9584ded19956c7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 20 May 2011 22:20:51 +0200 Subject: [PATCH] Temporarily ship with ContentLength middleware. --- Gemfile | 1 - actionpack/actionpack.gemspec | 2 +- railties/lib/rails/application.rb | 2 +- railties/lib/rails/rack.rb | 8 ++--- railties/lib/rails/rack/content_length.rb | 38 ++++++++++++++++++++ railties/lib/rails/rack/static.rb | 5 --- railties/test/application/middleware_test.rb | 6 ++-- 7 files changed, 47 insertions(+), 15 deletions(-) create mode 100644 railties/lib/rails/rack/content_length.rb delete mode 100644 railties/lib/rails/rack/static.rb diff --git a/Gemfile b/Gemfile index e2128abd9f..e3cfd12133 100644 --- a/Gemfile +++ b/Gemfile @@ -11,7 +11,6 @@ end gem "coffee-script" gem "sass" gem "uglifier", :git => "git://github.com/lautis/uglifier.git" -gem "rack", :git => "git://github.com/rack/rack.git" gem "rake", ">= 0.8.7" gem "mocha", ">= 0.9.8" diff --git a/actionpack/actionpack.gemspec b/actionpack/actionpack.gemspec index 08d5d689ae..45958ec75a 100644 --- a/actionpack/actionpack.gemspec +++ b/actionpack/actionpack.gemspec @@ -22,7 +22,7 @@ Gem::Specification.new do |s| s.add_dependency('rack-cache', '~> 1.0.1') s.add_dependency('builder', '~> 3.0.0') s.add_dependency('i18n', '~> 0.6.0beta1') - s.add_dependency('rack', '~> 1.3.0.beta') + s.add_dependency('rack', '~> 1.3.0.beta2') s.add_dependency('rack-test', '~> 0.6.0') s.add_dependency('rack-mount', '~> 0.8.1') s.add_dependency('sprockets', '~> 2.0.0.beta.5') diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 88ef95334f..816bff29b7 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -141,7 +141,7 @@ module Rails def default_middleware_stack ActionDispatch::MiddlewareStack.new.tap do |middleware| - middleware.use ::Rack::ContentLength, config.action_dispatch.x_sendfile_header + middleware.use ::Rails::Rack::ContentLength, config.action_dispatch.x_sendfile_header if rack_cache = config.action_controller.perform_caching && config.action_dispatch.rack_cache require "action_dispatch/http/rack_cache" diff --git a/railties/lib/rails/rack.rb b/railties/lib/rails/rack.rb index 1f20ceae44..d4a41b217e 100644 --- a/railties/lib/rails/rack.rb +++ b/railties/lib/rails/rack.rb @@ -1,8 +1,8 @@ module Rails module Rack - autoload :Debugger, "rails/rack/debugger" - autoload :Logger, "rails/rack/logger" - autoload :LogTailer, "rails/rack/log_tailer" - autoload :Static, "rails/rack/static" + autoload :ContentLength, "rails/rack/content_length" + autoload :Debugger, "rails/rack/debugger" + autoload :Logger, "rails/rack/logger" + autoload :LogTailer, "rails/rack/log_tailer" end end diff --git a/railties/lib/rails/rack/content_length.rb b/railties/lib/rails/rack/content_length.rb new file mode 100644 index 0000000000..6839af4152 --- /dev/null +++ b/railties/lib/rails/rack/content_length.rb @@ -0,0 +1,38 @@ +require 'action_dispatch' +require 'rack/utils' + +module Rails + module Rack + # Sets the Content-Length header on responses with fixed-length bodies. + class ContentLength + include ::Rack::Utils + + def initialize(app, sendfile=nil) + @app = app + @sendfile = sendfile + end + + def call(env) + status, headers, body = @app.call(env) + headers = HeaderHash.new(headers) + + if !STATUS_WITH_NO_ENTITY_BODY.include?(status.to_i) && + !headers['Content-Length'] && + !headers['Transfer-Encoding'] && + !(@sendfile && headers[@sendfile]) + + old_body = body + body, length = [], 0 + old_body.each do |part| + body << part + length += bytesize(part) + end + old_body.close if old_body.respond_to?(:close) + headers['Content-Length'] = length.to_s + end + + [status, headers, body] + end + end + end +end \ No newline at end of file diff --git a/railties/lib/rails/rack/static.rb b/railties/lib/rails/rack/static.rb deleted file mode 100644 index ebe8b9e103..0000000000 --- a/railties/lib/rails/rack/static.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'action_dispatch' - -module Rails::Rack - Static = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('Rails::Rack::Static', ActionDispatch::Static) -end diff --git a/railties/test/application/middleware_test.rb b/railties/test/application/middleware_test.rb index 6f14200a14..ef448f7791 100644 --- a/railties/test/application/middleware_test.rb +++ b/railties/test/application/middleware_test.rb @@ -19,7 +19,7 @@ module ApplicationTests boot! assert_equal [ - "Rack::ContentLength", + "Rails::Rack::ContentLength", "ActionDispatch::Static", "Rack::Lock", "ActiveSupport::Cache::Strategy::LocalCache", @@ -104,7 +104,7 @@ module ApplicationTests end test "insert middleware after" do - add_to_config "config.middleware.insert_after Rack::ContentLength, Rack::Config" + add_to_config "config.middleware.insert_after Rails::Rack::ContentLength, Rack::Config" boot! assert_equal "Rack::Config", middleware.second end @@ -127,7 +127,7 @@ module ApplicationTests end test "insert middleware before" do - add_to_config "config.middleware.insert_before Rack::ContentLength, Rack::Config" + add_to_config "config.middleware.insert_before Rails::Rack::ContentLength, Rack::Config" boot! assert_equal "Rack::Config", middleware.first end