Permalink
Browse files

toolbar should only have toolbar responsibilities

root level stuff belongs in bug itself
  • Loading branch information...
1 parent 31de244 commit 948314fd589bc4b3ee3281a0eb6df61a34c2c492 @knzconnor knzconnor committed with Luke Melia May 30, 2010
Showing with 58 additions and 60 deletions.
  1. +53 −5 lib/rack/bug.rb
  2. +5 −55 lib/rack/bug/toolbar.rb
View
58 lib/rack/bug.rb
@@ -1,10 +1,14 @@
+require "ipaddr"
+require "digest"
require "rack"
require "digest/sha1"
require "rack/bug/autoloading"
class Rack::Bug
+ include Options
+
VERSION = "0.3.0"
-
+
class SecurityError < StandardError
end
@@ -19,13 +23,57 @@ def self.disable
def self.enabled?
Thread.current["rack-bug.enabled"] == true
end
-
- def initialize(*args, &block)
- @toolbar = Toolbar.new(*args, &block)
+
+ def initialize(app, options = {}, &block)
+ @app = asset_server(app)
+ initialize_options options
+ instance_eval(&block) if block_given?
+
+ @toolbar = Toolbar.new(@app)
end
+
def call(env)
- @toolbar.call(env)
+ env.replace @default_options.merge(env)
+ @env = env
+ @original_request = Rack::Request.new(@env)
+
+ if toolbar_requested? && ip_authorized? && password_authorized?
+ @toolbar.call(env)
+ else
+ @app.call(env)
+ end
+ end
+
+private
+
+ def asset_server(app)
+ RackStaticBugAvoider.new(app, Rack::Static.new(app, :urls => ["/__rack_bug__"], :root => public_path))
+ end
+
+ def public_path
+ ::File.expand_path(::File.dirname(__FILE__) + "/bug/public")
+ end
+
+ def toolbar_requested?
+ @original_request.cookies["rack_bug_enabled"]
+ end
+
+ 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
+
+ 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
end
View
60 lib/rack/bug/toolbar.rb
@@ -1,45 +1,16 @@
-require "ipaddr"
-require "digest"
-
module Rack
class Bug
class Toolbar
- include Options
include Render
MIME_TYPES = ["text/html", "application/xhtml+xml"]
- def initialize(app, options = {})
- @app = asset_server(app)
- initialize_options options
- instance_eval(&block) if block_given?
- end
-
- def asset_server(app)
- RackStaticBugAvoider.new(app, Rack::Static.new(app, :urls => ["/__rack_bug__"], :root => public_path))
+ def initialize(app)
+ @app = app
end
-
- def public_path
- ::File.expand_path(::File.dirname(__FILE__) + "/../bug/public")
- end
-
+
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
-
- def pass
- @app.call(@env)
- end
-
- def dispatch
@env["rack-bug.panels"] = []
Rack::Bug.enable
@@ -48,7 +19,7 @@ def dispatch
@response = Rack::Response.new(body, status, headers)
- if @response.redirect? && options["rack-bug.intercept_redirects"]
+ if @response.redirect? && @env["rack-bug.intercept_redirects"]
intercept_redirect
end
if modify?
@@ -66,27 +37,6 @@ def intercept_redirect
@response = new_response
end
- def toolbar_requested?
- @original_request.cookies["rack_bug_enabled"]
- end
-
- 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
-
- 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
-
def modify?
@response.ok? &&
@env["HTTP_X_REQUESTED_WITH"] != "XMLHttpRequest" &&
@@ -96,7 +46,7 @@ def modify?
def builder
builder = Rack::Builder.new
- options["rack-bug.panel_classes"].each do |panel_class|
+ @env["rack-bug.panel_classes"].each do |panel_class|
builder.use panel_class
end

0 comments on commit 948314f

Please sign in to comment.