Skip to content

Commit

Permalink
Converted to a gem.
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerhunt committed Nov 4, 2009
0 parents commit 8edbe0e
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .document
@@ -0,0 +1,5 @@
README.rdoc
lib/**/*.rb
bin/*
features/**/*.feature
LICENSE
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
*.sw?
.DS_Store
coverage
rdoc
pkg
20 changes: 20 additions & 0 deletions LICENSE
@@ -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.
32 changes: 32 additions & 0 deletions README.rdoc
@@ -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.
34 changes: 34 additions & 0 deletions Rakefile
@@ -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
1 change: 1 addition & 0 deletions VERSION
@@ -0,0 +1 @@
0.0.1
24 changes: 24 additions & 0 deletions lib/canonical-host.rb
@@ -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

1 comment on commit 8edbe0e

@will
Copy link

@will will commented on 8edbe0e Jun 12, 2010

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

Please sign in to comment.