class ClientRedirect < Protocol::HTTP::Middleware
- Inherits from
Protocol::HTTP::Middleware
A basic client-side redirect.
Definitions
def initialize(app, status: 307, max_age: MAX_AGE)
Initialize client-side redirection behavior.
Signature
-
parameter
appInterface(:call) The downstream application.
-
parameter
statusInteger The status.
-
parameter
max_ageInteger The maximum cache age in seconds.
Implementation
def initialize(app, status: 307, max_age: MAX_AGE)
super(app)
@status = status
@max_age = max_age
end
def freeze
Freeze this object and its internal state.
Signature
-
returns
self This object.
Implementation
def freeze
return self if frozen?
@status.freeze
@max_age.freeze
super
end
def cache_control
Build the cache control header value.
Signature
-
returns
String The cache-control value.
Implementation
def cache_control
# http://jacquesmattheij.com/301-redirects-a-dangerous-one-way-street
"max-age=#{self.max_age}"
end
def make_headers(location)
Build headers for a client redirect.
Signature
-
parameter
locationString The redirect location.
-
returns
Hash(String, String) The redirect headers.
Implementation
def make_headers(location)
{
HTTP::LOCATION => location,
HTTP::CACHE_CONTROL => self.cache_control
}
end
def redirect(location)
Build a redirect response for the given location.
Signature
-
parameter
locationString The redirect location.
-
returns
Protocol::HTTP::Response The redirect response.
Implementation
def redirect(location)
return Response[self.status, self.make_headers(location), []]
end
def [](path)
Resolve a normalized request path to a redirect response.
Signature
-
parameter
pathString The normalized request path.
-
returns
Protocol::HTTP::Response | false The redirect response, or
falseby default.
Implementation
def [] path
false
end
def call(request)
Redirect a normalized request path when it matches, otherwise invoke the application.
Signature
-
parameter
requestUtopia::Request The request.
-
returns
Protocol::HTTP::Response The redirect or downstream response.
Implementation
def call(request)
path = request.url.path.encoded
if redirection = self[path]
return redirection
end
return @delegate.call(request)
end