Class: Utopia::Redirection::ClientRedirect

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/redirection.rb

Overview

A basic client-side redirect.

Direct Known Subclasses

DirectoryIndex, Moved, Rewrite

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, status: 307, max_age: DEFAULT_MAX_AGE) ⇒ ClientRedirect

Returns a new instance of ClientRedirect.



69
70
71
72
73
# File 'lib/utopia/redirection.rb', line 69

def initialize(app, status: 307, max_age: DEFAULT_MAX_AGE)
	@app = app
	@status = status
	@max_age = max_age
end

Instance Attribute Details

#max_ageObject (readonly)

Returns the value of attribute max_age.



85
86
87
# File 'lib/utopia/redirection.rb', line 85

def max_age
  @max_age
end

#statusObject (readonly)

Returns the value of attribute status.



84
85
86
# File 'lib/utopia/redirection.rb', line 84

def status
  @status
end

Instance Method Details

#[](path) ⇒ Object



103
104
105
# File 'lib/utopia/redirection.rb', line 103

def [] path
	false
end

#cache_controlObject



87
88
89
90
# File 'lib/utopia/redirection.rb', line 87

def cache_control
	# http://jacquesmattheij.com/301-redirects-a-dangerous-one-way-street
	"max-age=#{self.max_age}"
end

#call(env) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/utopia/redirection.rb', line 107

def call(env)
	# Normalize the path to remove redundant slashes, `.` and `..` segments.
	# This prevents protocol-relative redirect URLs (e.g. //evil.com/index)
	# from being generated when PATH_INFO contains a double leading slash.
	path = Path.create(env[Rack::PATH_INFO]).simplify.to_s
	
	if redirection = self[path]
		return redirection
	end
	
	return @app.call(env)
end

#freezeObject



75
76
77
78
79
80
81
82
# File 'lib/utopia/redirection.rb', line 75

def freeze
	return self if frozen?
	
	@status.freeze
	@max_age.freeze
	
	super
end

#make_headers(location) ⇒ Object



92
93
94
95
96
97
# File 'lib/utopia/redirection.rb', line 92

def make_headers(location)
	{
		HTTP::LOCATION => location,
		HTTP::CACHE_CONTROL => self.cache_control
	}
end

#redirect(location) ⇒ Object



99
100
101
# File 'lib/utopia/redirection.rb', line 99

def redirect(location)
	return [self.status, self.make_headers(location), []]
end