Class: ActionDispatch::Http::Headers

Inherits:
Object
  • Object
show all
Defined in:
runtime/action_dispatch/headers.rbs,
runtime/action_dispatch/headers.rb

Constant Summary collapse

CGI_VARIABLES =

The env keys that are CGI variables rather than HTTP_-prefixed header keys. Content-Type and Content-Length are the two request headers in this set.

%w[
  AUTH_TYPE CONTENT_LENGTH CONTENT_TYPE GATEWAY_INTERFACE HTTPS
  PATH_INFO PATH_TRANSLATED QUERY_STRING REMOTE_ADDR REMOTE_HOST
  REMOTE_IDENT REMOTE_USER REQUEST_METHOD SCRIPT_NAME SERVER_NAME
  SERVER_PORT SERVER_PROTOCOL SERVER_SOFTWARE
].freeze
HTTP_HEADER =

A lookup key that is only letters, digits, and dashes is an HTTP header name; an underscore means the caller already named an env key.

/\A[A-Za-z0-9-]+\z/
DEFAULT =

The sentinel that tells fetch "no default was given".

Object.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Headers

wrap the request's env hash. The hash is read live, so a view created once sees the request's current headers.

Parameters:

  • env (Hash{String => untyped}) —

    the request's internal env hash



29
# File 'runtime/action_dispatch/headers.rb', line 29

def initialize: (Hash[String, untyped] env) -> void

Class Method Details

.env_key(name) ⇒ String

the env key for a wire header name. The dispatcher uses this when copying parsed headers into the request's env hash.

Parameters:

  • name (String) —

    the wire header name

Returns:

  • (String) —

    the Rack-style env key



102
# File 'runtime/action_dispatch/headers.rb', line 102

def self.env_key: (String name) -> String

Instance Method Details

#[](key) ⇒ untyped

the header value, or nil when absent. Accepts both an HTTP header name (Authorization) and an env key (HTTP_AUTHORIZATION, CONTENT_TYPE).

Examples:

headers["Authorization"] # => "Bearer token"

Parameters:

  • key (String) —

    the header name or env key

Returns:

  • (untyped) —

    the header value, nil when absent



40
# File 'runtime/action_dispatch/headers.rb', line 40

def []: (String key) -> untyped

#each_header {|name, value| ... } ⇒ void

yield each request header as a name and value. Names are normalized to lowercase with dashes (authorization, content-type). Only header keys are yielded; env scratch keys are skipped.

Yield Parameters:

  • name (String) —

    the normalized header name

  • value (untyped) —

    the header value



78
# File 'runtime/action_dispatch/headers.rb', line 78

def each_header: () { (String name, untyped value) -> void } -> void

#fetch(key, default = DEFAULT) ⇒ untyped

the header value; without a default, raises KeyError when absent. An empty header value is present, so it answers "" rather than the default.

Parameters:

  • key (String) —

    the header name or env key

  • default (untyped) (defaults to: DEFAULT) —

    the value to answer when the header is absent

Returns:

  • (untyped) —

    the header value, or the default

Raises:

  • (KeyError) —

    the header is absent and no default was given



53
# File 'runtime/action_dispatch/headers.rb', line 53

def fetch: (String key, ?untyped default) -> untyped

#has_key?(key) ⇒ Boolean

whether the header is present. An empty value is present; only an absent key answers false.

Parameters:

  • key (String) —

    the header name or env key

Returns:

  • (Boolean) —

    true when the header key exists



67
# File 'runtime/action_dispatch/headers.rb', line 67

def has_key?: (String key) -> bool

#to_h ⇒ Hash{String => untyped}

every request header as a Hash keyed by normalized name.

Returns:

  • (Hash{String => untyped}) —

    the header names and values



88
# File 'runtime/action_dispatch/headers.rb', line 88

def to_h: () -> Hash[String, untyped]