Class: WebConsole::Middleware

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

Constant Summary collapse

TEMPLATES_PATH =
File.expand_path("../templates", __FILE__)

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



12
13
14
# File 'lib/web_console/middleware.rb', line 12

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/web_console/middleware.rb', line 16

def call(env)
  app_exception = catch :app_exception do
    request = create_regular_or_whiny_request(env)
    return call_app(env) unless request.permitted?

    if id = id_for_repl_session_update(request)
      return update_repl_session(id, request)
    elsif id = id_for_repl_session_stack_frame_change(request)
      return change_stack_trace(id, request)
    end


    status, headers, body = call_app(env)

    if (session = Session.from(Thread.current)) && acceptable_content_type?(headers)
      headers["x-web-console-session-id"] = session.id
      headers["x-web-console-mount-point"] = mount_point

      template = Template.new(env, session)
      body, headers = Injector.new(body, headers).inject(template.render("index"))
    end

    [ status, headers, body ]
  end
rescue => e
  WebConsole.logger.error("\n#{e.class}: #{e}\n\tfrom #{e.backtrace.join("\n\tfrom ")}")
  raise e
ensure
  # Clean up the fiber locals after the session creation. Object#console
  # uses those to communicate the current binding or exception to the middleware.
  Thread.current[:__web_console_exception] = nil
  Thread.current[:__web_console_binding] = nil

  raise app_exception if Exception === app_exception
end