rack-rewrite 1.4.01 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/History.rdoc +4 -0
- data/README.markdown +51 -4
- data/VERSION +1 -1
- data/lib/rack/rewrite.rb +6 -2
- data/lib/rack/rewrite/rule.rb +13 -1
- data/test/test_helper.rb +5 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9b433927615a0c3d699ee348e2442d5e2d3f3bed
|
4
|
+
data.tar.gz: 8b8f88d6bd02f4149e37a7d3956045fa795a467f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cfd596b1093d843050fe96bf3d6737c821d20a10d3371b17dc23a70c28e78cb55ddbeebe059f8842ad976170426c8cba5da8e215e21cd175165c7794c1dedd23
|
7
|
+
data.tar.gz: 45b9edc5712be185b07cbcbc81dceab6e0053370510c2c381f3ba95f10b72e0aaed2708cfb2cf4cbfe0d4ce6ac4734bd8741f2fd2f1cc1baa8bb53e414a52192
|
data/History.rdoc
CHANGED
data/README.markdown
CHANGED
@@ -14,6 +14,7 @@ can get away with rack-rewrite instead of writing Apache mod_rewrite rules.
|
|
14
14
|
### Sample rackup file
|
15
15
|
|
16
16
|
```ruby
|
17
|
+
# config.ru
|
17
18
|
gem 'rack-rewrite', '~> 1.2.1'
|
18
19
|
require 'rack/rewrite'
|
19
20
|
use Rack::Rewrite do
|
@@ -27,6 +28,7 @@ end
|
|
27
28
|
### Sample usage in a rails app
|
28
29
|
|
29
30
|
```ruby
|
31
|
+
# config/application.rb
|
30
32
|
config.middleware.insert_before(Rack::Lock, Rack::Rewrite) do
|
31
33
|
rewrite '/wiki/John_Trupiano', '/john'
|
32
34
|
r301 '/wiki/Yair_Flicker', '/yair'
|
@@ -35,8 +37,23 @@ config.middleware.insert_before(Rack::Lock, Rack::Rewrite) do
|
|
35
37
|
end
|
36
38
|
```
|
37
39
|
|
38
|
-
|
40
|
+
If you use `config.threadsafe`, you'll need to `insert_before(Rack::Runtime, Rack::Rewrite)` as `Rack::Lock` does
|
41
|
+
not exist when `config.allow_concurrency == true`:
|
39
42
|
|
43
|
+
```ruby
|
44
|
+
config.middleware.insert_before(Rack::Runtime, Rack::Rewrite) do
|
45
|
+
rewrite '/wiki/John_Trupiano', '/john'
|
46
|
+
r301 '/wiki/Yair_Flicker', '/yair'
|
47
|
+
r302 '/wiki/Greg_Jastrab', '/greg'
|
48
|
+
r301 %r{/wiki/(\w+)_\w+}, '/$1'
|
49
|
+
end
|
50
|
+
```
|
51
|
+
|
52
|
+
Or insert Rack::Rewrite to the top of the stack:
|
53
|
+
|
54
|
+
``` ruby
|
55
|
+
config.middleware.insert 0, 'Rack::Rewrite' {}
|
56
|
+
```
|
40
57
|
|
41
58
|
## Redirection codes
|
42
59
|
|
@@ -200,9 +217,9 @@ Recall that rules are interpreted from top to bottom. So you can install
|
|
200
217
|
"default" rewrite rules if you like. [2] is a sample default rule that
|
201
218
|
will redirect all other requests to the wiki to a google search.
|
202
219
|
|
203
|
-
### :send_file, :x_send_file
|
220
|
+
### :send_file, :x_send_file, :send_data
|
204
221
|
|
205
|
-
Calls to #send_file and #x_send_file also have the same signature as #rewrite.
|
222
|
+
Calls to #send_file and #x_send_file and #send_data also have the same signature as #rewrite.
|
206
223
|
If the rule matches, the 'to' parameter is interpreted as a path to a file
|
207
224
|
to be rendered instead of passing the application call up the rack stack.
|
208
225
|
|
@@ -213,6 +230,9 @@ send_file /*/, 'public/spammers.htm', :if => Proc.new { |rack_env|
|
|
213
230
|
x_send_file /^blog\/.*/, 'public/blog_offline.htm', :if => Proc.new { |rack_env|
|
214
231
|
File.exists?('public/blog_offline.htm')
|
215
232
|
}
|
233
|
+
send_data /^blog\/.*/, 'public/blog_offline.png', :if => Proc.new { |rack_env|
|
234
|
+
File.exists?('public/blog_offline.htm')
|
235
|
+
}
|
216
236
|
```
|
217
237
|
|
218
238
|
## Options Parameter
|
@@ -308,7 +328,7 @@ This will not match the relative URL /features but would match /features.xml.
|
|
308
328
|
|
309
329
|
### Keeping your querystring
|
310
330
|
|
311
|
-
When rewriting a URL, you may want to keep your querystring
|
331
|
+
When rewriting a URL, you may want to keep your querystring intact (for
|
312
332
|
example if you're tracking traffic sources). You will need to include a
|
313
333
|
capture group and substitution pattern in your rewrite rule to achieve this.
|
314
334
|
|
@@ -338,6 +358,33 @@ string to /today.html
|
|
338
358
|
r301 lambda { "/#{Time.current.strftime(%m%d%Y)}.html" }, '/today.html'
|
339
359
|
```
|
340
360
|
|
361
|
+
|
362
|
+
##Alternative loaders
|
363
|
+
|
364
|
+
rack-rewrite can also be driven by external loaders. Bundled with this library is a loader for YAML files.
|
365
|
+
|
366
|
+
```
|
367
|
+
config.middleware.insert_before(Rack::Lock, Rack::Rewrite,
|
368
|
+
:klass => Rack::Rewrite::YamlRuleSet,
|
369
|
+
:options => {:file_name => @file_name})
|
370
|
+
```
|
371
|
+
|
372
|
+
Using syntax like
|
373
|
+
|
374
|
+
```
|
375
|
+
-
|
376
|
+
method: r301
|
377
|
+
from: !ruby/regexp '/(.*)/print'
|
378
|
+
to : '$1/printer_friendly'
|
379
|
+
options :
|
380
|
+
host : 'example.com'
|
381
|
+
```
|
382
|
+
|
383
|
+
Any class can be used here as long as:
|
384
|
+
|
385
|
+
- the class take an options hash
|
386
|
+
- `#rules` returns an array of `Rack::Rewrite::Rule` instances
|
387
|
+
|
341
388
|
## Contribute
|
342
389
|
|
343
390
|
rack-rewrite is maintained by [@travisjeffery](http://github.com/travisjeffery).
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.5.0
|
data/lib/rack/rewrite.rb
CHANGED
@@ -5,9 +5,13 @@ module Rack
|
|
5
5
|
# A rack middleware for defining and applying rewrite rules. In many cases you
|
6
6
|
# can get away with rack-rewrite instead of writing Apache mod_rewrite rules.
|
7
7
|
class Rewrite
|
8
|
-
def initialize(app, &rule_block)
|
8
|
+
def initialize(app, given_options = {}, &rule_block)
|
9
|
+
options = {
|
10
|
+
:klass => RuleSet,
|
11
|
+
:options => {}
|
12
|
+
}.merge(given_options)
|
9
13
|
@app = app
|
10
|
-
@rule_set =
|
14
|
+
@rule_set = options[:klass].new(options[:options])
|
11
15
|
@rule_set.instance_eval(&rule_block) if block_given?
|
12
16
|
end
|
13
17
|
|
data/lib/rack/rewrite/rule.rb
CHANGED
@@ -4,7 +4,7 @@ module Rack
|
|
4
4
|
class Rewrite
|
5
5
|
class RuleSet
|
6
6
|
attr_reader :rules
|
7
|
-
def initialize #:nodoc:
|
7
|
+
def initialize(options = {})#:nodoc:
|
8
8
|
@rules = []
|
9
9
|
end
|
10
10
|
|
@@ -90,6 +90,13 @@ module Rack
|
|
90
90
|
add_rule :x_send_file, *args
|
91
91
|
end
|
92
92
|
|
93
|
+
# Creates a rule taht will render the raw data if matched
|
94
|
+
# send_data /*/, 'public/system/maintenance.html',
|
95
|
+
# :if => Proc.new { File.exists?('public/system/maintenance.html') }
|
96
|
+
def send_data(*args)
|
97
|
+
add_rule :send_data, *args
|
98
|
+
end
|
99
|
+
|
93
100
|
private
|
94
101
|
def add_rule(method, from, to, options = {}) #:nodoc:
|
95
102
|
@rules << Rule.new(method.to_sym, from, to, options)
|
@@ -160,6 +167,11 @@ module Rack
|
|
160
167
|
'Content-Length' => ::File.size(interpreted_to).to_s,
|
161
168
|
'Content-Type' => Rack::Mime.mime_type(::File.extname(interpreted_to))
|
162
169
|
}.merge!(additional_headers), []]
|
170
|
+
when :send_data
|
171
|
+
[status, {
|
172
|
+
'Content-Type' => interpreted_to.bytesize,
|
173
|
+
'Content-Type' => 'text/html',
|
174
|
+
}.merge!(additional_headers), [interpreted_to]]
|
163
175
|
else
|
164
176
|
raise Exception.new("Unsupported rule: #{self.rule_type}")
|
165
177
|
end
|
data/test/test_helper.rb
CHANGED
@@ -8,6 +8,11 @@ require 'test/unit'
|
|
8
8
|
class Test::Unit::TestCase
|
9
9
|
end
|
10
10
|
|
11
|
+
def rack_env_for(url, options = {})
|
12
|
+
components = url.split('?')
|
13
|
+
{'PATH_INFO' => components[0], 'QUERY_STRING' => components[1] || ''}.merge(options)
|
14
|
+
end
|
15
|
+
|
11
16
|
def supported_status_codes
|
12
17
|
[:r301, :r302, :r303, :r307]
|
13
18
|
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.
|
4
|
+
version: 1.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Travis Jeffery
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2014-01-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|