Class: Falcon::Redirection

Inherits:
Async::HTTP::Middleware
  • Object
show all
Defined in:
lib/falcon/redirection.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, hosts) ⇒ Redirection

Returns a new instance of Redirection.



35
36
37
38
39
# File 'lib/falcon/redirection.rb', line 35

def initialize(app, hosts)
  super(app)
  
  @hosts = hosts
end

Instance Method Details

#call(request) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/falcon/redirection.rb', line 48

def call(request)
  if endpoint = lookup(request)
    location = "https://#{request.authority}#{request.path}"
    
    return Async::HTTP::Response[301, {'location' => location}, []]
  else
    super
  end
end

#lookup(request) ⇒ Object



41
42
43
44
45
46
# File 'lib/falcon/redirection.rb', line 41

def lookup(request)
  # Trailing dot and port is ignored/normalized.
  if authority = request.authority.sub(/(\.)?(:\d+)?$/, '')
    return @hosts[authority]
  end
end