Rack::Attack

Constants

VERSION

Attributes

blacklisted_response[RW]
notifier[RW]
throttled_response[RW]

Public Class Methods

blacklist(name, &block) click to toggle source
# File lib/rack/attack.rb, line 20
def blacklist(name, &block)
  self.blacklists[name] = Blacklist.new(name, block)
end
blacklisted?(req) click to toggle source
# File lib/rack/attack.rb, line 72
def blacklisted?(req)
  blacklists.any? do |name, blacklist|
    blacklist[req]
  end
end
blacklists() click to toggle source
# File lib/rack/attack.rb, line 33
def blacklists; @blacklists ||= {}; end
cache() click to toggle source
# File lib/rack/attack.rb, line 94
def cache
  @cache ||= Cache.new
end
call(env) click to toggle source
# File lib/rack/attack.rb, line 51
def call(env)
  req = Rack::Request.new(env)

  if whitelisted?(req)
    @app.call(env)
  elsif blacklisted?(req)
    blacklisted_response[env]
  elsif throttled?(req)
    throttled_response[env]
  else
    tracked?(req)
    @app.call(env)
  end
end
clear!() click to toggle source
# File lib/rack/attack.rb, line 98
def clear!
  @whitelists, @blacklists, @throttles = {}, {}, {}
end
instrument(req) click to toggle source
# File lib/rack/attack.rb, line 90
def instrument(req)
  notifier.instrument('rack.attack', req) if notifier
end
new(app) click to toggle source
# File lib/rack/attack.rb, line 37
def new(app)
  @app = app

  # Set defaults
  @notifier ||= ActiveSupport::Notifications if defined?(ActiveSupport::Notifications)
  @blacklisted_response ||= lambda {|env| [401, {}, ["Unauthorized\n"]] }
  @throttled_response   ||= lambda {|env|
    retry_after = env['rack.attack.match_data'][:period] rescue nil
    [429, {'Retry-After' => retry_after.to_s}, ["Retry later\n"]]
  }

  self
end
throttle(name, options, &block) click to toggle source
# File lib/rack/attack.rb, line 24
def throttle(name, options, &block)
  self.throttles[name] = Throttle.new(name, options, block)
end
throttled?(req) click to toggle source
# File lib/rack/attack.rb, line 78
def throttled?(req)
  throttles.any? do |name, throttle|
    throttle[req]
  end
end
throttles() click to toggle source
# File lib/rack/attack.rb, line 34
def throttles;  @throttles  ||= {}; end
track(name, &block) click to toggle source
# File lib/rack/attack.rb, line 28
def track(name, &block)
  self.tracks[name] = Track.new(name, block)
end
tracked?(req) click to toggle source
# File lib/rack/attack.rb, line 84
def tracked?(req)
  tracks.each_value do |tracker|
    tracker[req]
  end
end
tracks() click to toggle source
# File lib/rack/attack.rb, line 35
def tracks;     @tracks     ||= {}; end
whitelist(name, &block) click to toggle source
# File lib/rack/attack.rb, line 16
def whitelist(name, &block)
  self.whitelists[name] = Whitelist.new(name, block)
end
whitelisted?(req) click to toggle source
# File lib/rack/attack.rb, line 66
def whitelisted?(req)
  whitelists.any? do |name, whitelist|
    whitelist[req]
  end
end
whitelists() click to toggle source
# File lib/rack/attack.rb, line 32
def whitelists; @whitelists ||= {}; end

[Validate]

Generated with the Darkfish Rdoc Generator 2.