Module: WebPipe::ConnSupport::Headers Private

Defined in:
lib/web_pipe/conn_support/headers.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Constant Summary collapse

HEADERS_AS_CGI =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Headers which come as plain CGI-like variables (without the HTTP_ prefixed) from the rack server.

%w[CONTENT_TYPE CONTENT_LENGTH].freeze

Class Method Summary collapse

Class Method Details

.add(headers, key, value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



26
27
28
29
30
# File 'lib/web_pipe/conn_support/headers.rb', line 26

def self.add(headers, key, value)
  Hash[
    headers.to_a.push(pair(key, value))
  ]
end

.delete(headers, key) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



32
33
34
# File 'lib/web_pipe/conn_support/headers.rb', line 32

def self.delete(headers, key)
  headers.reject { |k, _v| normalize_key(key) == k }
end

.extract(env) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Headers are all those pairs which key begins with HTTP_ plus those detailed in HEADERS_AS_CGI.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/web_pipe/conn_support/headers.rb', line 13

def self.extract(env)
  Hash[
    env
    .select { |k, _v| k.start_with?('HTTP_') }
    .map { |k, v| pair(k[5..], v) }
    .concat(
      env
        .select { |k, _v| HEADERS_AS_CGI.include?(k) }
        .map { |k, v| pair(k, v) }
    )
  ]
end

.normalize(headers) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



52
53
54
# File 'lib/web_pipe/conn_support/headers.rb', line 52

def self.normalize(headers)
  headers.transform_keys { |k| normalize_key(k) }
end

.normalize_key(key) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

As per RFC2616, headers names are case insensitive. This function normalizes them to PascalCase acting on dashes ('-').

When a rack server maps headers to CGI-like variables, both dashes and underscores (_) are treated as dashes. This function substitutes all '-' to '_'.

See https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2



48
49
50
# File 'lib/web_pipe/conn_support/headers.rb', line 48

def self.normalize_key(key)
  key.downcase.gsub('_', '-').split('-').map(&:capitalize).join('-')
end

.pair(key, value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



36
37
38
# File 'lib/web_pipe/conn_support/headers.rb', line 36

def self.pair(key, value)
  [normalize_key(key), value]
end