class Redirect
A HTTP middleware for redirecting a given set of hosts to a different endpoint. Typically used for implementing HTTP -> HTTPS redirects.
Nested
Definitions
def initialize(app, hosts, endpoint)
Initialize the redirect middleware.
Signature
-
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.
Implementation
def initialize(app, hosts, endpoint)
super(app)
@hosts = hosts
@endpoint = endpoint
end
def lookup(request)
Lookup the appropriate host for the given request.
Signature
-
parameter
request
Protocol::HTTP::Request
Implementation
def lookup(request)
# Trailing dot and port is ignored/normalized.
if authority = request.authority&.sub(/(\.)?(:\d+)?$/, "")
return @hosts[authority]
end
end
def call(request)
Redirect the request if the authority matches a specific host.
Signature
-
parameter
request
Protocol::HTTP::Request
Implementation
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