Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9574e8a
Showing
10 changed files
with
239 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
*.gem | ||
*.rbc | ||
.bundle | ||
.config | ||
.yardoc | ||
Gemfile.lock | ||
InstalledFiles | ||
_yardoc | ||
coverage | ||
doc/ | ||
lib/bundler/man | ||
pkg | ||
rdoc | ||
spec/reports | ||
test/tmp | ||
test/version_tmp | ||
tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
source 'https://rubygems.org' | ||
|
||
# Specify your gem's dependencies in rack-canonical-host.gemspec | ||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Copyright (c) 2012 Fabrizio Regini | ||
|
||
MIT License | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Rack::CanonicalHost | ||
|
||
Use this rack module if you want to have your application respond to just | ||
one domain name and redirect any other the domain. | ||
|
||
For example when your applicaiton responds to both www and non www prefixed | ||
domain name, or to both www.mysite.org and mysite.herokuapp.com. | ||
|
||
Redirect preserves path and querystring. | ||
|
||
## Installation | ||
|
||
Add this line to your application's Gemfile: | ||
|
||
gem 'rack-canonical-host', :require => 'rack/canonical_host' | ||
|
||
And then execute: | ||
|
||
$ bundle | ||
|
||
Or install it yourself as: | ||
|
||
$ gem install rack-canonical-host | ||
|
||
## Usage | ||
|
||
Configure three options: | ||
|
||
* host: the actual canonical host name for your site | ||
* scheme: redirect to http or https. Default to http | ||
* ignore: an array of hostnames to ignore | ||
|
||
Example for rails configuration: | ||
|
||
``` | ||
config.middleware.insert_before Rack::Lock, Rack::CanonicalHost, { | ||
:host => 'my-site.com', | ||
:scheme => 'https', | ||
:ignore => ['staging.my-site.com', 'balabik-staging.herokuapp.com'] | ||
} | ||
``` | ||
|
||
## Contributing | ||
|
||
1. Fork it | ||
2. Create your feature branch (`git checkout -b my-new-feature`) | ||
3. Commit your changes (`git commit -am 'Added some feature'`) | ||
4. Push to the branch (`git push origin my-new-feature`) | ||
5. Create new Pull Request |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/usr/bin/env rake | ||
require "bundler/gem_tasks" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module Rack | ||
module Canonical | ||
module Host | ||
VERSION = "0.0.1" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module Rack | ||
class CanonicalHost | ||
|
||
VERSION = 0.1 | ||
|
||
def initialize(app, options = {}) | ||
@app = app | ||
@host = options.fetch(:host) | ||
@scheme = options.fetch(:scheme) { 'http' } | ||
@ignore = options.fetch(:ignore) { [] } | ||
end | ||
|
||
|
||
def request_host | ||
@env['HTTP_HOST'].split(':').first | ||
end | ||
|
||
def call(env) | ||
@env = env | ||
if request_host != @host && !@ignore.include?(request_host) | ||
uri = URI.parse '' | ||
uri.host = @host | ||
uri.query = env['QUERY_STRING'] || '' | ||
uri.path = env['REQUEST_PATH'] || '' | ||
uri.scheme = @scheme | ||
|
||
status = 301 | ||
headers = {'Location' => uri.to_s, 'Content-Type' => 'text/plain'} | ||
body = ["Redirecting to canonical URL #{uri}"] | ||
|
||
[status, headers, body] | ||
else | ||
@app.call(env) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# -*- encoding: utf-8 -*- | ||
require File.expand_path('../lib/rack-canonical-host/version', __FILE__) | ||
|
||
Gem::Specification.new do |gem| | ||
gem.authors = ["Fabrizio Regini"] | ||
gem.email = ["freegenie@gmail.com"] | ||
gem.description = %q{Handle redirect to canonical host} | ||
gem.summary = %q{Rack module to redirect to one canonical host and redirect when hitting the app with any other name} | ||
gem.homepage = "" | ||
|
||
gem.files = `git ls-files`.split($\) | ||
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } | ||
gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) | ||
gem.name = "rack-canonical-host" | ||
gem.require_paths = ["lib"] | ||
gem.version = Rack::CanonicalHost::VERSION | ||
gem.add_development_dependency 'rspec', '~> 2.0' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
require 'rack/mock' | ||
require_relative '../lib/rack/canonical_host' | ||
|
||
describe Rack::CanonicalHost do | ||
let(:simple_app) do | ||
lambda {|env| [200, {'Content-type' => 'text/html'}, ['Hello Rack'] ] } | ||
end | ||
|
||
context 'with canonical host at test.example.org' do | ||
let(:app) do | ||
Rack::CanonicalHost.new(simple_app, | ||
:scheme => 'https', | ||
:host => 'test.example.org', | ||
:ignore => ['ignore.example.org'] | ||
) | ||
end | ||
|
||
it 'should pass if domain name is canonical' do | ||
response = Rack::MockRequest.new(app) | ||
.get('/', {'HTTP_HOST' => 'test.example.org'}) | ||
|
||
response.status.should == 200 | ||
end | ||
|
||
it 'should redirect if domain name is not canonical' do | ||
response = Rack::MockRequest.new(app) | ||
.get('/', {'HTTP_HOST' => 'foo.example.org'}) | ||
|
||
response.status.should == 301 | ||
end | ||
|
||
it 'should redirect with path' do | ||
response = Rack::MockRequest.new(app) | ||
.get('/', { | ||
'HTTP_HOST' => 'foo.example.org', | ||
'REQUEST_PATH' => '/login' | ||
}) | ||
|
||
response.headers['Location'].should =~ /\/login/ | ||
end | ||
|
||
it 'should redirect with query string' do | ||
response = Rack::MockRequest.new(app) | ||
.get('/', { | ||
'HTTP_HOST' => 'foo.example.org', | ||
'REQUEST_PATH' => '/login', | ||
'QUERY_STRING' => 'a=10&b=20' | ||
}) | ||
|
||
response.headers['Location'].should =~ /\?a=10&b=20/ | ||
end | ||
|
||
it 'should redirect with scheme' do | ||
response = Rack::MockRequest.new(app) | ||
.get('/', { | ||
'HTTP_HOST' => 'foo.example.org', | ||
}) | ||
|
||
response.headers['Location'].should =~ /https:\/\// | ||
end | ||
|
||
it 'should not redirect if host is in ignore list' do | ||
response = Rack::MockRequest.new(app) | ||
.get('/', { | ||
'HTTP_HOST' => 'ignore.example.org', | ||
}) | ||
|
||
response.status.should == 200 | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# This file was generated by the `rspec --init` command. Conventionally, all | ||
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. | ||
# Require this file using `require "spec_helper.rb"` to ensure that it is only | ||
# loaded once. | ||
# | ||
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration | ||
RSpec.configure do |config| | ||
config.treat_symbols_as_metadata_keys_with_true_values = true | ||
config.run_all_when_everything_filtered = true | ||
config.filter_run :focus | ||
end |