Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Whitespace fixes

  • Loading branch information...
commit 0200a43f62da150b07a9ab11bf55472d0d364fd4 1 parent 3e11868
@brynary authored
Showing with 383 additions and 378 deletions.
  1. +8 −8 History.txt
  2. +2 −2 README.md
  3. +6 −1 Rakefile
  4. +1 −1  Thorfile
  5. +1 −1  lib/rack/bug.rb
  6. +2 −2 lib/rack/bug/filtered_backtrace.rb
  7. +4 −4 lib/rack/bug/options.rb
  8. +12 −12 lib/rack/bug/panel.rb
  9. +8 −8 lib/rack/bug/panel_app.rb
  10. +11 −11 lib/rack/bug/panels/active_record_panel.rb
  11. +3 −3 lib/rack/bug/panels/active_record_panel/activerecord_extensions.rb
  12. +1 −1  lib/rack/bug/panels/cache_panel.rb
  13. +4 −4 lib/rack/bug/panels/cache_panel/memcache_extension.rb
  14. +11 −11 lib/rack/bug/panels/cache_panel/panel_app.rb
  15. +20 −20 lib/rack/bug/panels/cache_panel/stats.rb
  16. +10 −10 lib/rack/bug/panels/log_panel.rb
  17. +1 −1  lib/rack/bug/panels/log_panel/rails_extension.rb
  18. +8 −8 lib/rack/bug/panels/memory_panel.rb
  19. +5 −5 lib/rack/bug/panels/rails_info_panel.rb
  20. +1 −1  lib/rack/bug/panels/redis_panel.rb
  21. +2 −2 lib/rack/bug/panels/redis_panel/redis_extension.rb
  22. +12 −12 lib/rack/bug/panels/redis_panel/stats.rb
  23. +7 −7 lib/rack/bug/panels/request_variables_panel.rb
  24. +1 −1  lib/rack/bug/panels/sphinx_panel.rb
  25. +1 −1  lib/rack/bug/panels/sphinx_panel/sphinx_extension.rb
  26. +1 −1  lib/rack/bug/panels/sphinx_panel/stats.rb
  27. +1 −1  lib/rack/bug/panels/sql_panel.rb
  28. +7 −7 lib/rack/bug/panels/sql_panel/panel_app.rb
  29. +12 −12 lib/rack/bug/panels/sql_panel/query.rb
  30. +2 −2 lib/rack/bug/panels/sql_panel/sql_extension.rb
  31. +1 −1  lib/rack/bug/panels/templates_panel.rb
  32. +1 −1  lib/rack/bug/panels/templates_panel/actionview_extension.rb
  33. +14 −14 lib/rack/bug/panels/templates_panel/rendering.rb
  34. +8 −8 lib/rack/bug/panels/templates_panel/trace.rb
  35. +10 −10 lib/rack/bug/panels/timer_panel.rb
  36. +18 −18 lib/rack/bug/params_signature.rb
  37. +16 −16 lib/rack/bug/render.rb
  38. +32 −32 lib/rack/bug/toolbar.rb
  39. +5 −5 spec/fixtures/sample_app.rb
  40. +3 −3 spec/rack/bug/panels/active_record_panel_spec.rb
  41. +24 −24 spec/rack/bug/panels/cache_panel_spec.rb
  42. +3 −3 spec/rack/bug/panels/log_panel_spec.rb
  43. +3 −3 spec/rack/bug/panels/memory_panel_spec.rb
  44. +2 −2 spec/rack/bug/panels/rails_info_panel_spec.rb
  45. +10 −10 spec/rack/bug/panels/redis_panel_spec.rb
  46. +24 −24 spec/rack/bug/panels/sql_panel_spec.rb
  47. +11 −11 spec/rack/bug/panels/templates_panel_spec.rb
  48. +6 −6 spec/rack/bug/panels/timer_panel_spec.rb
  49. +17 −17 spec/rack/toolbar_spec.rb
  50. +10 −10 spec/spec_helper.rb
