Class: Rack::IpFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/ip_filter.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, list, path = '/') ⇒ IpFilter

Returns a new instance of IpFilter.



7
8
9
10
11
# File 'lib/rack/ip_filter.rb', line 7

def initialize(app, list, path = '/')
  @app  = app
  @path = path
  @list = list
end

Instance Method Details

#approved?(env) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
# File 'lib/rack/ip_filter.rb', line 21

def approved?(env)
  return true unless path_match?(env['REQUEST_PATH'])
  @list.approved?(remote_address(env))
end

#call(env) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/rack/ip_filter.rb', line 13

def call(env)
  if approved?(env)
    @app.call(env)
  else
    forbidden!
  end
end

#forbidden!Object



34
35
36
# File 'lib/rack/ip_filter.rb', line 34

def forbidden!
  [403, { 'Content-Type' => 'text/html', 'Content-Length' => '0' }, []]
end

#path_match?(path) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/rack/ip_filter.rb', line 38

def path_match?(path)
  path =~ /^#{@path}/
end

#remote_address(env) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/rack/ip_filter.rb', line 26

def remote_address(env)
  if env['HTTP_X_FORWARDED_FOR']
    env['HTTP_X_FORWARDED_FOR'].split(',').first.strip
  else
    env['REMOTE_ADDR']
  end
end