rack-canonical-hostname 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +49 -0
- data/Rakefile +2 -0
- data/lib/rack/canonical_host.rb +37 -0
- data/rack-canonical-hostname.gemspec +18 -0
- data/spec/canonical_host_spec.rb +72 -0
- data/spec/spec_helper.rb +11 -0
- metadata +68 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Fabrizio Regini
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Rack::CanonicalHost
|
2
|
+
|
3
|
+
Use this rack module if you want to have your application respond to just
|
4
|
+
one domain name and redirect any other the domain.
|
5
|
+
|
6
|
+
For example when your applicaiton responds to both www and non www prefixed
|
7
|
+
domain name, or to both www.mysite.org and mysite.herokuapp.com.
|
8
|
+
|
9
|
+
Redirect preserves path and querystring.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'rack-canonical-host', :require => 'rack/canonical_host'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install rack-canonical-host
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
Configure three options:
|
28
|
+
|
29
|
+
* host: the actual canonical host name for your site
|
30
|
+
* scheme: redirect to http or https. Default to http
|
31
|
+
* ignore: an array of hostnames to ignore
|
32
|
+
|
33
|
+
Example for rails configuration:
|
34
|
+
|
35
|
+
```
|
36
|
+
config.middleware.insert_before Rack::Lock, Rack::CanonicalHost, {
|
37
|
+
:host => 'my-site.com',
|
38
|
+
:scheme => 'https',
|
39
|
+
:ignore => ['staging.my-site.com', 'balabik-staging.herokuapp.com']
|
40
|
+
}
|
41
|
+
```
|
42
|
+
|
43
|
+
## Contributing
|
44
|
+
|
45
|
+
1. Fork it
|
46
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
47
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
48
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
49
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
module Rack
|
2
|
+
class CanonicalHost
|
3
|
+
|
4
|
+
VERSION = 0.1
|
5
|
+
|
6
|
+
def initialize(app, options = {})
|
7
|
+
@app = app
|
8
|
+
@host = options.fetch(:host)
|
9
|
+
@scheme = options.fetch(:scheme) { 'http' }
|
10
|
+
@ignore = options.fetch(:ignore) { [] }
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
def request_host
|
15
|
+
@env['HTTP_HOST'].split(':').first
|
16
|
+
end
|
17
|
+
|
18
|
+
def call(env)
|
19
|
+
@env = env
|
20
|
+
if request_host != @host && !@ignore.include?(request_host)
|
21
|
+
uri = URI.parse ''
|
22
|
+
uri.host = @host
|
23
|
+
uri.query = env['QUERY_STRING'] || ''
|
24
|
+
uri.path = env['REQUEST_PATH'] || ''
|
25
|
+
uri.scheme = @scheme
|
26
|
+
|
27
|
+
status = 301
|
28
|
+
headers = {'Location' => uri.to_s, 'Content-Type' => 'text/plain'}
|
29
|
+
body = ["Redirecting to canonical URL #{uri}"]
|
30
|
+
|
31
|
+
[status, headers, body]
|
32
|
+
else
|
33
|
+
@app.call(env)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/rack/canonical_host', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Fabrizio Regini"]
|
6
|
+
gem.email = ["freegenie@gmail.com"]
|
7
|
+
gem.description = %q{Handle redirect to canonical host}
|
8
|
+
gem.summary = %q{Rack module to redirect to one canonical host and redirect when hitting the app with any other name}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "rack-canonical-hostname"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Rack::CanonicalHost::VERSION
|
17
|
+
gem.add_development_dependency 'rspec', '~> 2.0'
|
18
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'rack/mock'
|
2
|
+
require_relative '../lib/rack/canonical_host'
|
3
|
+
|
4
|
+
describe Rack::CanonicalHost do
|
5
|
+
let(:simple_app) do
|
6
|
+
lambda {|env| [200, {'Content-type' => 'text/html'}, ['Hello Rack'] ] }
|
7
|
+
end
|
8
|
+
|
9
|
+
context 'with canonical host at test.example.org' do
|
10
|
+
let(:app) do
|
11
|
+
Rack::CanonicalHost.new(simple_app,
|
12
|
+
:scheme => 'https',
|
13
|
+
:host => 'test.example.org',
|
14
|
+
:ignore => ['ignore.example.org']
|
15
|
+
)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should pass if domain name is canonical' do
|
19
|
+
response = Rack::MockRequest.new(app)
|
20
|
+
.get('/', {'HTTP_HOST' => 'test.example.org'})
|
21
|
+
|
22
|
+
response.status.should == 200
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should redirect if domain name is not canonical' do
|
26
|
+
response = Rack::MockRequest.new(app)
|
27
|
+
.get('/', {'HTTP_HOST' => 'foo.example.org'})
|
28
|
+
|
29
|
+
response.status.should == 301
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should redirect with path' do
|
33
|
+
response = Rack::MockRequest.new(app)
|
34
|
+
.get('/', {
|
35
|
+
'HTTP_HOST' => 'foo.example.org',
|
36
|
+
'REQUEST_PATH' => '/login'
|
37
|
+
})
|
38
|
+
|
39
|
+
response.headers['Location'].should =~ /\/login/
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should redirect with query string' do
|
43
|
+
response = Rack::MockRequest.new(app)
|
44
|
+
.get('/', {
|
45
|
+
'HTTP_HOST' => 'foo.example.org',
|
46
|
+
'REQUEST_PATH' => '/login',
|
47
|
+
'QUERY_STRING' => 'a=10&b=20'
|
48
|
+
})
|
49
|
+
|
50
|
+
response.headers['Location'].should =~ /\?a=10&b=20/
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'should redirect with scheme' do
|
54
|
+
response = Rack::MockRequest.new(app)
|
55
|
+
.get('/', {
|
56
|
+
'HTTP_HOST' => 'foo.example.org',
|
57
|
+
})
|
58
|
+
|
59
|
+
response.headers['Location'].should =~ /https:\/\//
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'should not redirect if host is in ignore list' do
|
63
|
+
response = Rack::MockRequest.new(app)
|
64
|
+
.get('/', {
|
65
|
+
'HTTP_HOST' => 'ignore.example.org',
|
66
|
+
})
|
67
|
+
|
68
|
+
response.status.should == 200
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper.rb"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
config.filter_run :focus
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-canonical-hostname
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Fabrizio Regini
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-18 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &2154510280 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2154510280
|
25
|
+
description: Handle redirect to canonical host
|
26
|
+
email:
|
27
|
+
- freegenie@gmail.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- LICENSE
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- lib/rack/canonical_host.rb
|
38
|
+
- rack-canonical-hostname.gemspec
|
39
|
+
- spec/canonical_host_spec.rb
|
40
|
+
- spec/spec_helper.rb
|
41
|
+
homepage: ''
|
42
|
+
licenses: []
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.8.17
|
62
|
+
signing_key:
|
63
|
+
specification_version: 3
|
64
|
+
summary: Rack module to redirect to one canonical host and redirect when hitting the
|
65
|
+
app with any other name
|
66
|
+
test_files:
|
67
|
+
- spec/canonical_host_spec.rb
|
68
|
+
- spec/spec_helper.rb
|