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
-
#from ⇒ Object
readonly
:nodoc:.
-
#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.
-
#initialize(rule_type, from, to, options = {}) ⇒ Rule
constructor
:nodoc:.
-
#matches?(rack_env) ⇒ Boolean
:nodoc:.
Constructor Details
#initialize(rule_type, from, to, options = {}) ⇒ Rule
:nodoc:
103 104 105 |
# File 'lib/rack/rewrite/rule.rb', line 103 def initialize(rule_type, from, to, ={}) #:nodoc: @rule_type, @from, @to, @options = rule_type, from, to, () end |
Instance Attribute Details
#from ⇒ Object (readonly)
:nodoc:
102 103 104 |
# File 'lib/rack/rewrite/rule.rb', line 102 def from @from end |
#options ⇒ Object (readonly)
:nodoc:
102 103 104 |
# File 'lib/rack/rewrite/rule.rb', line 102 def @options end |
#rule_type ⇒ Object (readonly)
:nodoc:
102 103 104 |
# File 'lib/rack/rewrite/rule.rb', line 102 def rule_type @rule_type end |
#to ⇒ Object (readonly)
:nodoc:
102 103 104 |
# File 'lib/rack/rewrite/rule.rb', line 102 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
116 117 118 119 120 121 122 123 124 125 126 127 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 |
# File 'lib/rack/rewrite/rule.rb', line 116 def apply!(env) #:nodoc: interpreted_to = self.interpret_to(env) additional_headers = {} if @options[:headers] if @options[:headers].respond_to?(:call) additional_headers = @options[:headers].call(*@matches) || {} else additional_headers = @options[:headers] || {} end end status = @options[: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), []] else raise Exception.new("Unsupported rule: #{self.rule_type}") end end |
#matches?(rack_env) ⇒ Boolean
:nodoc:
107 108 109 110 111 112 |
# File 'lib/rack/rewrite/rule.rb', line 107 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 |