Class: HeavensDoor::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/heavens_door/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.


5
6
7
# File 'lib/heavens_door/middleware.rb', line 5

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object


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

def call(env)
  status, headers, body = @app.call env

  if headers && headers['Content-Type']&.include?('text/html') &&
      (status / 100 != 3) &&  # redirections
      (!env['action_dispatch.content_security_policy']&.script_src('unsafe-inline') && !env['action_dispatch.content_security_policy']&.style_src('unsafe-inline'))  # the Rails default top page has this
    case body
    when ActionDispatch::Response, ActionDispatch::Response::RackBody
      body = body.body
    when Array
      body = body[0]
    end

    body = body.dup if body.frozen?
    body.sub!(/<\/head[^>]*>/) { %Q[<link rel="stylesheet" href="#{Rails.application.config.assets.prefix}/heavens_door.css" /><script src="#{Rails.application.config.assets.prefix}/heavens_door.js"></script>\n#{$~}] }
    body.sub!(/<body[^>]*>/) { %Q[#{$~}\n<div id="heavens-door" class="heavens-door-custom"><span id="heavens-door-open" class="heavens-door-button">⏺</span><span id="heavens-door-close" class="heavens-door-button">⏹</span><span id="heavens-door-copy" class="heavens-door-button">📋</span></div>] }

    [status, headers, [body]]
  else
    [status, headers, body]
  end
end