rack-rewrite 1.2.1 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +3 -3
- data/README.rdoc +42 -10
- data/Rakefile +2 -2
- data/VERSION +1 -1
- data/lib/rack/rewrite/rule.rb +38 -0
- data/rack-rewrite.gemspec +3 -4
- data/test/rack-rewrite_test.rb +20 -16
- data/test/rule_test.rb +14 -10
- data/test/test_helper.rb +4 -0
- metadata +35 -17
data/Gemfile.lock
CHANGED
@@ -1,20 +1,20 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
rack-rewrite (1.
|
4
|
+
rack-rewrite (1.3.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: http://rubygems.org/
|
8
8
|
specs:
|
9
9
|
mocha (0.9.12)
|
10
|
-
rack (1.
|
10
|
+
rack (1.4.1)
|
11
11
|
shoulda (2.10.3)
|
12
12
|
|
13
13
|
PLATFORMS
|
14
14
|
ruby
|
15
15
|
|
16
16
|
DEPENDENCIES
|
17
|
-
bundler
|
17
|
+
bundler
|
18
18
|
mocha (~> 0.9.7)
|
19
19
|
rack
|
20
20
|
rack-rewrite!
|
data/README.rdoc
CHANGED
@@ -16,21 +16,53 @@ can get away with rack-rewrite instead of writing Apache mod_rewrite rules.
|
|
16
16
|
gem 'rack-rewrite', '~> 1.2.1'
|
17
17
|
require 'rack/rewrite'
|
18
18
|
use Rack::Rewrite do
|
19
|
-
rewrite
|
20
|
-
r301
|
21
|
-
r302
|
22
|
-
r301
|
19
|
+
rewrite '/wiki/John_Trupiano', '/john'
|
20
|
+
r301 '/wiki/Yair_Flicker', '/yair'
|
21
|
+
r302 '/wiki/Greg_Jastrab', '/greg'
|
22
|
+
r301 %r{/wiki/(\w+)_\w+}, '/$1'
|
23
23
|
end
|
24
24
|
|
25
25
|
=== Sample usage in a rails app
|
26
26
|
|
27
27
|
config.middleware.insert_before(Rack::Lock, Rack::Rewrite) do
|
28
|
-
rewrite
|
29
|
-
r301
|
30
|
-
r302
|
31
|
-
r301
|
28
|
+
rewrite '/wiki/John_Trupiano', '/john'
|
29
|
+
r301 '/wiki/Yair_Flicker', '/yair'
|
30
|
+
r302 '/wiki/Greg_Jastrab', '/greg'
|
31
|
+
r301 %r{/wiki/(\w+)_\w+}, '/$1'
|
32
32
|
end
|
33
33
|
|
34
|
+
== Redirection codes
|
35
|
+
|
36
|
+
All +redirect+ status codes from the HTTP spec[http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html] are supported:
|
37
|
+
|
38
|
+
* 301 moved permanently
|
39
|
+
* 302 found
|
40
|
+
* 303 see other
|
41
|
+
* 307 temporary redirect
|
42
|
+
|
43
|
+
These translate to the following methods inside the Rack::Rewrite block:
|
44
|
+
|
45
|
+
r301 '/wiki/John_Trupiano', '/john'
|
46
|
+
moved_permanently '/wiki/John_Trupiano', '/john'
|
47
|
+
p '/wiki/John_Trupiano', '/john' # shortcut alias
|
48
|
+
|
49
|
+
r302 '/wiki/John_Trupiano', '/john'
|
50
|
+
found '/wiki/John_Trupiano', '/john'
|
51
|
+
|
52
|
+
r303 '/wiki/John_Trupiano', '/john'
|
53
|
+
see_other '/wiki/John_Trupiano', '/john'
|
54
|
+
|
55
|
+
r307 '/wiki/John_Trupiano', '/john'
|
56
|
+
temporary_redirect '/wiki/John_Trupiano', '/john'
|
57
|
+
t '/wiki/John_Trupiano', '/john' # shortcut alias
|
58
|
+
|
59
|
+
The 303 and 307 codes were added to the HTTP spec to make unambiguously clear
|
60
|
+
what clients should do with the request method. 303 means that the new request
|
61
|
+
should always be made via GET. 307 means that the new request should use the
|
62
|
+
same method as the original request. Status code 302 was left as it is, since
|
63
|
+
it was already in use by the time these issues came to light. In practice it
|
64
|
+
behaves the same as 303.
|
65
|
+
|
34
66
|
== Use Cases
|
35
67
|
|
36
68
|
=== Rebuild of existing site in a new technology
|
@@ -128,11 +160,11 @@ and rewrite them as the first name only. This is an actual catch-all rule we
|
|
128
160
|
applied when we rebuilt our website in September 2009
|
129
161
|
( http://www.smartlogicsolutions.com ).
|
130
162
|
|
131
|
-
=== :r301, :
|
163
|
+
=== :r301, :r302, :r303, :r307
|
132
164
|
|
133
165
|
Calls to #r301 and #r302 have the same signature as #rewrite. The difference,
|
134
166
|
however, is that these actually short-circuit the rack stack and send back
|
135
|
-
|
167
|
+
their respective status codes. See these examples:
|
136
168
|
|
137
169
|
r301 '/wiki/John_Trupiano', '/john' # [1]
|
138
170
|
r301 '/wiki/(.*)', 'http://www.google.com/?q=$1' # [2]
|
data/Rakefile
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'rake'
|
3
|
-
|
4
3
|
require 'rake/testtask'
|
4
|
+
require 'rdoc/task'
|
5
|
+
|
5
6
|
Rake::TestTask.new(:test) do |test|
|
6
7
|
test.libs << 'lib' << 'test' << '.'
|
7
8
|
test.pattern = 'test/**/*_test.rb'
|
@@ -23,7 +24,6 @@ end
|
|
23
24
|
|
24
25
|
task :default => :test
|
25
26
|
|
26
|
-
require 'rake/rdoctask'
|
27
27
|
Rake::RDocTask.new do |rdoc|
|
28
28
|
if File.exist?('VERSION')
|
29
29
|
version = File.read('VERSION')
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.3.0
|
data/lib/rack/rewrite/rule.rb
CHANGED
@@ -27,18 +27,52 @@ module Rack
|
|
27
27
|
#
|
28
28
|
# r301 '/wiki/John_Trupiano', '/john'
|
29
29
|
# r301 '/contact-us.php', '/contact-us'
|
30
|
+
#
|
31
|
+
# You can use +moved_permanently+ or just +p+ instead of +r301+.
|
30
32
|
def r301(*args)
|
31
33
|
add_rule :r301, *args
|
32
34
|
end
|
33
35
|
|
36
|
+
alias :moved_permanently :r301
|
37
|
+
alias :p :r301
|
38
|
+
|
34
39
|
# Creates a redirect rule that will send a 302 when matching.
|
35
40
|
#
|
36
41
|
# r302 '/wiki/John_Trupiano', '/john'
|
37
42
|
# r302 '/wiki/(.*)', 'http://www.google.com/?q=$1'
|
43
|
+
#
|
44
|
+
# You can use +found+ instead of +r302+.
|
38
45
|
def r302(*args)
|
39
46
|
add_rule :r302, *args
|
40
47
|
end
|
41
48
|
|
49
|
+
alias :found :r302
|
50
|
+
|
51
|
+
# Creates a redirect rule that will send a 303 when matching.
|
52
|
+
#
|
53
|
+
# r303 '/wiki/John_Trupiano', '/john'
|
54
|
+
# r303 '/wiki/(.*)', 'http://www.google.com/?q=$1'
|
55
|
+
#
|
56
|
+
# You can use +see_other+ instead of +r303+.
|
57
|
+
def r303(*args)
|
58
|
+
add_rule :r303, *args
|
59
|
+
end
|
60
|
+
|
61
|
+
alias :see_other :r303
|
62
|
+
|
63
|
+
# Creates a redirect rule that will send a 307 when matching.
|
64
|
+
#
|
65
|
+
# r307 '/wiki/John_Trupiano', '/john'
|
66
|
+
# r307 '/wiki/(.*)', 'http://www.google.com/?q=$1'
|
67
|
+
#
|
68
|
+
# You can use +temporary_redirect+ or +t+ instead of +r307+.
|
69
|
+
def r307(*args)
|
70
|
+
add_rule :r307, *args
|
71
|
+
end
|
72
|
+
|
73
|
+
alias :temporary_redirect :r307
|
74
|
+
alias :t :r307
|
75
|
+
|
42
76
|
# Creates a rule that will render a file if matched.
|
43
77
|
#
|
44
78
|
# send_file /*/, 'public/system/maintenance.html',
|
@@ -87,6 +121,10 @@ module Rack
|
|
87
121
|
[301, {'Location' => interpreted_to, 'Content-Type' => Rack::Mime.mime_type(::File.extname(interpreted_to))}.merge!(additional_headers), [redirect_message(interpreted_to)]]
|
88
122
|
when :r302
|
89
123
|
[302, {'Location' => interpreted_to, 'Content-Type' => Rack::Mime.mime_type(::File.extname(interpreted_to))}.merge!(additional_headers), [redirect_message(interpreted_to)]]
|
124
|
+
when :r303
|
125
|
+
[303, {'Location' => interpreted_to, 'Content-Type' => Rack::Mime.mime_type(::File.extname(interpreted_to))}.merge!(additional_headers), [redirect_message(interpreted_to)]]
|
126
|
+
when :r307
|
127
|
+
[307, {'Location' => interpreted_to, 'Content-Type' => Rack::Mime.mime_type(::File.extname(interpreted_to))}.merge!(additional_headers), [redirect_message(interpreted_to)]]
|
90
128
|
when :rewrite
|
91
129
|
# return [200, {}, {:content => env.inspect}]
|
92
130
|
env['REQUEST_URI'] = interpreted_to
|
data/rack-rewrite.gemspec
CHANGED
@@ -3,10 +3,10 @@ Gem::Specification.new do |s|
|
|
3
3
|
s.version = File.read('VERSION')
|
4
4
|
|
5
5
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
6
|
-
s.authors = ["John Trupiano"]
|
6
|
+
s.authors = ["Travis Jeffery", "John Trupiano"]
|
7
7
|
s.date = Date.today.to_s
|
8
8
|
s.description = %q{A rack middleware for enforcing rewrite rules. In many cases you can get away with rack-rewrite instead of writing Apache mod_rewrite rules.}
|
9
|
-
s.email = %q{
|
9
|
+
s.email = %q{travisjeffery@gmail.com}
|
10
10
|
s.extra_rdoc_files = [
|
11
11
|
"LICENSE",
|
12
12
|
"History.rdoc",
|
@@ -43,9 +43,8 @@ Gem::Specification.new do |s|
|
|
43
43
|
"test/rule_test.rb",
|
44
44
|
"test/test_helper.rb"
|
45
45
|
]
|
46
|
-
#s.add_dependency 'rack'
|
47
46
|
|
48
|
-
s.add_development_dependency 'bundler'
|
47
|
+
s.add_development_dependency 'bundler'
|
49
48
|
s.add_development_dependency 'shoulda', '~> 2.10.2'
|
50
49
|
s.add_development_dependency 'mocha', '~> 0.9.7'
|
51
50
|
s.add_development_dependency 'rack'
|
data/test/rack-rewrite_test.rb
CHANGED
@@ -56,24 +56,28 @@ class RackRewriteTest < Test::Unit::TestCase
|
|
56
56
|
should_not_halt
|
57
57
|
end
|
58
58
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
59
|
+
[301, 302, 303, 307].each do |status|
|
60
|
+
context "when a #{status} rule matches" do
|
61
|
+
setup {
|
62
|
+
@rack = Rack::Rewrite.new(@app) do
|
63
|
+
send("r#{status}", '/wiki/Yair_Flicker', '/yair')
|
64
|
+
end
|
65
|
+
}
|
66
|
+
should_halt
|
67
|
+
should_location_redirect_to('/yair', status)
|
68
|
+
end
|
67
69
|
end
|
68
70
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
71
|
+
[[:p, 301], [:moved_permanently, 301], [:found, 302], [:see_other, 303], [:t, 307], [:temporary_redirect, 307]].each do |rule|
|
72
|
+
context "when a #{rule.first} rule matches" do
|
73
|
+
setup {
|
74
|
+
@rack = Rack::Rewrite.new(@app) do
|
75
|
+
send(rule.first, '/wiki/Yair_Flicker', '/yair')
|
76
|
+
end
|
77
|
+
}
|
78
|
+
should_halt
|
79
|
+
should_location_redirect_to('/yair', rule.last)
|
80
|
+
end
|
77
81
|
end
|
78
82
|
|
79
83
|
context 'when a rewrite rule matches' do
|
data/test/rule_test.rb
CHANGED
@@ -34,16 +34,20 @@ class RuleTest < Test::Unit::TestCase
|
|
34
34
|
end
|
35
35
|
|
36
36
|
context '#Rule#apply' do
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
37
|
+
supported_status_codes.each do |rule_type|
|
38
|
+
should "set Location header to result of #interpret_to for a #{rule_type}" do
|
39
|
+
rule = Rack::Rewrite::Rule.new(rule_type, %r{/abc}, '/def')
|
40
|
+
env = {'PATH_INFO' => '/abc'}
|
41
|
+
assert_equal rule.send(:interpret_to, '/abc'), rule.apply!(env)[1]['Location']
|
42
|
+
end
|
41
43
|
end
|
42
44
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
45
|
+
supported_status_codes.each do |rule_type|
|
46
|
+
should "include a link to the result of #interpret_to for a #{rule_type}" do
|
47
|
+
rule = Rack::Rewrite::Rule.new(rule_type, %r{/abc}, '/def')
|
48
|
+
env = {'PATH_INFO' => '/abc'}
|
49
|
+
assert_match /\/def/, rule.apply!(env)[2][0]
|
50
|
+
end
|
47
51
|
end
|
48
52
|
|
49
53
|
should 'keep the QUERY_STRING when a 301 rule matches a URL with a querystring' do
|
@@ -71,7 +75,7 @@ class RuleTest < Test::Unit::TestCase
|
|
71
75
|
end
|
72
76
|
|
73
77
|
should 'set Content-Type header to text/html for a 301 and 302 request for a .html page' do
|
74
|
-
|
78
|
+
supported_status_codes.each do |rule_type|
|
75
79
|
rule = Rack::Rewrite::Rule.new(rule_type, %r{/abc}, '/def.html')
|
76
80
|
env = {'PATH_INFO' => '/abc'}
|
77
81
|
assert_equal 'text/html', rule.apply!(env)[1]['Content-Type']
|
@@ -79,7 +83,7 @@ class RuleTest < Test::Unit::TestCase
|
|
79
83
|
end
|
80
84
|
|
81
85
|
should 'set Content-Type header to text/css for a 301 and 302 request for a .css page' do
|
82
|
-
|
86
|
+
supported_status_codes.each do |rule_type|
|
83
87
|
rule = Rack::Rewrite::Rule.new(rule_type, %r{/abc}, '/def.css')
|
84
88
|
env = {'PATH_INFO' => '/abc'}
|
85
89
|
assert_equal 'text/css', rule.apply!(env)[1]['Content-Type']
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,31 +1,36 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-rewrite
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
|
+
- Travis Jeffery
|
8
9
|
- John Trupiano
|
9
10
|
autorequire:
|
10
11
|
bindir: bin
|
11
12
|
cert_chain: []
|
12
|
-
date:
|
13
|
-
default_executable:
|
13
|
+
date: 2012-10-13 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: bundler
|
17
|
-
requirement:
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
|
-
- -
|
20
|
+
- - ! '>='
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version:
|
22
|
+
version: '0'
|
23
23
|
type: :development
|
24
24
|
prerelease: false
|
25
|
-
version_requirements:
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ! '>='
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '0'
|
26
31
|
- !ruby/object:Gem::Dependency
|
27
32
|
name: shoulda
|
28
|
-
requirement:
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
29
34
|
none: false
|
30
35
|
requirements:
|
31
36
|
- - ~>
|
@@ -33,10 +38,15 @@ dependencies:
|
|
33
38
|
version: 2.10.2
|
34
39
|
type: :development
|
35
40
|
prerelease: false
|
36
|
-
version_requirements:
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ~>
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 2.10.2
|
37
47
|
- !ruby/object:Gem::Dependency
|
38
48
|
name: mocha
|
39
|
-
requirement:
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
40
50
|
none: false
|
41
51
|
requirements:
|
42
52
|
- - ~>
|
@@ -44,10 +54,15 @@ dependencies:
|
|
44
54
|
version: 0.9.7
|
45
55
|
type: :development
|
46
56
|
prerelease: false
|
47
|
-
version_requirements:
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ~>
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 0.9.7
|
48
63
|
- !ruby/object:Gem::Dependency
|
49
64
|
name: rack
|
50
|
-
requirement:
|
65
|
+
requirement: !ruby/object:Gem::Requirement
|
51
66
|
none: false
|
52
67
|
requirements:
|
53
68
|
- - ! '>='
|
@@ -55,10 +70,15 @@ dependencies:
|
|
55
70
|
version: '0'
|
56
71
|
type: :development
|
57
72
|
prerelease: false
|
58
|
-
version_requirements:
|
73
|
+
version_requirements: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
59
79
|
description: A rack middleware for enforcing rewrite rules. In many cases you can
|
60
80
|
get away with rack-rewrite instead of writing Apache mod_rewrite rules.
|
61
|
-
email:
|
81
|
+
email: travisjeffery@gmail.com
|
62
82
|
executables: []
|
63
83
|
extensions: []
|
64
84
|
extra_rdoc_files:
|
@@ -82,7 +102,6 @@ files:
|
|
82
102
|
- test/rack-rewrite_test.rb
|
83
103
|
- test/rule_test.rb
|
84
104
|
- test/test_helper.rb
|
85
|
-
has_rdoc: true
|
86
105
|
homepage: http://github.com/jtrupiano/rack-rewrite
|
87
106
|
licenses: []
|
88
107
|
post_install_message:
|
@@ -104,13 +123,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
104
123
|
version: '0'
|
105
124
|
requirements: []
|
106
125
|
rubyforge_project: johntrupiano
|
107
|
-
rubygems_version: 1.
|
126
|
+
rubygems_version: 1.8.23
|
108
127
|
signing_key:
|
109
128
|
specification_version: 3
|
110
129
|
summary: A rack middleware for enforcing rewrite rules
|
111
130
|
test_files:
|
112
131
|
- test/rack-rewrite_test.rb
|
113
132
|
- test/geminstaller.yml
|
114
|
-
- test/rack-rewrite_test.rb
|
115
133
|
- test/rule_test.rb
|
116
134
|
- test/test_helper.rb
|