Module: SiteHub::CookieRewriting

Included in:
Middleware::ReverseProxy
Defined in:
lib/sitehub/cookie_rewriting.rb

Overview

TODO: - change in to object and remove .reek exclusions for UtilityFunction

Constant Summary collapse

ENDING_WITH_NEWLINE =
/#{NEW_LINE}$/

Instance Method Summary collapse

Instance Method Details

#cookies_hash_to_string(cookies_hash) ⇒ Object



27
28
29
30
31
# File 'lib/sitehub/cookie_rewriting.rb', line 27

def cookies_hash_to_string(cookies_hash)
  cookies_hash.values.inject(EMPTY_STRING.dup) do |cookie_string, cookie|
    cookie_string << "#{cookie}#{NEW_LINE}"
  end.sub(ENDING_WITH_NEWLINE, EMPTY_STRING)
end

#cookies_string_as_hash(cookie_string) ⇒ Object



33
34
35
36
37
38
# File 'lib/sitehub/cookie_rewriting.rb', line 33

def cookies_string_as_hash(cookie_string)
  cookie_string.lines.each_with_object({}) do |cookie_line, cookies|
    cookie = SiteHub::Cookie.new(cookie_line)
    cookies[cookie.name] = cookie
  end
end

#rewrite_cookies(headers, substitute_domain:) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/sitehub/cookie_rewriting.rb', line 8

def rewrite_cookies(headers, substitute_domain:)
  cookies_hash = cookies_string_as_hash(headers[Constants::HttpHeaderKeys::SET_COOKIE])

  cookies_hash.values.each do |cookie|
    domain_attribute = cookie.find(:domain) || next
    update_domain(domain_attribute, substitute_domain)
  end
  headers[Constants::HttpHeaderKeys::SET_COOKIE] = cookies_hash_to_string(cookies_hash)
end

#update_domain(domain_attribute, substitute_domain) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/sitehub/cookie_rewriting.rb', line 18

def update_domain(domain_attribute, substitute_domain)
  substitute = substitute_domain.dup
  if domain_attribute.value.start_with?(FULL_STOP)
    domain_attribute.update(substitute.prepend(FULL_STOP))
  else
    domain_attribute.update(substitute)
  end
end