class SiteHub::HttpHeaders

Constants

EXCLUDED_HEADERS
HTTP_PREFIX
RACK_HTTP_HEADER_ID

Public Class Methods

from_rack_env(env) click to toggle source
# File lib/sitehub/http_headers.rb, line 7
def from_rack_env(env)
  new(format_keys(remove_rack_specific_headers(env.dup)))
end
new(env) click to toggle source
# File lib/sitehub/http_headers.rb, line 45
def initialize(env)
  env.each do |key, value|
    self[key.to_s.downcase] = value
  end

  filter_prohibited_headers
end

Private Class Methods

format_keys(env) click to toggle source
# File lib/sitehub/http_headers.rb, line 20
def format_keys(env)
  env.each_with_object({}) do |key_value, hash|
    key, value = *key_value
    hash[header_name(key)] = value
  end
end
header_name(name) click to toggle source
# File lib/sitehub/http_headers.rb, line 27
def header_name(name)
  name.sub(HTTP_PREFIX, EMPTY_STRING).downcase.gsub(UNDERSCORE, HYPHEN)
end
remove_rack_specific_headers(env) click to toggle source
# File lib/sitehub/http_headers.rb, line 13
def remove_rack_specific_headers(env)
  env.reject do |key, value|
    !Constants::RackHttpHeaderKeys::HTTP_HEADER_FILTER_EXCEPTIONS.include?(key.to_s.upcase) &&
      (!RACK_HTTP_HEADER_ID.match(key) || !value)
  end
end

Private Instance Methods

filter_prohibited_headers() click to toggle source
# File lib/sitehub/http_headers.rb, line 55
def filter_prohibited_headers
  remove_headers(hop_by_hop_headers.concat(EXCLUDED_HEADERS))
end
hop_by_hop_headers() click to toggle source
# File lib/sitehub/http_headers.rb, line 59
def hop_by_hop_headers
  field = self[CONNECTION_HEADER] || EMPTY_STRING
  field.split(COMMA).collect(&:downcase)
end
remove_headers(excluded) click to toggle source
# File lib/sitehub/http_headers.rb, line 64
def remove_headers(excluded)
  reject! do |key, _value|
    excluded.member?(key)
  end
end