Updated README.rdoc again
[feedcatcher.git] / vendor / rails / actionpack / lib / action_controller / vendor / rack-1.0 / rack / content_length.rb
1 require 'rack/utils'
2
3 module Rack
4 # Sets the Content-Length header on responses with fixed-length bodies.
5 class ContentLength
6 include Rack::Utils
7
8 def initialize(app)
9 @app = app
10 end
11
12 def call(env)
13 status, headers, body = @app.call(env)
14 headers = HeaderHash.new(headers)
15
16 if !STATUS_WITH_NO_ENTITY_BODY.include?(status) &&
17 !headers['Content-Length'] &&
18 !headers['Transfer-Encoding'] &&
19 (body.respond_to?(:to_ary) || body.respond_to?(:to_str))
20
21 body = [body] if body.respond_to?(:to_str) # rack 0.4 compat
22 length = body.to_ary.inject(0) { |len, part| len + bytesize(part) }
23 headers['Content-Length'] = length.to_s
24 end
25
26 [status, headers, body]
27 end
28 end
29 end