Sha256: b3163b86520fa6b6f4ffb4331dd1b3e16eebd4f7441e6cf1004c224540937581

Contents?: true

Size: 730 Bytes

Versions: 14

Compression:

Stored size: 730 Bytes

Contents

module Rack
  # Sets the Content-Length header on responses with fixed-length bodies.
  class ContentLength
    def initialize(app)
      @app = app
    end

    def call(env)
      status, headers, body = @app.call(env)
      headers = Utils::HeaderHash.new(headers)

      if !Utils::STATUS_WITH_NO_ENTITY_BODY.include?(status) &&
         !headers['Content-Length'] &&
         !headers['Transfer-Encoding'] &&
         (body.respond_to?(:to_ary) || body.respond_to?(:to_str))

        body = [body] if body.respond_to?(:to_str) # rack 0.4 compat
        length = body.to_ary.inject(0) { |len, part| len + part.length }
        headers['Content-Length'] = length.to_s
      end

      [status, headers, body]
    end
  end
end

Version data entries

14 entries across 14 versions & 6 rubygems

Version Path
p8-castronaut-0.6.1.1 vendor/rack/lib/rack/content_length.rb
relevance-castronaut-0.6.0 vendor/rack/lib/rack/content_length.rb
relevance-castronaut-0.6.1 vendor/rack/lib/rack/content_length.rb
relevance-castronaut-0.7.4 vendor/rack/lib/rack/content_length.rb
relevance-castronaut-0.7.5 vendor/rack/lib/rack/content_length.rb
nbudin-castronaut-0.7.5 vendor/rack/lib/rack/content_length.rb
mack-0.8.3 lib/gems/rack-0.9.1/lib/rack/content_length.rb
mack-0.8.3.1 lib/gems/rack-0.9.1/lib/rack/content_length.rb
passenger-2.1.2 vendor/rack-0.9.1/lib/rack/content_length.rb
passenger-2.1.3 vendor/rack-0.9.1/lib/rack/content_length.rb
passenger-2.2.0 vendor/rack-0.9.1/lib/rack/content_length.rb
passenger-2.2.1 vendor/rack-0.9.1/lib/rack/content_length.rb
rack-0.9.0 lib/rack/content_length.rb
rack-0.9.1 lib/rack/content_length.rb