Class: Rack::MagicIncomingUrl

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/magic-incoming-url.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, map) ⇒ MagicIncomingUrl

Returns a new instance of MagicIncomingUrl.



23
24
25
# File 'lib/rack/magic-incoming-url.rb', line 23

def initialize(app, map)
  @app, @map = app, map
end

Instance Method Details

#call(env) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rack/magic-incoming-url.rb', line 39

def call env
  redirect = redirect_for_env(env)

  if redirect
    # Only continue if no referer or non-local refererer
    if env['HTTP_REFERER'].nil? or env['HTTP_REFERER'].index(env['HTTP_HOST']).nil?
      if redirect[0..0] == '/'
        redirect = "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}#{redirect}"
      end
      res = Rack::Response.new
      res.redirect(redirect)

      return res.finish
    end
  end

  @app.call(env)
end

#redirect_for_env(env) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rack/magic-incoming-url.rb', line 27

def redirect_for_env(env)
  return nil unless @map

  host, path, server = env['HTTP_HOST'], env['PATH_INFO'], env['SERVER_NAME']

  settings = @map[host] || @map[server]

  return unless settings

  settings[path]
end