rack-www-enforcer 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +8 -4
- data/lib/rack/www-enforcer.rb +6 -1
- data/rack-www-enforcer.gemspec +1 -1
- data/test/test_www-enforcer.rb +13 -0
- metadata +11 -5
data/README.md
CHANGED
@@ -1,11 +1,15 @@
|
|
1
|
-
Rack::
|
1
|
+
Rack::WwwEnforcer
|
2
2
|
=================
|
3
3
|
|
4
|
-
Rack Middleware for redirect requests to an specific subdomain of your app. (Ex. http://mysite.com to
|
4
|
+
Rack Middleware for redirect requests to an specific subdomain of your app. (Ex. http://mysite.com to http://app.mysite.com)
|
5
5
|
|
6
6
|
Usage
|
7
7
|
-------
|
8
8
|
|
9
|
-
use Rack::
|
9
|
+
use Rack::WwwEnforcer, :subdomain => "app"
|
10
10
|
|
11
|
-
If subdomain isn't present "www" is used by default
|
11
|
+
If subdomain isn't present "www" is used by default.
|
12
|
+
|
13
|
+
You can use it in your Rails app, in the environment configuration file add it to Middleware stack:
|
14
|
+
|
15
|
+
config.middleware.insert_before ActionDispatch::Static, Rack::WwwEnforcer
|
data/lib/rack/www-enforcer.rb
CHANGED
@@ -7,6 +7,7 @@ module Rack
|
|
7
7
|
def initialize(app, options = {})
|
8
8
|
@app = app
|
9
9
|
@subdomain = options[:subdomain] || "www"
|
10
|
+
@replace_subdomain = options[:replace_subdomain]
|
10
11
|
end
|
11
12
|
|
12
13
|
def call(env)
|
@@ -27,7 +28,11 @@ module Rack
|
|
27
28
|
|
28
29
|
def redirect_to_subdomain(req)
|
29
30
|
url = URI(req.url)
|
30
|
-
|
31
|
+
unless @replace_subdomain
|
32
|
+
url.host = "#{@subdomain}.#{url.host}"
|
33
|
+
else
|
34
|
+
url.host = url.host.gsub(/^([^.]+)(\..*)$/, "#{@subdomain}#{'\2'}")
|
35
|
+
end
|
31
36
|
headers = {'Content-Type' => 'text/html', 'Location' => url.to_s}
|
32
37
|
|
33
38
|
[301, headers, []]
|
data/rack-www-enforcer.gemspec
CHANGED
data/test/test_www-enforcer.rb
CHANGED
@@ -26,9 +26,22 @@ class TestWwwEnforcer < Test::Unit::TestCase
|
|
26
26
|
end
|
27
27
|
|
28
28
|
def test_redirect_to_custom_subdomain
|
29
|
+
old_app = self.app
|
29
30
|
self.app = Rack::WwwEnforcer.new(mock_app, :subdomain => "app")
|
30
31
|
get "http://example.org/"
|
31
32
|
assert_equal "http://app.example.org/",
|
32
33
|
last_response.headers['Location']
|
34
|
+
ensure
|
35
|
+
self.app = old_app
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_redirect_if_subdomain_already_given_and_replacing_subdomain
|
39
|
+
old_app = self.app
|
40
|
+
self.app = Rack::WwwEnforcer.new(mock_app, :subdomain => "app", :replace_subdomain => true)
|
41
|
+
get "http://www.example.org"
|
42
|
+
assert_equal "http://app.example.org/",
|
43
|
+
last_response.headers['Location']
|
44
|
+
ensure
|
45
|
+
self.app = old_app
|
33
46
|
end
|
34
47
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-www-enforcer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-09-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,12 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
25
30
|
description: ! ' Rack middleware to redirect requests to an specific subdomain
|
26
31
|
in your app.
|
27
32
|
|
@@ -61,9 +66,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
61
66
|
version: '0'
|
62
67
|
requirements: []
|
63
68
|
rubyforge_project:
|
64
|
-
rubygems_version: 1.8.
|
69
|
+
rubygems_version: 1.8.22
|
65
70
|
signing_key:
|
66
71
|
specification_version: 3
|
67
72
|
summary: Redirect requests to a subdomain in your app
|
68
73
|
test_files:
|
69
74
|
- test/test_www-enforcer.rb
|
75
|
+
has_rdoc:
|