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 8edbe0e
Showing
7 changed files
with
121 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,5 @@ | ||
README.rdoc | ||
lib/**/*.rb | ||
bin/* | ||
features/**/*.feature | ||
LICENSE |
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,5 @@ | ||
*.sw? | ||
.DS_Store | ||
coverage | ||
rdoc | ||
pkg |
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,20 @@ | ||
Copyright (c) 2009 Tyler Hunt | ||
|
||
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,32 @@ | ||
= Canonical Host | ||
|
||
Canonical Host is a bit of Rack middleware that lets you define a single host | ||
as the canonical host for your site, and redirect all other hostnames to that | ||
canonical host. | ||
|
||
In the simplest case, just specify the host name and any requests coming in | ||
that aren't for the host example.com will be rewritten with the proper host. | ||
|
||
use CanonicalHost, 'example.com' | ||
|
||
You could also define a constant in your environment-specific configuration | ||
files to set up a canonical host redirect for only certain environments. | ||
|
||
CANONICAL_HOST = 'example.com' | ||
|
||
Then, you can set up the middle are like this. | ||
|
||
use CanonicalHost, CANONICAL_HOST if defined?(CANONICAL_HOST) | ||
|
||
Alternatively, you can pass a block whose return value will be used to set the | ||
canonical host. | ||
|
||
use CanonicalHost do | ||
case ENV['RACK_ENV'].to_sym | ||
when :staging then 'example.com' | ||
when :production then 'staging.example.com' | ||
end | ||
end | ||
|
||
|
||
Copyright (c) 2009 Tyler Hunt. See LICENSE for details. |
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,34 @@ | ||
require 'rubygems' | ||
require 'rake' | ||
require 'rake/rdoctask' | ||
|
||
begin | ||
require 'jeweler' | ||
|
||
Jeweler::Tasks.new do |gem| | ||
gem.name = 'rack-canonical-host' | ||
gem.summary = %Q{Rack middleware for defining a canonical host name.} | ||
gem.description = %Q{Rack middleware for defining a canonical host name. It will redirect all requests to non-canonical hosts to the canonical host.} | ||
gem.email = 'tyler@tylerhunt.com' | ||
gem.homepage = 'http://github.com/tylerhunt/rack-canonical-host' | ||
gem.authors = ['Tyler Hunt'] | ||
end | ||
|
||
Jeweler::GemcutterTasks.new | ||
rescue LoadError | ||
puts 'Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler' | ||
end | ||
|
||
Rake::RDocTask.new do |rdoc| | ||
if File.exist?('VERSION') | ||
version = File.read('VERSION') | ||
else | ||
version = '' | ||
end | ||
|
||
rdoc.rdoc_dir = 'rdoc' | ||
rdoc.title = "rack-canonical-host #{version}" | ||
rdoc.rdoc_files.include('README*') | ||
rdoc.rdoc_files.include('LICENSE') | ||
rdoc.rdoc_files.include('lib/**/*.rb') | ||
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 @@ | ||
0.0.1 |
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,24 @@ | ||
module Rack # :nodoc: | ||
class CanonicalHost | ||
def initialize(app, host=nil, &block) | ||
@app = app | ||
@host = (block_given? && block.call) || host | ||
end | ||
|
||
def call(env) | ||
if url = url(env) | ||
[301, { 'Location' => url }, ['Redirecting...']] | ||
else | ||
@app.call(env) | ||
end | ||
end | ||
|
||
def url(env) | ||
if @host && env['SERVER_NAME'] != @host | ||
url = Rack::Request.new(env).url | ||
url.sub(%r{\A(https?://)(.*?)(:\d+)?(/|$)}, "\\1#{@host}\\3/") | ||
end | ||
end | ||
private :url | ||
end | ||
end |
8edbe0e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this looks like exactly what I need!
Cheers