Sha256: 6a574668926b20ab9a2c36aa308bfeec2707097d36ba5e8a170fa71ca6f53db5

Contents?: true

Size: 864 Bytes

Versions: 9

Compression:

Stored size: 864 Bytes

Contents

# frozen_string_literal: true

require 'rack/utils'
require 'rack/body_proxy'

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.key?(status.to_i) &&
         !headers[CONTENT_LENGTH] &&
         !headers[TRANSFER_ENCODING] &&
         body.respond_to?(:to_ary)

        obody = body
        body, length = [], 0
        obody.each { |part| body << part; length += part.bytesize }

        body = BodyProxy.new(body) do
          obody.close if obody.respond_to?(:close)
        end

        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-2.1.4.4 lib/rack/content_length.rb
rack-2.1.4.3 lib/rack/content_length.rb
rack-2.1.4.2 lib/rack/content_length.rb
rack-2.1.4.1 lib/rack/content_length.rb
rack-2.1.4 lib/rack/content_length.rb
rack-2.1.3 lib/rack/content_length.rb
rack-2.1.2 lib/rack/content_length.rb
rack-2.1.1 lib/rack/content_length.rb
rack-2.1.0 lib/rack/content_length.rb