Class: Rack::Rewrite::Rule
- Inherits:
-
Object
- Object
- Rack::Rewrite::Rule
- Defined in:
- lib/rack/rewrite/rule.rb
Overview
TODO: Break rules into subclasses
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
:nodoc:.
-
#rule_type ⇒ Object
readonly
:nodoc:.
-
#to ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
-
#apply!(env) ⇒ Object
Either (a) return a Rack response (short-circuiting the Rack stack), or (b) alter env as necessary and return true.
- #from ⇒ Object
-
#initialize(rule_type, from, to, options = {}) ⇒ Rule
constructor
:nodoc:.
-
#matches?(rack_env) ⇒ Boolean
:nodoc:.
Constructor Details
#initialize(rule_type, from, to, options = {}) ⇒ Rule
:nodoc:
110 111 112 |
# File 'lib/rack/rewrite/rule.rb', line 110 def initialize(rule_type, from, to, ={}) #:nodoc: @rule_type, @from, @to, = rule_type, from, to, () end |
Instance Attribute Details
#options ⇒ Object (readonly)
:nodoc:
109 110 111 |
# File 'lib/rack/rewrite/rule.rb', line 109 def end |
#rule_type ⇒ Object (readonly)
:nodoc:
109 110 111 |
# File 'lib/rack/rewrite/rule.rb', line 109 def rule_type @rule_type end |
#to ⇒ Object (readonly)
:nodoc:
109 110 111 |
# File 'lib/rack/rewrite/rule.rb', line 109 def to @to end |
Instance Method Details
#apply!(env) ⇒ Object
Either (a) return a Rack response (short-circuiting the Rack stack), or (b) alter env as necessary and return true
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/rack/rewrite/rule.rb', line 128 def apply!(env) #:nodoc: interpreted_to = self.interpret_to(env) additional_headers = {} if [:headers] if [:headers].respond_to?(:call) additional_headers = [:headers].call || {} else additional_headers = [:headers] || {} end end status = [:status] || 200 case self.rule_type when :r301 [301, {'Location' => interpreted_to, 'Content-Type' => Rack::Mime.mime_type(::File.extname(interpreted_to))}.merge!(additional_headers), [(interpreted_to)]] when :r302 [302, {'Location' => interpreted_to, 'Content-Type' => Rack::Mime.mime_type(::File.extname(interpreted_to))}.merge!(additional_headers), [(interpreted_to)]] when :r303 [303, {'Location' => interpreted_to, 'Content-Type' => Rack::Mime.mime_type(::File.extname(interpreted_to))}.merge!(additional_headers), [(interpreted_to)]] when :r307 [307, {'Location' => interpreted_to, 'Content-Type' => Rack::Mime.mime_type(::File.extname(interpreted_to))}.merge!(additional_headers), [(interpreted_to)]] when :rewrite # return [200, {}, {:content => env.inspect}] env['REQUEST_URI'] = interpreted_to if q_index = interpreted_to.index('?') env['PATH_INFO'] = interpreted_to[0..q_index-1] env['QUERY_STRING'] = interpreted_to[q_index+1..interpreted_to.size-1] else env['PATH_INFO'] = interpreted_to env['QUERY_STRING'] = '' end true when :send_file [status, { 'Content-Length' => ::File.size(interpreted_to).to_s, 'Content-Type' => Rack::Mime.mime_type(::File.extname(interpreted_to)) }.merge!(additional_headers), [::File.read(interpreted_to)]] when :x_send_file [status, { 'X-Sendfile' => interpreted_to, 'Content-Length' => ::File.size(interpreted_to).to_s, 'Content-Type' => Rack::Mime.mime_type(::File.extname(interpreted_to)) }.merge!(additional_headers), []] when :send_data [status, { 'Content-Length' => interpreted_to.bytesize, 'Content-Type' => 'text/html', }.merge!(additional_headers), [interpreted_to]] else raise Exception.new("Unsupported rule: #{self.rule_type}") end end |
#from ⇒ Object
121 122 123 124 |
# File 'lib/rack/rewrite/rule.rb', line 121 def from return @static_from if @static_from @from.respond_to?(:call) ? @from.call : @static_from = @from end |
#matches?(rack_env) ⇒ Boolean
:nodoc:
114 115 116 117 118 119 |
# File 'lib/rack/rewrite/rule.rb', line 114 def matches?(rack_env) #:nodoc: return false if [:if].respond_to?(:call) && ![:if].call(rack_env) path = build_path_from_env(rack_env) self.(rack_env) && string_matches?(path, self.from) end |