summary refs log tree commit
path: root/lib/rack/content_length.rb
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2011-05-19 09:31:18 -0400
committerJosé Valim <jose.valim@gmail.com>2011-05-19 09:31:18 -0400
commit2b3abc73f3e3a23110f2c8f43458416674130511 (patch)
treea9923c356a2be97251f427154b3f08e0f491c5d1 /lib/rack/content_length.rb
parenta4a788e48458a501636fe9955c2a5e3de0fa1b7d (diff)
downloadrack-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.rb13
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