Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Converted to a gem.

  • Loading branch information...
commit 8edbe0e90578ae1b279cb64ee9df64f007aecd40 0 parents
@tylerhunt authored
5 .document
@@ -0,0 +1,5 @@
+README.rdoc
+lib/**/*.rb
+bin/*
+features/**/*.feature
+LICENSE
5 .gitignore
@@ -0,0 +1,5 @@
+*.sw?
+.DS_Store
+coverage
+rdoc
+pkg
20 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 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 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  VERSION
@@ -0,0 +1 @@
+0.0.1
24 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

Thanks, this looks like exactly what I need!

Cheers

Please sign in to comment.
Something went wrong with that request. Please try again.