Class: Rack::ContentLength

Inherits:
Object show all
Includes:
Utils
Defined in:
lib/vendor/rack-1.5.2/lib/rack/content_length.rb

Overview

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

Constant Summary

Constants included from Utils

Utils::DEFAULT_SEP, Utils::ESCAPE_HTML, Utils::ESCAPE_HTML_PATTERN, Utils::HTTP_STATUS_CODES, Utils::Multipart, Utils::STATUS_WITH_NO_ENTITY_BODY, Utils::SYMBOL_TO_STATUS_CODE

Instance Method Summary collapse

Methods included from Utils

best_q_match, build_nested_query, build_query, byte_ranges, bytesize, delete_cookie_header!, escape, escape_html, escape_path, normalize_params, params_hash_type?, parse_nested_query, parse_query, q_values, rfc2109, rfc2822, secure_compare, select_best_encoding, set_cookie_header!, status_code, unescape

Constructor Details

#initialize(app) ⇒ ContentLength

Returns a new instance of ContentLength.



9
10
11
# File 'lib/vendor/rack-1.5.2/lib/rack/content_length.rb', line 9

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/vendor/rack-1.5.2/lib/rack/content_length.rb', line 13

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)

    obody = body
    body, length = [], 0
    obody.each { |part| body << part; length += bytesize(part) }
    obody.close if obody.respond_to?(:close)

    headers['Content-Length'] = length.to_s
  end

  [status, headers, body]
end