rack-non-www-enforcer 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/README.md +14 -0
- data/Rakefile +9 -0
- data/lib/rack-non-www-enforcer.rb +1 -0
- data/lib/rack/non-www-enforcer.rb +22 -0
- data/rack-non-www-enforcer.gemspec +20 -0
- data/test/test_non-www-enforcer.rb +27 -0
- metadata +70 -0
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Guillermo Iguaran
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
Rack::NonWwwEnforcer
|
2
|
+
=================
|
3
|
+
|
4
|
+
Rack Middleware for redirect requests to the main domain of the app. (Ex. http://www.mysite.com to https://mysite.com)
|
5
|
+
|
6
|
+
Usage
|
7
|
+
-------
|
8
|
+
|
9
|
+
use Rack::NonWwwEnforcer
|
10
|
+
|
11
|
+
|
12
|
+
You can use it in your Rails app, in the environment configuration file add it to Middleware stack:
|
13
|
+
|
14
|
+
config.middleware.insert_before ActionDispatch::Static, Rack::NonWwwEnforcer
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "rack/non-www-enforcer"
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require "rack"
|
2
|
+
require "rack/request"
|
3
|
+
|
4
|
+
module Rack
|
5
|
+
class NonWwwEnforcer
|
6
|
+
|
7
|
+
def initialize(app)
|
8
|
+
@app = app
|
9
|
+
end
|
10
|
+
|
11
|
+
def call(env)
|
12
|
+
req = Rack::Request.new(env)
|
13
|
+
url = URI(req.url)
|
14
|
+
if url.host =~ /^www\./i
|
15
|
+
headers = {'Content-Type' => 'text/html', 'Location' => url.to_s.gsub('www.', '')}
|
16
|
+
[301, headers, []]
|
17
|
+
else
|
18
|
+
@app.call(env)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "rack-non-www-enforcer"
|
5
|
+
s.version = "0.0.1"
|
6
|
+
s.authors = ["Guillermo Iguaran"]
|
7
|
+
s.email = ["guilleiguaran@gmail.com"]
|
8
|
+
s.homepage = "https://github.com/guilleiguaran/rack-www-enforcer"
|
9
|
+
s.summary = %q{Redirect subdomains requesto to the main domain in your app}
|
10
|
+
s.description = <<-EOS
|
11
|
+
Rack middleware to redirect requests to the main domain in your app.
|
12
|
+
EOS
|
13
|
+
|
14
|
+
s.add_dependency 'rack'
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
$:.push File.expand_path("../../lib", __FILE__)
|
2
|
+
require 'rack-non-www-enforcer'
|
3
|
+
|
4
|
+
require 'test/unit'
|
5
|
+
require 'rack/test'
|
6
|
+
|
7
|
+
class TestNonWwwEnforcer < Test::Unit::TestCase
|
8
|
+
include Rack::Test::Methods
|
9
|
+
|
10
|
+
def mock_app
|
11
|
+
lambda { |env|
|
12
|
+
headers = {'Content-Type' => "text/html"}
|
13
|
+
[200, headers, ["OK"]]
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
def app
|
18
|
+
@app ||= Rack::NonWwwEnforcer.new(mock_app)
|
19
|
+
end
|
20
|
+
attr_writer :app
|
21
|
+
|
22
|
+
def test_redirect_www_to_main_domain
|
23
|
+
get "http://www.example.org/"
|
24
|
+
assert_equal "http://example.org/",
|
25
|
+
last_response.headers['Location']
|
26
|
+
end
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-non-www-enforcer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Guillermo Iguaran
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-08-03 00:00:00.000000000 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rack
|
17
|
+
requirement: &2157194700 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *2157194700
|
26
|
+
description: ! ' Rack middleware to redirect requests to the main domain in your
|
27
|
+
app.
|
28
|
+
|
29
|
+
'
|
30
|
+
email:
|
31
|
+
- guilleiguaran@gmail.com
|
32
|
+
executables: []
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files: []
|
35
|
+
files:
|
36
|
+
- Gemfile
|
37
|
+
- LICENSE
|
38
|
+
- README.md
|
39
|
+
- Rakefile
|
40
|
+
- lib/rack-non-www-enforcer.rb
|
41
|
+
- lib/rack/non-www-enforcer.rb
|
42
|
+
- rack-non-www-enforcer.gemspec
|
43
|
+
- test/test_non-www-enforcer.rb
|
44
|
+
has_rdoc: true
|
45
|
+
homepage: https://github.com/guilleiguaran/rack-www-enforcer
|
46
|
+
licenses: []
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.6.2
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: Redirect subdomains requesto to the main domain in your app
|
69
|
+
test_files:
|
70
|
+
- test/test_non-www-enforcer.rb
|