Class: Rack::Bug::Toolbar

Inherits:
Object
  • Object
show all
Includes:
Options, Render
Defined in:
lib/rack/bug/toolbar.rb

Constant Summary collapse

MIME_TYPES =
["text/html", "application/xhtml+xml"]

Instance Method Summary collapse

Methods included from Render

#compile, #compile!, #compiled_source, #method_name, #method_name_without_locals, #render_template, #signed_params

Methods included from Options

#options, #options=, #set

Constructor Details

#initialize(app, options = {}) ⇒ Toolbar

Returns a new instance of Toolbar.



27
28
29
30
31
# File 'lib/rack/bug/toolbar.rb', line 27

def initialize(app, options = {})
  @app = asset_server(app)
  initialize_options options
  instance_eval(&block) if block_given?
end

Instance Method Details

#asset_server(app) ⇒ Object



33
34
35
# File 'lib/rack/bug/toolbar.rb', line 33

def asset_server(app)
  RackStaticBugAvoider.new(app, Rack::Static.new(app, :urls => ["/__rack_bug__"], :root => public_path))
end

#builderObject



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rack/bug/toolbar.rb', line 111

def builder
  builder = Rack::Builder.new

  options["rack-bug.panel_classes"].each do |panel_class|
    builder.use panel_class
  end

  builder.run @app

  return builder
end

#call(env) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rack/bug/toolbar.rb', line 41

def call(env)
  env.replace @default_options.merge(env)
  @env = env
  @original_request = Request.new(@env)

  if toolbar_requested? && ip_authorized? && password_authorized?
    dispatch
  else
    pass
  end
end

#dispatchObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rack/bug/toolbar.rb', line 57

def dispatch
  @env["rack-bug.panels"] = []

  Rack::Bug.enable
  status, headers, body = builder.call(@env)
  Rack::Bug.disable

  @response = Rack::Response.new(body, status, headers)

  if @response.redirect? && options["rack-bug.intercept_redirects"]
    intercept_redirect
  end
  if modify?
    inject_toolbar
  end

  return @response.to_a
end

#inject_toolbarObject



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rack/bug/toolbar.rb', line 123

def inject_toolbar
  full_body = @response.body.join
  full_body.sub! /<\/body>/, render + "</body>"

  @response["Content-Length"] = full_body.size.to_s

  # Ensure that browser does
  @response["Etag"] = ""
  @response["Cache-Control"] = "no-cache"

  @response.body = [full_body]
end

#intercept_redirectObject



76
77
78
79
80
81
82
# File 'lib/rack/bug/toolbar.rb', line 76

def intercept_redirect
  redirect_to = @response.location
  new_body = render_template("redirect", :redirect_to => @response.location)
  new_response = Rack::Response.new(new_body, 200, { "Content-Type" => "text/html" })
  new_response["Content-Length"] = new_body.size.to_s
  @response = new_response
end

#ip_authorized?Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
# File 'lib/rack/bug/toolbar.rb', line 88

def ip_authorized?
  return true unless options["rack-bug.ip_masks"]

  options["rack-bug.ip_masks"].any? do |ip_mask|
    ip_mask.include?(IPAddr.new(@original_request.ip))
  end
end

#modify?Boolean

Returns:

  • (Boolean)


105
106
107
108
109
# File 'lib/rack/bug/toolbar.rb', line 105

def modify?
  @response.ok? &&
  @env["HTTP_X_REQUESTED_WITH"] != "XMLHttpRequest" &&
  MIME_TYPES.include?(@response.content_type.split(";").first)
end

#passObject



53
54
55
# File 'lib/rack/bug/toolbar.rb', line 53

def pass
  @app.call(@env)
end

#password_authorized?Boolean

Returns:

  • (Boolean)


96
97
98
99
100
101
102
103
# File 'lib/rack/bug/toolbar.rb', line 96

def password_authorized?
  return true unless options["rack-bug.password"]

  expected_sha = Digest::SHA1.hexdigest ["rack_bug", options["rack-bug.password"]].join(":")
  actual_sha = @original_request.cookies["rack_bug_password"]

  actual_sha == expected_sha
end

#public_pathObject



37
38
39
# File 'lib/rack/bug/toolbar.rb', line 37

def public_path
  ::File.expand_path(::File.dirname(__FILE__) + "/../bug/public")
end

#renderObject



136
137
138
# File 'lib/rack/bug/toolbar.rb', line 136

def render
  render_template("toolbar", :panels => @env["rack-bug.panels"].reverse)
end

#toolbar_requested?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/rack/bug/toolbar.rb', line 84

def toolbar_requested?
  @original_request.cookies["rack_bug_enabled"]
end