rack-www 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +10 -0
- data/README.rdoc +20 -5
- data/lib/rack/www.rb +19 -13
- metadata +6 -6
data/CHANGELOG.rdoc
CHANGED
@@ -1,10 +1,20 @@
|
|
1
|
+
== 1.3
|
2
|
+
|
3
|
+
* Added possibility to redirects to any given :subdomain [Ryan Weald (https://github.com/rweald)].
|
4
|
+
* Added more tests
|
5
|
+
|
1
6
|
== 1.2
|
7
|
+
|
2
8
|
* Redirects to the right url without calling the app
|
3
9
|
* Keep the path when redirecting
|
4
10
|
* Keep the query string when redirecting
|
5
11
|
* Added more tests
|
12
|
+
|
6
13
|
== 1.1
|
14
|
+
|
7
15
|
* Added possibility to redirects with www or without www.
|
8
16
|
* Added possibility to set a param :message to show while redirecting.
|
17
|
+
|
9
18
|
== 1.0
|
19
|
+
|
10
20
|
* Redirects all traffic to www.
|
data/README.rdoc
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
|
1
|
+
= rack-www
|
2
2
|
|
3
|
-
Rack middleware to force redirects
|
3
|
+
Rack middleware to force subdomain redirects, e.g: www.
|
4
4
|
|
5
5
|
=== Installation
|
6
6
|
|
@@ -10,25 +10,40 @@ Rack middleware to force redirects all traffic to a single domain with or withou
|
|
10
10
|
#when using bundler
|
11
11
|
gem 'rack-www'
|
12
12
|
|
13
|
+
|
13
14
|
=== Usage
|
14
15
|
|
15
|
-
Default usage:
|
16
|
+
Default usage (by default will redirect all requests to www subdomain):
|
16
17
|
|
17
|
-
#redirects all traffic to www
|
18
|
+
#redirects all traffic to www subdomain
|
18
19
|
config.middleware.use Rack::WWW
|
19
20
|
|
20
|
-
|
21
|
+
Customizing the :www option to true or false:
|
21
22
|
|
22
23
|
#redirects all traffic to www
|
23
24
|
config.middleware.use Rack::WWW, :www => true
|
24
25
|
|
26
|
+
|
25
27
|
#redirects all traffic to the same domain without www
|
26
28
|
config.middleware.use Rack::WWW, :www => false
|
27
29
|
|
30
|
+
Redirecting to a given subdomain:
|
31
|
+
|
32
|
+
#redirects all traffic to the 'secure' subdomain
|
33
|
+
config.middleware.use Rack::WWW, :subdomain => "secure"
|
34
|
+
|
28
35
|
If you like it's also possible to show a message while redirecting the user:
|
29
36
|
|
30
37
|
config.middleware.use Rack::WWW, :www => false, :message => "You are being redirected..."
|
31
38
|
|
39
|
+
|
40
|
+
=== Options
|
41
|
+
|
42
|
+
* :www => default is true, redirects all traffic to www;
|
43
|
+
* :subdomain => redirects to any given subdomain;
|
44
|
+
* :message => display a message while redirecting;
|
45
|
+
|
46
|
+
|
32
47
|
=== License
|
33
48
|
|
34
49
|
MIT License. Copyright 2011 Jhimy Fernandes Villar. http://www.stjhimy.com
|
data/lib/rack/www.rb
CHANGED
@@ -4,15 +4,18 @@ require 'rack/request'
|
|
4
4
|
module Rack
|
5
5
|
class WWW
|
6
6
|
def initialize(app, options = {})
|
7
|
-
@options = {:
|
7
|
+
@options = {:subdomain => "www" }.merge(options)
|
8
8
|
@app = app
|
9
|
-
|
9
|
+
|
10
|
+
@redirect = true
|
11
|
+
@redirect = @options[:www] if @options[:www] != nil
|
10
12
|
@message = @options[:message]
|
13
|
+
@subdomain = @options[:subdomain]
|
11
14
|
end
|
12
15
|
|
13
16
|
def call(env)
|
14
|
-
if (
|
15
|
-
|
17
|
+
if (already_subdomain?(env) && @redirect) || (!already_subdomain?(env) && !@redirect)
|
18
|
+
@app.call(env)
|
16
19
|
else
|
17
20
|
url = prepare_url(env)
|
18
21
|
headers = {"Content-Type" => "text/html", "location" => url}
|
@@ -21,27 +24,30 @@ module Rack
|
|
21
24
|
end
|
22
25
|
|
23
26
|
private
|
24
|
-
def
|
25
|
-
env["HTTP_HOST"].downcase =~ /^(
|
27
|
+
def already_subdomain?(env)
|
28
|
+
env["HTTP_HOST"].downcase =~ /^(#{@subdomain}.)/
|
26
29
|
end
|
27
30
|
|
28
31
|
def prepare_url(env)
|
29
32
|
scheme = env["rack.url_scheme"]
|
30
|
-
host = env["SERVER_NAME"].gsub(/^(www.)/, "")
|
31
|
-
path = env["PATH_INFO"].to_s
|
32
33
|
|
33
|
-
|
34
|
-
|
35
|
-
|
34
|
+
host = env["SERVER_NAME"].gsub(/^(#{@subdomain}.)/, "")
|
35
|
+
host = host.gsub(/^(www.)/, "")
|
36
|
+
|
37
|
+
path = env["PATH_INFO"]
|
38
|
+
|
39
|
+
query_string = ""
|
40
|
+
if !env["QUERY_STRING"].empty?
|
36
41
|
query_string = "?" + env["QUERY_STRING"]
|
37
42
|
end
|
38
43
|
|
39
|
-
if @
|
40
|
-
host = "
|
44
|
+
if @redirect == true
|
45
|
+
host = "://#{@subdomain}." + host
|
41
46
|
else
|
42
47
|
host = "://" + host
|
43
48
|
end
|
44
49
|
scheme + host + path + query_string
|
45
50
|
end
|
51
|
+
|
46
52
|
end
|
47
53
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-www
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
-
-
|
8
|
+
- 3
|
9
9
|
- 0
|
10
|
-
version: 1.
|
10
|
+
version: 1.3.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jhimy Fernandes Villar
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-05-
|
18
|
+
date: 2011-05-25 00:00:00 -03:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: "0"
|
33
33
|
type: :runtime
|
34
34
|
version_requirements: *id001
|
35
|
-
description: " Rack middleware to force redirects
|
35
|
+
description: " Rack middleware to force subdomain redirects, e.g: www.\n"
|
36
36
|
email: stjhimy@gmail.com
|
37
37
|
executables: []
|
38
38
|
|
@@ -79,6 +79,6 @@ rubyforge_project:
|
|
79
79
|
rubygems_version: 1.5.2
|
80
80
|
signing_key:
|
81
81
|
specification_version: 3
|
82
|
-
summary: Force redirects to a
|
82
|
+
summary: "Force redirects to a any given subdomain, e.g: www."
|
83
83
|
test_files: []
|
84
84
|
|