Sha256: 6cb63ad330759211c94d53428b304d110c5c68cc5169e84590601e926b4dbaa8

Contents?: true

Size: 955 Bytes

Versions: 12

Compression:

Stored size: 955 Bytes

Contents

require 'action_dispatch'
require 'rack/utils'

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

      def initialize(app, sendfile=nil)
        @app = app
        @sendfile = sendfile
      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'] &&
           !(@sendfile && headers[@sendfile])

          old_body = body
          body, length = [], 0
          old_body.each do |part|
            body << part
            length += bytesize(part)
          end
          old_body.close if old_body.respond_to?(:close)
          headers['Content-Length'] = length.to_s
        end

        [status, headers, body]
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
railties-3.1.1 lib/rails/rack/content_length.rb
railties-3.1.1.rc3 lib/rails/rack/content_length.rb
railties-3.1.1.rc2 lib/rails/rack/content_length.rb
railties-3.1.1.rc1 lib/rails/rack/content_length.rb
railties-3.1.0 lib/rails/rack/content_length.rb
railties-3.1.0.rc8 lib/rails/rack/content_length.rb
railties-3.1.0.rc6 lib/rails/rack/content_length.rb
railties-3.1.0.rc5 lib/rails/rack/content_length.rb
railties-3.1.0.rc4 lib/rails/rack/content_length.rb
railties-3.1.0.rc3 lib/rails/rack/content_length.rb
railties-3.1.0.rc2 lib/rails/rack/content_length.rb
railties-3.1.0.rc1 lib/rails/rack/content_length.rb