diff options
author | José Valim <jose.valim@gmail.com> | 2011-05-19 09:31:18 -0400 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2011-05-19 09:31:18 -0400 |
commit | 2b3abc73f3e3a23110f2c8f43458416674130511 (patch) | |
tree | a9923c356a2be97251f427154b3f08e0f491c5d1 /lib/rack/content_length.rb | |
parent | a4a788e48458a501636fe9955c2a5e3de0fa1b7d (diff) | |
download | rack-2b3abc73f3e3a23110f2c8f43458416674130511.tar.gz |
Improve Rack::ContentLength middleware.
1) It makes Rack::ContentLength middleware conform with the rack specification by removing the to_ary check; 2) Make Rack::ContentLength accept an extra header argument that checks for sendfile headers
Diffstat (limited to 'lib/rack/content_length.rb')
-rw-r--r-- | lib/rack/content_length.rb | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/lib/rack/content_length.rb b/lib/rack/content_length.rb index 32f31142..f5e1932c 100644 --- a/lib/rack/content_length.rb +++ b/lib/rack/content_length.rb @@ -5,8 +5,9 @@ module Rack class ContentLength include Rack::Utils - def initialize(app) + def initialize(app, sendfile=nil) @app = app + @sendfile = sendfile end def call(env) @@ -16,10 +17,14 @@ module Rack if !STATUS_WITH_NO_ENTITY_BODY.include?(status.to_i) && !headers['Content-Length'] && !headers['Transfer-Encoding'] && - body.respond_to?(:to_ary) + !(@sendfile && headers[@sendfile]) - length = 0 - body.each { |part| length += bytesize(part) } + new_body, length = [], 0 + body.each do |part| + new_body << part + length += bytesize(part) + end + body = new_body headers['Content-Length'] = length.to_s end |