Class: Shugoi::RenderHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/shugoi/render_handler.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, token_signer, html_store, config_cache, _pow = nil) ⇒ RenderHandler

Returns a new instance of RenderHandler.



3
4
5
6
7
8
# File 'lib/shugoi/render_handler.rb', line 3

def initialize(config, token_signer, html_store, config_cache, _pow = nil)
  @config = config
  @token_signer = token_signer
  @html_store = html_store
  @config_cache = config_cache
end

Class Method Details

.inject_referrer_policy(html) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/shugoi/render_handler.rb', line 31

def self.inject_referrer_policy(html)
  meta = '<meta name="referrer" content="strict-origin-when-cross-origin">'
  if html.include?("<head>")
    html.sub("<head>", "<head>#{meta}")
  elsif (m = html.match(/<html[^>]*>/))
    html.sub(m[0], "#{m[0]}#{meta}")
  else
    "#{meta}#{html}"
  end
end

Instance Method Details

#render_data(token, mid, grant, ip) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/shugoi/render_handler.rb', line 10

def render_data(token, mid, grant, ip)
  return { error: "not_found" } if token.to_s.empty? || token.length < 16 || token.length > 300
  tok_site_key = token.split(":")[0]
  return { error: "not_found" } if tok_site_key != @config.site_key
  tok_ts = token.split(":")[1].to_i
  return { error: "not_found" } if !tok_ts.zero? && Utils.now_ms - tok_ts > HtmlStore::TOKEN_TTL_MS
  return { error: "not_found" } unless @token_signer.verify_render_grant(mid, grant, token, ip, @config.site_key)

  content_replace_on = content_replace_flag?(token)
  html = @html_store.read(token)
  return { html: inject_notice(html, mid) } if html
  unless content_replace_on
    site_html = @html_store.site_html(tok_site_key)
    return { html: inject_notice(site_html, mid) } if site_html
  end

  return { error: "not_found" } unless @token_signer.verify_token(token)

  { error: "not_found" }
end