Class: Rack::ContentType

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

Overview

Sets the content-type header on responses which don’t have one.

Builder Usage:

use Rack::ContentType, "text/plain"

When no content type argument is provided, “text/html” is the default.

Constant Summary

Constants included from Utils

Utils::COMMON_SEP, Utils::DEFAULT_SEP, Utils::HTTP_STATUS_CODES, Utils::InvalidParameterError, Utils::KeySpaceConstrainedParams, Utils::NULL_BYTE, Utils::PATH_SEPS, Utils::ParameterTypeError, Utils::ParamsTooDeepError, 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, clean_path_info, #clock_time, delete_cookie_header!, delete_set_cookie_header, delete_set_cookie_header!, escape, escape_path, forwarded_values, get_byte_ranges, param_depth_limit, param_depth_limit=, parse_cookies, parse_cookies_header, parse_nested_query, parse_query, q_values, rfc2822, #secure_compare, select_best_encoding, set_cookie_header, set_cookie_header!, status_code, unescape, unescape_path, valid_path?

Constructor Details

#initialize(app, content_type = "text/html") ⇒ ContentType

Returns a new instance of ContentType.



18
19
20
21
# File 'lib/rack/content_type.rb', line 18

def initialize(app, content_type = "text/html")
  @app = app
  @content_type = content_type
end

Instance Method Details

#call(env) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/rack/content_type.rb', line 23

def call(env)
  status, headers, _ = response = @app.call(env)

  unless STATUS_WITH_NO_ENTITY_BODY.key?(status.to_i)
    headers[CONTENT_TYPE] ||= @content_type
  end

  response
end