View
16 History.txt
@@ -1,20 +1,20 @@
== HEAD
* New features
-
+
* Log panel includes log level and timestamp (Tim Connor)
* Sphinx panel (George Chatzigeorgiou)
* Backtraces for Redis panel (Luke Melia & Joey Aghion)
-
+
* Minor fixes
-
+
* Don't "enable" rack bug if you hit cancel on the bookmarklet prompt (Mischa Fierer)
-
+
* Compatibilty
-
+
* backtrace filtering now supports more than just Rails (Alex Chaffee)
* compatibility with current rack-test (Luke Melia & Joey Aghion)
* update Sinatra sample app (Tim Conner)
-
-== 0.2.1
- * The beginning of recorded history
+== 0.2.1
+
+ * The beginning of recorded history
View
4 README.md
@@ -33,7 +33,7 @@ Features
Rails quick start
---------------------------
- script/plugin install git://github.com/brynary/rack-bug.git
+ script/plugin install git://github.com/brynary/rack-bug.git
In config/environments/development.rb, add:
@@ -74,7 +74,7 @@ We have have found that Rack::Bug is fast enough to run in production for specif
### Configuration ####
-Add the middleware configuration to an initializer or the appropriate environment files, taking the rest of this section into consideration.
+Add the middleware configuration to an initializer or the appropriate environment files, taking the rest of this section into consideration.
### Security ####
View
7 Rakefile
@@ -15,4 +15,9 @@ Spec::Rake::SpecTask.new(:rcov) do |t|
end
desc "Run the specs"
-task :default => :spec
+task :default => :spec
+
+desc 'Removes trailing whitespace'
+task :whitespace do
+ sh %{find . -name '*.rb' -exec sed -i '' 's/ *$//g' {} \\;}
+end
View
2  Thorfile
@@ -106,4 +106,4 @@ class Release < Thor
def gem
sh "gem push pkg/#{read_gemspec.file_name}"
end
-end
+end
View
2  lib/rack/bug.rb
@@ -42,4 +42,4 @@ def self.enabled?
def self.new(*args, &block)
Toolbar.new(*args, &block)
end
-end
+end
View
4 lib/rack/bug/filtered_backtrace.rb
@@ -5,7 +5,7 @@ module FilteredBacktrace
def backtrace
@backtrace
end
-
+
def has_backtrace?
filtered_backtrace.any?
end
@@ -16,7 +16,7 @@ def filtered_backtrace
(line.index(root_for_backtrace_filtering) == 0) && !(line.index(root_for_backtrace_filtering("vendor")) == 0)
end
end
-
+
def root_for_backtrace_filtering(sub_path = nil)
if defined?(Rails) && Rails.respond_to?(:root)
sub_path ? Rails.root.join(sub_path) : Rails.root
View
8 lib/rack/bug/options.rb
@@ -9,7 +9,7 @@ def option_accessor(key)
define_method("#{key}?") { || !! read_option(key) }
end
end
-
+
option_accessor :secret_key
option_accessor :ip_masks
option_accessor :password
@@ -45,7 +45,7 @@ def set(option, value=self, &block)
end
private
-
+
def read_option(key)
options[option_name(key)]
end
@@ -61,7 +61,7 @@ def option_name(key)
else raise ArgumentError
end
end
-
+
def initialize_options(options={})
@default_options = {
'rack-bug.ip_masks' => [IPAddr.new("127.0.0.1")],
@@ -84,6 +84,6 @@ def initialize_options(options={})
}
self.options = options
end
-
+
end
end
View
24 lib/rack/bug/panel.rb
@@ -2,14 +2,14 @@
module Rack
module Bug
-
+
# Panels are also Rack middleware
class Panel
include Render
include ERB::Util
-
+
attr_reader :request
-
+
def initialize(app)
if panel_app
@app = Rack::Cascade.new([panel_app, app])
@@ -17,7 +17,7 @@ def initialize(app)
@app = app
end
end
-
+
def call(env)
before(env)
status, headers, body = @app.call(env)
@@ -26,25 +26,25 @@ def call(env)
env["rack-bug.panels"] << self
return [status, headers, body]
end
-
+
def panel_app
nil
end
-
+
def has_content?
true
end
-
+
def before(env)
end
-
+
def after(env, status, headers, body)
end
-
+
def render(template)
end
-
+
end
-
+
end
-end
+end
View
16 lib/rack/bug/panel_app.rb
@@ -1,20 +1,20 @@
module Rack
module Bug
-
+
class PanelApp
include Rack::Bug::Render
-
+
attr_reader :request
-
+
def call(env)
@request = Rack::Request.new(env)
dispatch
end
-
+
def render_template(*args)
Rack::Response.new([super]).to_a
end
-
+
def params
@request.GET
end
@@ -26,8 +26,8 @@ def not_found
def validate_params
ParamsSignature.new(request).validate!
end
-
+
end
-
+
end
-end
+end
View
22 lib/rack/bug/panels/active_record_panel.rb
@@ -2,44 +2,44 @@
module Rack
module Bug
-
+
class ActiveRecordPanel < Panel
-
+
def self.record(class_name)
return unless Rack::Bug.enabled?
records[class_name] += 1
end
-
+
def self.reset
Thread.current["rack.bug.active_records"] = Hash.new { 0 }
end
-
+
def self.records
Thread.current["rack.bug.active_records"] ||= Hash.new { 0 }
end
-
+
def self.total
records.inject(0) do |memo, (key, value)|
memo + value
end
end
-
+
def name
"active_record"
end
-
+
def heading
"#{self.class.total} AR Objects"
end
-
+
def content
records = self.class.records.to_a.sort_by { |key, value| value }.reverse
result = render_template "panels/active_record", :records => records
self.class.reset
result
end
-
+
end
-
+
end
-end
+end
View
6 lib/rack/bug/panels/active_record_panel/activerecord_extensions.rb
@@ -6,13 +6,13 @@ def after_initialize_with_rack_bug
Rack::Bug::ActiveRecordPanel.record(self.class.base_class.name)
after_initialize_without_rack_bug
end
-
+
alias_method_chain :after_initialize, :rack_bug
else
def after_initialize
Rack::Bug::ActiveRecordPanel.record(self.class.base_class.name)
end
end
-
+
end
-end
+end
View
2  lib/rack/bug/panels/cache_panel.rb
@@ -47,4 +47,4 @@ def content
end
end
-end
+end
View
8 lib/rack/bug/panels/cache_panel/memcache_extension.rb
@@ -42,13 +42,13 @@ def prepend_with_rack_bug(key, value)
prepend_without_rack_bug(key, value)
end
end
-
+
def delete_with_rack_bug(key)
Rack::Bug::CachePanel.record(:delete, key) do
delete_without_rack_bug(key)
end
end
-
+
def get_with_rack_bug(keys, marshal=true)
if keys.is_a? Array
Rack::Bug::CachePanel.record(:get_multi, *keys) do
@@ -117,7 +117,7 @@ def delete_with_rack_bug(key, expiry = 0)
delete_without_rack_bug(key, expiry)
end
end
-
+
alias_method_chain :decr, :rack_bug
alias_method_chain :get, :rack_bug
alias_method_chain :get_multi, :rack_bug
@@ -126,4 +126,4 @@ def delete_with_rack_bug(key, expiry = 0)
alias_method_chain :add, :rack_bug
alias_method_chain :delete, :rack_bug
end
-end
+end
View
22 lib/rack/bug/panels/cache_panel/panel_app.rb
@@ -1,9 +1,9 @@
module Rack
module Bug
class CachePanel
-
+
class PanelApp < ::Rack::Bug::PanelApp
-
+
def dispatch
case request.path_info
when "/__rack_bug__/view_cache" then view_cache
@@ -12,37 +12,37 @@ def dispatch
else not_found
end
end
-
+
def ok
Rack::Response.new(["OK"]).to_a
end
-
+
def view_cache
validate_params
render_template "panels/view_cache", :key => params["key"], :value => Rails.cache.read(params["key"])
end
-
+
def delete_cache
validate_params
raise "Rails not found... can't delete key" unless defined?(Rails)
Rails.cache.delete(params["key"])
ok
end
-
+
def delete_cache_list
validate_params
raise "Rails not found... can't delete key" unless defined?(Rails)
-
+
params.each do |key, value|
next unless key =~ /^keys_/
Rails.cache.delete(value)
end
-
+
ok
end
-
+
end
-
+
end
end
-end
+end
View
40 lib/rack/bug/panels/cache_panel/stats.rb
@@ -1,22 +1,22 @@
module Rack
module Bug
class CachePanel
-
+
class Stats
class Query
attr_reader :method, :time, :hit, :keys
-
+
def initialize(method, time, hit, keys)
@method = method
@time = time
@hit = hit
@keys = keys
end
-
+
def display_time
"%.2fms" % time
end
-
+
def display_keys
if keys.size == 1
keys.first
@@ -25,64 +25,64 @@ def display_keys
end
end
end
-
+
attr_reader :calls
attr_reader :keys
attr_reader :queries
-
+
def initialize
@queries = []
- @misses =
+ @misses =
@calls = 0
@time = 0.0
@keys = []
end
-
+
def record_call(method, time, hit, *keys)
@queries << Query.new(method, time, hit, keys)
@calls += 1
@time += time
@keys += keys
end
-
+
def display_time
"%.2fms" % time
end
-
+
def time
@queries.inject(0) do |memo, query|
memo + query.time
end
end
-
+
def gets
count_queries(:get)
end
-
+
def sets
count_queries(:set)
end
-
+
def deletes
count_queries(:delete)
end
-
+
def get_multis
count_queries(:get_multi)
end
-
+
def hits
@queries.select { |q| [:get, :get_multi].include?(q.method) && q.hit }.size
end
-
+
def misses
@queries.select { |q| [:get, :get_multi].include?(q.method) && !q.hit }.size
end
-
+
def count_queries(method)
@queries.select { |q| q.method == method }.size
end
-
+
def queries_to_param
params = {}
@queries.each_with_index do |query, index|
@@ -91,7 +91,7 @@ def queries_to_param
params
end
end
-
+
end
end
-end
+end
View
20 lib/rack/bug/panels/log_panel.rb
@@ -2,23 +2,23 @@
module Rack
module Bug
-
+
class LogPanel < Panel
class LogEntry
attr_reader :level, :time, :message
LEVELS = ['DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL']
-
+
def initialize(level, time, message)
@level = LEVELS[level]
@time = time
@message = message
end
-
+
def cleaned_message
@message.to_s.gsub(/\e\[[;\d]+m/, "")
end
end
-
+
def self.record(message, log_level)
return unless Rack::Bug.enabled?
return unless message
@@ -26,20 +26,20 @@ def self.record(message, log_level)
timestamp = ((Time.now - Thread.current["rack.bug.logs.start"]) * 1000).to_i
logs << LogEntry.new(log_level, timestamp, message)
end
-
+
def self.reset
Thread.current["rack.bug.logs"] = []
Thread.current["rack.bug.logs.start"] = nil
end
-
+
def self.logs
Thread.current["rack.bug.logs"] ||= []
end
-
+
def name
"log"
end
-
+
def heading
"Log"
end
@@ -49,8 +49,8 @@ def content
self.class.reset
return result
end
-
+
end
-
+
end
end
View
2  lib/rack/bug/panels/log_panel/rails_extension.rb
@@ -8,4 +8,4 @@ def add(*args, &block)
end
Rails.logger.extend LoggingExtensions
-end
+end
View
16 lib/rack/bug/panels/memory_panel.rb
@@ -1,18 +1,18 @@
-#
+#
module Rack
module Bug
-
+
class MemoryPanel < Panel
-
+
def before(env)
@original_memory = `ps -o rss= -p #{$$}`.to_i
end
-
+
def after(env, status, headers, body)
@total_memory = `ps -o rss= -p #{$$}`.to_i
@memory_increase = @total_memory - @original_memory
end
-
+
def heading
"#{@memory_increase} KB &#916;, #{@total_memory} KB total"
end
@@ -20,8 +20,8 @@ def heading
def has_content?
false
end
-
+
end
-
+
end
-end
+end
View
10 lib/rack/bug/panels/rails_info_panel.rb
@@ -1,12 +1,12 @@
module Rack
module Bug
-
+
class RailsInfoPanel < Panel
-
+
def name
"rails_info"
end
-
+
def heading
return unless (defined?(Rails) && defined?(Rails::Info))
"Rails #{Rails.version}"
@@ -16,8 +16,8 @@ def content
return unless (defined?(Rails) && defined?(Rails::Info))
render_template "panels/rails_info"
end
-
+
end
-
+
end
end
View
2  lib/rack/bug/panels/redis_panel.rb
@@ -41,4 +41,4 @@ def content
end
end
-end
+end
View
4 lib/rack/bug/panels/redis_panel/redis_extension.rb
@@ -8,7 +8,7 @@ def call_command_with_rack_bug(argv)
call_command_without_rack_bug(argv)
end
end
-
+
alias_method_chain :call_command, :rack_bug
end
-end
+end
View
24 lib/rack/bug/panels/redis_panel/stats.rb
@@ -1,52 +1,52 @@
module Rack
module Bug
class RedisPanel
-
+
class Stats
class Query
include Rack::Bug::FilteredBacktrace
-
+
attr_reader :time
attr_reader :command
-
+
def initialize(time, command_args, backtrace)
@time = time
@command = command_args.inspect
@backtrace = backtrace
end
-
+
def display_time
"%.2fms" % time
end
end
-
+
attr_reader :calls
attr_reader :queries
-
+
def initialize
@queries = []
@calls = 0
@time = 0.0
end
-
+
def record_call(time, command_args, backtrace)
@queries << Query.new(time, command_args, backtrace)
@calls += 1
@time += time
end
-
+
def display_time
"%.2fms" % time
end
-
+
def time
@queries.inject(0) do |memo, query|
memo + query.time
end
end
-
+
end
-
+
end
end
-end
+end
View
14 lib/rack/bug/panels/request_variables_panel.rb
@@ -1,25 +1,25 @@
module Rack
module Bug
-
+
class RequestVariablesPanel < Panel
-
+
def name
"request_variables"
end
-
+
def before(env)
@env = env
end
-
+
def heading
"Rack Env"
end
-
+
def content
render_template "panels/request_variables", :request => @request, :env => @env
end
end
-
+
end
-end
+end
View
2  lib/rack/bug/panels/sphinx_panel.rb
@@ -8,7 +8,7 @@ class SphinxPanel < Panel
def self.record(*sphinx_command_args, &block)
return block.call unless Rack::Bug.enabled?
-
+
start_time = Time.now
result = block.call
total_time = Time.now - start_time
View
2  lib/rack/bug/panels/sphinx_panel/sphinx_extension.rb
@@ -7,7 +7,7 @@ def request_with_rack_bug(command, messages)
request_without_rack_bug(command, messages)
end
end
-
+
alias_method_chain :request, :rack_bug
end
end
View
2  lib/rack/bug/panels/sphinx_panel/stats.rb
@@ -93,4 +93,4 @@ def time
end
end
-end
+end
View
2  lib/rack/bug/panels/sql_panel.rb
@@ -52,4 +52,4 @@ def content
end
end
-end
+end
View
14 lib/rack/bug/panels/sql_panel/panel_app.rb
@@ -1,9 +1,9 @@
module Rack
module Bug
class SQLPanel
-
+
class PanelApp < ::Rack::Bug::PanelApp
-
+
def dispatch
case request.path_info
when "/__rack_bug__/explain_sql" then explain_sql
@@ -12,26 +12,26 @@ def dispatch
else not_found
end
end
-
+
def explain_sql
validate_params
query = Query.new(params["query"], params["time"].to_f)
render_template "panels/explain_sql", :result => query.explain, :query => query.sql, :time => query.time
end
-
+
def profile_sql
validate_params
query = Query.new(params["query"], params["time"].to_f)
render_template "panels/profile_sql", :result => query.profile, :query => query.sql, :time => query.time
end
-
+
def execute_sql
validate_params
query = Query.new(params["query"], params["time"].to_f)
render_template "panels/execute_sql", :result => query.execute, :query => query.sql, :time => query.time
end
-
+
end
end
end
-end
+end
View
24 lib/rack/bug/panels/sql_panel/query.rb
@@ -1,19 +1,19 @@
module Rack
module Bug
class SQLPanel
-
+
class Query
include Rack::Bug::FilteredBacktrace
-
+
attr_reader :sql
attr_reader :time
-
+
def initialize(sql, time, backtrace = [])
@sql = sql
@time = time
@backtrace = backtrace
end
-
+
def human_time
"%.2fms" % (@time * 1_000)
end
@@ -21,18 +21,18 @@ def human_time
def inspectable?
sql.strip =~ /^SELECT /i
end
-
+
def with_profiling
self.class.execute("SET PROFILING=1")
result = yield
self.class.execute("SET PROFILING=0")
return result
end
-
+
def explain
self.class.execute "EXPLAIN #{@sql}"
end
-
+
def profile
with_profiling do
execute
@@ -43,21 +43,21 @@ def profile
SQL
end
end
-
+
def execute
self.class.execute(@sql)
end
-
+
def valid_hash?(secret_key, possible_hash)
hash = Digest::SHA1.hexdigest [secret_key, @sql].join(":")
possible_hash == hash
end
-
+
def self.execute(sql)
ActiveRecord::Base.connection.execute(sql)
end
end
-
+
end
end
-end
+end
View
4 lib/rack/bug/panels/sql_panel/sql_extension.rb
@@ -5,7 +5,7 @@ def log_with_rack_bug(sql, name, &block)
log_without_rack_bug(sql, name, &block)
end
end
-
+
alias_method_chain :log, :rack_bug
end
-end
+end
View
2  lib/rack/bug/panels/templates_panel.rb
@@ -41,4 +41,4 @@ def content
end
end
-end
+end
View
2  lib/rack/bug/panels/templates_panel/actionview_extension.rb
@@ -9,4 +9,4 @@ def render_template_with_rack_bug(*args, &block)
alias_method_chain :render_template, :rack_bug
end
-end
+end
View
28 lib/rack/bug/panels/templates_panel/rendering.rb
@@ -1,37 +1,37 @@
module Rack
module Bug
class TemplatesPanel
-
+
class Rendering
attr_accessor :name
attr_accessor :start_time
attr_accessor :end_time
attr_accessor :parent
attr_reader :children
-
-
+
+
def initialize(name)
@name = name
@children = []
end
-
+
def add(rendering)
@children << rendering
rendering.parent = self
end
-
+
def time
@end_time - @start_time
end
-
+
def exclusive_time
time - child_time
end
-
+
def child_time
children.inject(0.0) { |memo, c| memo + c.time }
end
-
+
def time_summary
if children.any?
"%.2fms, %.2f exclusive" % [time * 1_000, exclusive_time * 1_000]
@@ -43,25 +43,25 @@ def html
<<-HTML
<li>
<p>#{name} (#{time_summary})</p>
-
+
#{children_html}
</li>
HTML
end
-
+
def children_html
return "" unless children.any?
-
+
<<-HTML
<ul>#{joined_children_html}</ul>
HTML
end
-
+
def joined_children_html
children.map { |c| c.html }.join
end
end
-
+
end
end
-end
+end
View
16 lib/rack/bug/panels/templates_panel/trace.rb
@@ -1,34 +1,34 @@
module Rack
module Bug
class TemplatesPanel
-
+
class Trace
-
+
def start(template_name)
rendering = Rendering.new(template_name)
rendering.start_time = Time.now
@current.add(rendering)
@current = rendering
end
-
+
def finished(template_name)
@current.end_time = Time.now
@current = @current.parent
end
-
+
def initialize
@current = root
end
-
+
def total_time
root.child_time
end
-
+
def root
@root ||= Rendering.new("root")
end
end
-
+
end
end
-end
+end
View
20 lib/rack/bug/panels/timer_panel.rb
@@ -2,39 +2,39 @@
module Rack
module Bug
-
+
class TimerPanel < Panel
-
+
def name
"timer"
end
-
+
def call(env)
status, headers, body = nil
@times = Benchmark.measure do
status, headers, body = @app.call(env)
end
-
+
@measurements = [
["User CPU time", "%.2fms" % (@times.utime * 1_000)],
["System CPU time", "%.2fms" % (@times.stime * 1_000)],
["Total CPU time", "%.2fms" % (@times.total * 1_000)],
["Elapsed time", "%.2fms" % (@times.real * 1_000)]
]
-
+
env["rack-bug.panels"] << self
return [status, headers, body]
end
-
+
def heading
"%.2fms" % (@times.real * 1_000)
end
-
+
def content
render_template "panels/timer", :measurements => @measurements
end
-
+
end
-
+
end
-end
+end
View
36 lib/rack/bug/params_signature.rb
@@ -2,64 +2,64 @@
module Rack
module Bug
-
+
class ParamsSignature
extend ERB::Util
-
+
def self.sign(request, hash)
parts = []
-
+
hash.keys.sort.each do |key|
parts << "#{key}=#{u(hash[key])}"
end
-
+
signature = new(request).signature(hash)
parts << "hash=#{u(signature)}"
-
+
parts.join("&amp;")
end
-
+
attr_reader :request
-
+
def initialize(request)
@request = request
end
-
+
def secret_key
@request.env['rack-bug.secret_key']
end
-
+
def secret_key_blank?
secret_key.nil? || secret_key == ""
end
-
+
def validate!
if secret_key_blank?
raise SecurityError.new("Missing secret key")
end
-
+
if secret_key_blank? || request.params["hash"] != signature(request.params)
raise SecurityError.new("Invalid query hash.")
end
end
-
+
def signature(params)
Digest::SHA1.hexdigest(signature_base(params))
end
-
+
def signature_base(params)
signature = []
signature << secret_key
-
+
params.keys.sort.each do |key|
next if key == "hash"
signature << params[key].to_s
end
-
+
signature.join(":")
end
-
+
end
-
+
end
-end
+end
View
32 lib/rack/bug/render.rb
@@ -2,50 +2,50 @@
module Rack
module Bug
-
+
module Render
include ERB::Util
-
+
def signed_params(hash)
ParamsSignature.sign(request, hash)
end
-
+
module CompiledTemplates
end
include CompiledTemplates
-
+
def render_template(filename, local_assigns = {})
compile(filename, local_assigns)
render_symbol = method_name(filename, local_assigns)
send(render_symbol, local_assigns)
end
-
+
def compile(filename, local_assigns)
render_symbol = method_name(filename, local_assigns)
-
+
if !CompiledTemplates.instance_methods.include?(render_symbol.to_s)
compile!(filename, local_assigns)
end
end
-
+
def compile!(filename, local_assigns)
- render_symbol = method_name(filename, local_assigns)
+ render_symbol = method_name(filename, local_assigns)
locals_code = local_assigns.keys.map { |key| "#{key} = local_assigns[:#{key}];" }.join
-
+
source = <<-end_src
def #{render_symbol}(local_assigns)
#{locals_code}
#{compiled_source(filename)}
end
end_src
-
+
CompiledTemplates.module_eval(source, filename, 0)
end
-
+
def compiled_source(filename)
::ERB.new(::File.read(::File.dirname(__FILE__) + "/../bug/views/#{filename}.html.erb"), nil, "-").src
end
-
+
def method_name(filename, local_assigns)
if local_assigns && local_assigns.any?
method_name = method_name_without_locals(filename).dup
@@ -55,12 +55,12 @@ def method_name(filename, local_assigns)
end
method_name.to_sym
end
-
+
def method_name_without_locals(filename)
filename.split("/").join("_")
end
-
+
end
-
+
end
-end
+end
View
64 lib/rack/bug/toolbar.rb
@@ -8,7 +8,7 @@ def initialize(app, static_app)
@app = app
@static_app = static_app
end
-
+
def call(env)
if env["PATH_INFO"]
@static_app.call(env)
@@ -17,27 +17,27 @@ def call(env)
end
end
end
-
+
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))
end
-
+
def public_path
::File.expand_path(::File.dirname(__FILE__) + "/../bug/public")
end
-
+
def call(env)
env.replace @default_options.merge(env)
@env = env
@@ -49,30 +49,30 @@ def call(env)
pass
end
end
-
+
def pass
@app.call(@env)
end
-
+
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
-
+
def intercept_redirect
redirect_to = @response.location
new_body = render_template("redirect", :redirect_to => @response.location)
@@ -80,64 +80,64 @@ def intercept_redirect
new_response["Content-Length"] = new_body.size.to_s
@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" &&
MIME_TYPES.include?(@response.content_type.split(";").first)
end
-
+
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
-
+
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
-
+
def render
render_template("toolbar", :panels => @env["rack-bug.panels"].reverse)
end
-
+
end
-
+
end
end
View
10 spec/fixtures/sample_app.rb
@@ -5,16 +5,16 @@ class SampleApp < Sinatra::Base
get "/redirect" do
redirect "/"
end
-
+
get "/error" do
raise "Error!"
end
-
+
get "/" do
if params[:content_type]
headers["Content-Type"] = params[:content_type]
end
-
+
<<-HTML
<html>
<head>
@@ -26,5 +26,5 @@ class SampleApp < Sinatra::Base
</html>
HTML
end
-
-end
+
+end
View
6 spec/rack/bug/panels/active_record_panel_spec.rb
@@ -6,7 +6,7 @@ module Rack::Bug
ActiveRecordPanel.reset
rack_env "rack-bug.panel_classes", [ActiveRecordPanel]
end
-
+
describe "heading" do
it "displays the total number of instantiated AR objects" do
ActiveRecordPanel.record("User")
@@ -15,7 +15,7 @@ module Rack::Bug
response.should have_heading("2 AR Objects")
end
end
-
+
describe "content" do
it "displays the count of instantiated objects for each class" do
ActiveRecordPanel.record("User")
@@ -27,4 +27,4 @@ module Rack::Bug
end
end
end
-end
+end
View
48 spec/rack/bug/panels/cache_panel_spec.rb
@@ -6,81 +6,81 @@ module Rack::Bug
CachePanel.reset
rack_env "rack-bug.panel_classes", [CachePanel]
end
-
+
describe "heading" do
it "displays the total memcache time" do
response = get_via_rack "/"
response.should have_heading("Cache: 0.00ms")
end
end
-
+
describe "content" do
describe "usage table" do
it "displays the total number of memcache calls" do
CachePanel.record(:get, "user:1") { }
response = get_via_rack "/"
-
+
# This causes a bus error:
# response.should have_selector("th:content('Total Calls') + td", :content => "1")
response.should have_row("#cache_usage", "Total Calls", "1")
end
-
+
it "displays the total memcache time" do
response = get_via_rack "/"
response.should have_row("#cache_usage", "Total Time", "0.00ms")
end
-
+
it "dispays the number of memcache hits" do
CachePanel.record(:get, "user:1") { }
response = get_via_rack "/"
response.should have_row("#cache_usage", "Hits", "0")
end
-
+
it "displays the number of memcache misses" do
CachePanel.record(:get, "user:1") { }
response = get_via_rack "/"
response.should have_row("#cache_usage", "Misses", "1")
end
-
+
it "displays the number of memcache gets" do
CachePanel.record(:get, "user:1") { }
response = get_via_rack "/"
response.should have_row("#cache_usage", "gets", "1")
end
-
+
it "displays the number of memcache sets" do
CachePanel.record(:set, "user:1") { }
response = get_via_rack "/"
response.should have_row("#cache_usage", "sets", "1")
end
-
+
it "displays the number of memcache deletes" do
CachePanel.record(:delete, "user:1") { }
response = get_via_rack "/"
response.should have_row("#cache_usage", "deletes", "1")
end
-
+
it "displays the number of memcache get_multis" do
CachePanel.record(:get_multi, "user:1", "user:2") { }
response = get_via_rack "/"
response.should have_row("#cache_usage", "get_multis", "1")
end
end
-
+
describe "breakdown" do
it "displays each memcache operation" do
CachePanel.record(:get, "user:1") { }
response = get_via_rack "/"
response.should have_row("#cache_breakdown", "get")
end
-
+
it "displays the time for each memcache call" do
CachePanel.record(:get, "user:1") { }
response = get_via_rack "/"
response.should have_row("#cache_breakdown", "user:1", TIME_MS_REGEXP)
end
-
+
it "displays the keys for each memcache call" do
CachePanel.record(:get, "user:1") { }
response = get_via_rack "/"
@@ -88,24 +88,24 @@ module Rack::Bug
end
end
end
-
+
describe "expire_all" do
before do
rack_env "rack-bug.secret_key", 'abc'
end
-
+
it "expires the cache keys" do
Rails.stub!(:cache => mock("cache"))
Rails.cache.should_receive(:delete).with("user:1")
Rails.cache.should_receive(:delete).with("user:2")
Rails.cache.should_receive(:delete).with("user:3")
Rails.cache.should_receive(:delete).with("user:4")
-
+
get_via_rack "/__rack_bug__/delete_cache_list",
:keys_1 => "user:1", :keys_2 => "user:2", :keys_3 => "user:3", :keys_4 => "user:4",
:hash => "c367b76e0199c308862a3afd8fba32b8715e7976"
end
-
+
it "returns OK" do
Rails.stub!(:cache => mock("cache", :delete => nil))
response = get_via_rack "/__rack_bug__/delete_cache_list",
@@ -114,19 +114,19 @@ module Rack::Bug
response.should contain("OK")
end
end
-
+
describe "expire" do
before do
rack_env "rack-bug.secret_key", 'abc'
end
-
+
it "expires the cache key" do
Rails.stub!(:cache => mock("cache"))
Rails.cache.should_receive(:delete).with("user:1")
get_via_rack "/__rack_bug__/delete_cache", :key => "user:1",
:hash => "f87215442d312d8e42cf51e6b66fc3eb3d59ac74"
end
-
+
it "returns OK" do
Rails.stub!(:cache => mock("cache", :delete => nil))
response = get_via_rack "/__rack_bug__/delete_cache", :key => "user:1",
@@ -134,19 +134,19 @@ module Rack::Bug
response.should contain("OK")
end
end
-
+
describe "view_cache" do
before do
rack_env "rack-bug.secret_key", 'abc'
end
-
+
it "renders the cache key" do
Rails.stub!(:cache => mock("cache", :read => "cache body"))
response = get_via_rack "/__rack_bug__/view_cache", :key => "user:1",
:hash => "f87215442d312d8e42cf51e6b66fc3eb3d59ac74"
response.should contain("cache body")
end
-
+
it "renders non-String cache values properly" do
Rails.stub!(:cache => mock("cache", :read => [1, 2]))
response = get_via_rack "/__rack_bug__/view_cache", :key => "user:1",
@@ -156,4 +156,4 @@ module Rack::Bug
end
end
-end
+end
View
6 spec/rack/bug/panels/log_panel_spec.rb
@@ -6,14 +6,14 @@ module Rack::Bug
LogPanel.reset
rack_env "rack-bug.panel_classes", [LogPanel]
end
-
+
describe "heading" do
it "displays 'Log'" do
response = get_via_rack "/"
response.should have_heading("Log")
end
end
-
+
describe "content" do
it "displays recorded log lines" do
LogPanel.record("This is a logged message", 0)
@@ -23,4 +23,4 @@ module Rack::Bug
end
end
end
-end
+end
View
6 spec/rack/bug/panels/memory_panel_spec.rb
@@ -5,17 +5,17 @@ module Rack::Bug
before do
rack_env "rack-bug.panel_classes", [MemoryPanel]
end
-
+
describe "heading" do
it "displays the total memory" do
response = get_via_rack "/"
response.should have_heading(/\d+ KB total/)
end
-
+
it "displays the memory change during the request" do
response = get_via_rack "/"
response.should have_heading(/\d+ KB Δ/)
end
end
end
-end
+end
View
4 spec/rack/bug/panels/rails_info_panel_spec.rb
@@ -5,7 +5,7 @@ module Rack::Bug
before do
rack_env "rack-bug.panel_classes", [RailsInfoPanel]
end
-
+
describe "heading" do
it "displays the Rails version" do
Rails.stub!(:version => "v2.3.0")
@@ -22,4 +22,4 @@ module Rack::Bug
end
end
end
-end
+end
View
20 spec/rack/bug/panels/redis_panel_spec.rb
@@ -7,57 +7,57 @@ module Rack::Bug
RedisPanel.reset
rack_env "rack-bug.panel_classes", [RedisPanel]
end
-
+
describe "heading" do
it "displays the total redis time" do
response = get_via_rack "/"
response.should have_heading("Redis: 0.00ms")
end
end
-
+
describe "content" do
describe "usage table" do
it "displays the total number of redis calls" do
RedisPanel.record(["get, user:1"], Kernel.caller) { }
response = get_via_rack "/"
-
+
# This causes a bus error:
# response.should have_selector("th:content('Total Calls') + td", :content => "1")
response.should have_row("#redis_usage", "Total Calls", "1")
end
-
+
it "displays the total redis time" do
response = get_via_rack "/"
response.should have_row("#redis_usage", "Total Time", "0.00ms")
end
end
-
+
describe "breakdown" do
it "displays each redis operation" do
RedisPanel.record(["get, user:1"], Kernel.caller) { }
response = get_via_rack "/"
response.should have_row("#redis_breakdown", "get")
end
-
+
it "displays the time for redis call" do
RedisPanel.record(["get, user:1"], Kernel.caller) { }
response = get_via_rack "/"
response.should have_row("#redis_breakdown", "user:1", TIME_MS_REGEXP)
end
-
+
it "displays the arguments for each redis call" do
RedisPanel.record(["get, user:1"], Kernel.caller) { }
response = get_via_rack "/"
response.should have_row("#redis_breakdown", "user:1", "get")
end
-
+
it "displays a link to show the backtrace when it's available" do
RedisPanel.record(["get, user:1"], Kernel.caller) { }
response = get_via_rack "/"
response.should have_row("#redis_breakdown", "user:1", "Show Backtrace")
end
-
+
it "does not display a link to show the backtrace when it's not available" do
RedisPanel.record(["get, user:1"], []) { }
response = get_via_rack "/"
@@ -66,4 +66,4 @@ module Rack::Bug
end
end
end
-end
+end
View
48 spec/rack/bug/panels/sql_panel_spec.rb
@@ -6,126 +6,126 @@ module Rack::Bug
SQLPanel.reset
rack_env "rack-bug.panel_classes", [SQLPanel]
end
-
+
describe "heading" do
it "displays the total SQL query count" do
SQLPanel.record("SELECT NOW();") { }
response = get_via_rack "/"
response.should have_heading("1 Queries")
end
-
+
it "displays the total SQL time" do
SQLPanel.record("SELECT NOW();") { }
response = get_via_rack "/"
response.should have_heading(/Queries \(\d+\.\d{2}ms\)/)
end
end
-
+
describe "content" do
it "displays each executed SQL query" do
SQLPanel.record("SELECT NOW();") { }
response = get_via_rack "/"
response.should have_row("#sql", "SELECT NOW();")
end
-
+
it "displays the time of each executed SQL query" do
SQLPanel.record("SELECT NOW();") { }
response = get_via_rack "/"
response.should have_row("#sql", "SELECT NOW();", TIME_MS_REGEXP)
end
end
-
+
def stub_result(results = [[]])
columns = results.first
fields = columns.map { |c| stub("field", :name => c) }
rows = results[1..-1]
-
+
result = stub("result", :fetch_fields => fields)
result.stub!(:each).and_yield(*rows)
return result
end
-
+
def expect_query(sql, results)
conn = stub("connection")
ActiveRecord::Base.stub!(:connection => conn)
conn.should_receive(:execute).with(sql).and_return(stub_result(results))
end
-
+
describe "execute_sql" do
it "displays the query results" do
rack_env "rack-bug.secret_key", "abc"
expect_query "SELECT username FROM users",
[["username"],
["bryan"]]
-
+
response = get_via_rack "/__rack_bug__/execute_sql", :query => "SELECT username FROM users",
:hash => "6f286f55b75716e5c91f16d77d09fa73b353ebc1"
response.should contain("SELECT username FROM users")
response.should be_ok
end
-
+
it "is forbidden when the hash is missing or wrong" do
rack_env "rack-bug.secret_key", 'abc'
-
+
lambda {
get_via_rack "/__rack_bug__/execute_sql", :query => "SELECT username FROM users",
:hash => "foobar"
}.should raise_error(SecurityError)
end
-
+
it "is not available when the rack-bug.secret_key is nil" do
rack_env "rack-bug.secret_key", nil
-
+
lambda {
get_via_rack "/__rack_bug__/execute_sql", :query => "SELECT username FROM users",
:hash => "6f286f55b75716e5c91f16d77d09fa73b353ebc1"
}.should raise_error(SecurityError)
end
-
+
it "is not available when the rack-bug.secret_key is an empty string" do
rack_env "rack-bug.secret_key", ""
-
+
lambda {
get_via_rack "/__rack_bug__/execute_sql", :query => "SELECT username FROM users",
:hash => "6f286f55b75716e5c91f16d77d09fa73b353ebc1"
}.should raise_error(SecurityError)
end
end
-
+
describe "explain_sql" do
it "displays the query explain plan" do
rack_env "rack-bug.secret_key", "abc"
expect_query "EXPLAIN SELECT username FROM users",
[["table"],
["users"]]
-
+
response = get_via_rack "/__rack_bug__/explain_sql", :query => "SELECT username FROM users",
:hash => "6f286f55b75716e5c91f16d77d09fa73b353ebc1"
response.should contain("SELECT username FROM users")
response.should be_ok
end
-
+
it "is forbidden when the hash is missing or wrong" do
rack_env "rack-bug.secret_key", 'abc'
-
+
lambda {
get_via_rack "/__rack_bug__/explain_sql", :query => "SELECT username FROM users",
:hash => "foobar"
}.should raise_error(SecurityError)
end
-
+
it "is not available when the rack-bug.secret_key is nil" do
rack_env "rack-bug.secret_key", nil
-
+
lambda {
get_via_rack "/__rack_bug__/explain_sql", :query => "SELECT username FROM users",
:hash => "6f286f55b75716e5c91f16d77d09fa73b353ebc1"
}.should raise_error(SecurityError)
end
-
+
it "is not available when the rack-bug.secret_key is an empty string" do
rack_env "rack-bug.secret_key", ""
-
+
lambda {
get_via_rack "/__rack_bug__/explain_sql", :query => "SELECT username FROM users",
:hash => "6f286f55b75716e5c91f16d77d09fa73b353ebc1"
@@ -133,4 +133,4 @@ def expect_query(sql, results)
end
end
end
-end
+end
View
22 spec/rack/bug/panels/templates_panel_spec.rb