class Falcon::Middleware::Redirect
A HTTP middleware for redirecting a given set of hosts to a different endpoint. Typically used for implementing HTTP -> HTTPS redirects.
Public Class Methods
new(app, hosts, endpoint)
click to toggle source
Initialize the redirect middleware. @parameter app [Protocol::HTTP::Middleware] The middleware to wrap. @parameter hosts [Hash(String, Service::Proxy
)] The map of hosts. @parameter endpoint [Endpoint] The template endpoint to use to build the redirect location.
Calls superclass method
# File lib/falcon/middleware/redirect.rb, line 44 def initialize(app, hosts, endpoint) super(app) @hosts = hosts @endpoint = endpoint end
Public Instance Methods
call(request)
click to toggle source
Redirect
the request if the authority matches a specific host. @parameter request [Protocol::HTTP::Request]
Calls superclass method
# File lib/falcon/middleware/redirect.rb, line 62 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 Protocol::HTTP::Response[301, [['location', location]], []] else super end end
lookup(request)
click to toggle source
Lookup the appropriate host for the given request. @parameter request [Protocol::HTTP::Request]
# File lib/falcon/middleware/redirect.rb, line 53 def lookup(request) # Trailing dot and port is ignored/normalized. if authority = request.authority&.sub(/(\.)?(:\d+)?$/, '') return @hosts[authority] end end