Skip to content

Commit

Permalink
Temporarily ship with ContentLength middleware.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed May 20, 2011
1 parent 19ee841 commit 5eadb4d
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 15 deletions.
1 change: 0 additions & 1 deletion Gemfile
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion actionpack/actionpack.gemspec
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion railties/lib/rails/application.rb
Expand Up @@ -141,7 +141,7 @@ def config

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"
Expand Down
8 changes: 4 additions & 4 deletions 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
38 changes: 38 additions & 0 deletions 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
5 changes: 0 additions & 5 deletions railties/lib/rails/rack/static.rb

This file was deleted.

6 changes: 3 additions & 3 deletions railties/test/application/middleware_test.rb
Expand Up @@ -19,7 +19,7 @@ def app
boot!

assert_equal [
"Rack::ContentLength",
"Rails::Rack::ContentLength",
"ActionDispatch::Static",
"Rack::Lock",
"ActiveSupport::Cache::Strategy::LocalCache",
Expand Down Expand Up @@ -104,7 +104,7 @@ def app
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
Expand All @@ -127,7 +127,7 @@ def app
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
Expand Down

0 comments on commit 5eadb4d

Please sign in to comment.