Class: Rails::Rack::ContentLength

Inherits:
Object
  • Object
show all
Includes:
Rack::Utils
Defined in:
railties/lib/rails/rack/content_length.rb

Overview

Sets the Content-Length header on responses with fixed-length bodies.

Instance Method Summary collapse

Constructor Details

#initialize(app, sendfile = nil) ⇒ ContentLength

Returns a new instance of ContentLength.



10
11
12
13
# File 'railties/lib/rails/rack/content_length.rb', line 10

def initialize(app, sendfile=nil)
  @app = app
  @sendfile = sendfile
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'railties/lib/rails/rack/content_length.rb', line 15

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