rack-rewrite 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.rdoc CHANGED
@@ -1,3 +1,7 @@
1
+ === 1.2.0 / 2011-09--20
2
+ * API
3
+ * :headers option to send additional headers with the response
4
+
1
5
  === 1.1.0 / 2011-08-15
2
6
  * API
3
7
  * :host and :method option to match SERVER_NAME and REQUEST_METHOD env params.
data/README.rdoc CHANGED
@@ -6,7 +6,6 @@ can get away with rack-rewrite instead of writing Apache mod_rewrite rules.
6
6
  == References
7
7
 
8
8
  * Source[http://github.com/jtrupiano/rack-rewrite]
9
- * Documentation[http://johntrupiano.rubyforge.org/rack-rewrite]
10
9
  * {Rack::Rewrite for Site Maintenance and Downtime}[http://blog.smartlogicsolutions.com/2009/11/16/rack-rewrite-for-site-maintenance-and-downtime/]
11
10
  * {Rack::Rewrite + Google Analytics Makes Site Transitions Seamless}[http://blog.smartlogicsolutions.com/2009/11/24/rack-rewrite-google-analytics-makes-site-transitions-seamless/]
12
11
 
@@ -14,7 +13,7 @@ can get away with rack-rewrite instead of writing Apache mod_rewrite rules.
14
13
 
15
14
  === Sample rackup file
16
15
 
17
- gem 'rack-rewrite', '~> 1.0.0'
16
+ gem 'rack-rewrite', '~> 1.2.0'
18
17
  require 'rack/rewrite'
19
18
  use Rack::Rewrite do
20
19
  rewrite '/wiki/John_Trupiano', '/john'
@@ -24,7 +23,7 @@ can get away with rack-rewrite instead of writing Apache mod_rewrite rules.
24
23
  end
25
24
 
26
25
  === Sample usage in a rails app
27
- config.gem 'rack-rewrite', '~> 1.0.0'
26
+ config.gem 'rack-rewrite', '~> 1.2.0'
28
27
  require 'rack/rewrite'
29
28
  config.middleware.insert_before(Rack::Lock, Rack::Rewrite) do
30
29
  rewrite '/wiki/John_Trupiano', '/john'
@@ -156,20 +155,12 @@ to be rendered instead of passing the application call up the rack stack.
156
155
  File.exists?('public/blog_offline.htm')
157
156
  }
158
157
 
159
- == Tips
160
-
161
- === Keeping your querystring
162
-
163
- When rewriting a URL, you may want to keep your querystring in tact (for
164
- example if you're tracking traffic sources). You will need to include a
165
- capture group and substitution pattern in your rewrite rule to achieve this.
158
+ == Options Parameter
166
159
 
167
- rewrite %r{/wiki/John_Trupiano(\?.*)?}, '/john$1'
168
-
169
- This rule will store the querystring in a capture group (via '(?.*)' ) and
170
- will substitute the querystring back into the rewritten URL (via $1).
160
+ Each rewrite rule takes an optional options parameter. The following options
161
+ are supported.
171
162
 
172
- === Matching on a host
163
+ === :host
173
164
 
174
165
  Using the :host option you can match requests to a specific hostname.
175
166
 
@@ -177,10 +168,17 @@ Using the :host option you can match requests to a specific hostname.
177
168
 
178
169
  This rule will only match when the hostname is "facerecognizer.com"
179
170
 
180
- === Restrict rule to HTTP methods (GET, POST, PUT, DELETE, etc)
171
+ === :headers
172
+
173
+ Using the :headers option you can set custom response headers e.g. for HTTP
174
+ caching instructions.
181
175
 
182
- In certain scenarios you will want to restrict your rules by the HTTP method
183
- of a given request. rack-rewrite exposes the :method option for this purpose.
176
+ r301 "/features", "/facial_features", :headers => {'Cache-Control' => 'no-cache'}
177
+
178
+ === :method
179
+
180
+ Using the :method option you can restrict the matching of a rule by the HTTP
181
+ method of a given request.
184
182
 
185
183
  # redirect GET's one way
186
184
  r301 "/players", "/current_players", :method => :get
@@ -188,18 +186,40 @@ of a given request. rack-rewrite exposes the :method option for this purpose.
188
186
  # and redirect POST's another way
189
187
  r302 "/players", "/no_longer_available.html?message=No&longer&supported", :method => :post
190
188
 
191
- === Rule Guards
189
+ === :if
192
190
 
193
- All rules support passing guards as Procs/lambdas. Guards simply return
194
- true or false indicating whether the rule declaration is a match. The
195
- following example demonstrates how the presence of a maintenance page
196
- on the filesystem can be utilized to take your site(s) offline.
191
+ Using the :if option you can define arbitrary rule guards. Guards are any
192
+ object responding to #call that return true or false indicating whether the
193
+ rule matches. The following example demonstrates how the presence of a
194
+ maintenance page on the filesystem can be utilized to take your site(s) offline.
197
195
 
198
196
  maintenance_file = File.join(RAILS_ROOT, 'public', 'system', 'maintenance.html')
199
197
  x_send_file /.*/, maintenance_file, :if => Proc.new { |rack_env|
200
198
  File.exists?(maintenance_file)
201
199
  }
202
200
 
201
+ === :not
202
+
203
+ Using the :not option you can negatively match against the path. This can
204
+ be useful when writing a regular expression match is difficult.
205
+
206
+ rewrite %r{^\/features}, '/facial_features', :not => '/features'
207
+
208
+ This will not match the relative URL /features but would match /features.xml.
209
+
210
+ == Tips
211
+
212
+ === Keeping your querystring
213
+
214
+ When rewriting a URL, you may want to keep your querystring in tact (for
215
+ example if you're tracking traffic sources). You will need to include a
216
+ capture group and substitution pattern in your rewrite rule to achieve this.
217
+
218
+ rewrite %r{/wiki/John_Trupiano(\?.*)?}, '/john$1'
219
+
220
+ This rule will store the querystring in a capture group (via '(?.*)' ) and
221
+ will substitute the querystring back into the rewritten URL (via $1).
222
+
203
223
  === Arbitrary Rewriting
204
224
 
205
225
  All rules support passing a Proc as the second argument allowing you to
@@ -212,4 +232,4 @@ received between 12AM and 8AM to an unavailable page.
212
232
 
213
233
  == Copyright
214
234
 
215
- Copyright (c) 2009-2010 John Trupiano. See LICENSE for details.
235
+ Copyright (c) 2009-2011 John Trupiano. See LICENSE for details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.0
1
+ 1.2.0
@@ -81,11 +81,12 @@ module Rack
81
81
  # (b) alter env as necessary and return true
82
82
  def apply!(env) #:nodoc:
83
83
  interpreted_to = self.interpret_to(env)
84
+ additional_headers = @options[:headers] || {}
84
85
  case self.rule_type
85
86
  when :r301
86
- [301, {'Location' => interpreted_to, 'Content-Type' => Rack::Mime.mime_type(::File.extname(interpreted_to))}, [redirect_message(interpreted_to)]]
87
+ [301, {'Location' => interpreted_to, 'Content-Type' => Rack::Mime.mime_type(::File.extname(interpreted_to))}.merge!(additional_headers), [redirect_message(interpreted_to)]]
87
88
  when :r302
88
- [302, {'Location' => interpreted_to, 'Content-Type' => Rack::Mime.mime_type(::File.extname(interpreted_to))}, [redirect_message(interpreted_to)]]
89
+ [302, {'Location' => interpreted_to, 'Content-Type' => Rack::Mime.mime_type(::File.extname(interpreted_to))}.merge!(additional_headers), [redirect_message(interpreted_to)]]
89
90
  when :rewrite
90
91
  # return [200, {}, {:content => env.inspect}]
91
92
  env['REQUEST_URI'] = interpreted_to
@@ -101,13 +102,13 @@ module Rack
101
102
  [200, {
102
103
  'Content-Length' => ::File.size(interpreted_to).to_s,
103
104
  'Content-Type' => Rack::Mime.mime_type(::File.extname(interpreted_to))
104
- }, [::File.read(interpreted_to)]]
105
+ }.merge!(additional_headers), [::File.read(interpreted_to)]]
105
106
  when :x_send_file
106
107
  [200, {
107
108
  'X-Sendfile' => interpreted_to,
108
109
  'Content-Length' => ::File.size(interpreted_to).to_s,
109
110
  'Content-Type' => Rack::Mime.mime_type(::File.extname(interpreted_to))
110
- }, []]
111
+ }.merge!(additional_headers), []]
111
112
  else
112
113
  raise Exception.new("Unsupported rule: #{self.rule_type}")
113
114
  end
data/test/rule_test.rb CHANGED
@@ -86,10 +86,18 @@ class RuleTest < Test::Unit::TestCase
86
86
  end
87
87
  end
88
88
 
89
+ should 'set additional headers for a 301 and 302 request' do
90
+ [:r301, :r302].each do |rule_type|
91
+ rule = Rack::Rewrite::Rule.new(rule_type, %r{/abc}, '/def.css', {:headers => {'Cache-Control' => 'no-cache'}})
92
+ env = {'PATH_INFO' => '/abc'}
93
+ assert_equal 'no-cache', rule.apply!(env)[1]['Cache-Control']
94
+ end
95
+ end
96
+
89
97
  context 'Given an :x_send_file rule that matches' do
90
98
  setup do
91
99
  @file = File.join(TEST_ROOT, 'geminstaller.yml')
92
- @rule = Rack::Rewrite::Rule.new(:x_send_file, /.*/, @file)
100
+ @rule = Rack::Rewrite::Rule.new(:x_send_file, /.*/, @file, :headers => {'Cache-Control' => 'no-cache'})
93
101
  env = {'PATH_INFO' => '/abc'}
94
102
  @response = @rule.apply!(env)
95
103
  end
@@ -110,6 +118,10 @@ class RuleTest < Test::Unit::TestCase
110
118
  assert_equal File.size(@file).to_s, @response[1]['Content-Length']
111
119
  end
112
120
 
121
+ should 'return additional headers' do
122
+ assert_equal 'no-cache', @response[1]['Cache-Control']
123
+ end
124
+
113
125
  should 'return empty content' do
114
126
  assert_equal [], @response[2]
115
127
  end
@@ -118,7 +130,7 @@ class RuleTest < Test::Unit::TestCase
118
130
  context 'Given a :send_file rule that matches' do
119
131
  setup do
120
132
  @file = File.join(TEST_ROOT, 'geminstaller.yml')
121
- @rule = Rack::Rewrite::Rule.new(:send_file, /.*/, @file)
133
+ @rule = Rack::Rewrite::Rule.new(:send_file, /.*/, @file, :headers => {'Cache-Control' => 'no-cache'})
122
134
  env = {'PATH_INFO' => '/abc'}
123
135
  @response = @rule.apply!(env)
124
136
  end
@@ -139,6 +151,10 @@ class RuleTest < Test::Unit::TestCase
139
151
  assert_equal File.size(@file).to_s, @response[1]['Content-Length']
140
152
  end
141
153
 
154
+ should 'return additional headers' do
155
+ assert_equal 'no-cache', @response[1]['Cache-Control']
156
+ end
157
+
142
158
  should 'return the contents of geminstaller.yml in an array for Ruby 1.9.2 compatibility' do
143
159
  assert_equal [File.read(@file)], @response[2]
144
160
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-rewrite
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-15 00:00:00.000000000 -04:00
12
+ date: 2011-09-20 00:00:00.000000000 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler
17
- requirement: &2152850260 !ruby/object:Gem::Requirement
17
+ requirement: &2156177220 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 1.0.10
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *2152850260
25
+ version_requirements: *2156177220
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: shoulda
28
- requirement: &2152849800 !ruby/object:Gem::Requirement
28
+ requirement: &2156176760 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ~>
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: 2.10.2
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *2152849800
36
+ version_requirements: *2156176760
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: mocha
39
- requirement: &2152849340 !ruby/object:Gem::Requirement
39
+ requirement: &2156200620 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ~>
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: 0.9.7
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *2152849340
47
+ version_requirements: *2156200620
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: rack
50
- requirement: &2152848960 !ruby/object:Gem::Requirement
50
+ requirement: &2156200240 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
@@ -55,7 +55,7 @@ dependencies:
55
55
  version: '0'
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *2152848960
58
+ version_requirements: *2156200240
59
59
  description: A rack middleware for enforcing rewrite rules. In many cases you can
60
60
  get away with rack-rewrite instead of writing Apache mod_rewrite rules.
61
61
  email: jtrupiano@gmail.com