Class: Rack::ContentLength

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

Overview

Sets the Content-Length header on responses that do not specify a Content-Length or Transfer-Encoding header. Note that this does not fix responses that have an invalid Content-Length header specified.

Constant Summary

Constants included from Utils

Utils::COMMON_SEP, Utils::DEFAULT_SEP, Utils::ESCAPE_HTML, Utils::ESCAPE_HTML_PATTERN, Utils::HTTP_STATUS_CODES, Utils::InvalidParameterError, Utils::KeySpaceConstrainedParams, Utils::NULL_BYTE, Utils::PATH_SEPS, Utils::ParameterTypeError, Utils::RFC2822_DAY_NAME, Utils::RFC2822_MONTH_NAME, Utils::STATUS_WITH_NO_ENTITY_BODY, Utils::SYMBOL_TO_STATUS_CODE

Instance Method Summary collapse

Methods included from Utils

add_cookie_to_header, add_remove_cookie_to_header, best_q_match, build_nested_query, build_query, byte_ranges, clean_path_info, #clock_time, delete_cookie_header!, escape, escape_html, escape_path, get_byte_ranges, key_space_limit, key_space_limit=, make_delete_cookie_header, param_depth_limit, param_depth_limit=, parse_cookies, parse_cookies_header, parse_nested_query, parse_query, q_values, rfc2109, rfc2822, secure_compare, select_best_encoding, set_cookie_header!, status_code, unescape, unescape_path, valid_path?

Constructor Details

#initialize(app) ⇒ ContentLength

Returns a new instance of ContentLength.



12
13
14
# File 'lib/rack/content_length.rb', line 12

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



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

def call(env)
  status, headers, body = @app.call(env)
  headers = HeaderHash[headers]

  if !STATUS_WITH_NO_ENTITY_BODY.key?(status.to_i) &&
     !headers[CONTENT_LENGTH] &&
     !headers[TRANSFER_ENCODING]

    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