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, endpoint) ⇒ Redirection



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

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

Instance Method Details

#call(request) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/falcon/redirection.rb', line 49

def call(request)
  if host = lookup(request)
    if @endpoint.default_port?
      location = "#{@endpoint.scheme}://#{host.authority}#{request.path}"
    else
      location = "#{@endpoint.scheme}://#{host.authority}:#{@endpoint.port}#{request.path}"
    end
    
    return Async::HTTP::Response[301, [['location', location]], []]
  else
    super
  end
end

#lookup(request) ⇒ Object



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

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