Sha256: bb26cc41a042c8caad3f3069ee778c7468cd81dd3f34bc7e4cb1442da020cd2e

Contents?: true

Size: 771 Bytes

Versions: 9

Compression:

Stored size: 771 Bytes

Contents

require 'rack/utils'

module Rack
  # Sets the Content-Length header on responses with fixed-length bodies.
  class ContentLength
    include Rack::Utils

    def initialize(app)
      @app = app
    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'] &&
         (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 + bytesize(part) }
        headers['Content-Length'] = length.to_s
      end

      [status, headers, body]
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rack-1.2.8 lib/rack/content_length.rb
rack-1.2.7 lib/rack/content_length.rb
rack-1.2.6 lib/rack/content_length.rb
rack-1.2.5 lib/rack/content_length.rb
rack-1.2.4 lib/rack/content_length.rb
rack-1.2.3 lib/rack/content_length.rb
rack-1.2.2 lib/rack/content_length.rb
rack-1.2.1 lib/rack/content_length.rb
rack-1.2.0 lib/rack/content_length.rb