rack-canonical-host 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Tyler Hunt
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,32 @@
1
+ = Rack Canonical Host
2
+
3
+ Rack Canonical Host is a bit of Rack middleware that lets you define a single
4
+ host as the canonical host for your site, and redirect all other hostnames to
5
+ that canonical host.
6
+
7
+ In the simplest case, just specify the host name and any requests coming in
8
+ that aren't for the host example.com will be rewritten with the proper host.
9
+
10
+ use Rack::CanonicalHost, 'example.com'
11
+
12
+ You could also define a constant in your environment-specific configuration
13
+ files to set up a canonical host redirect for only certain environments.
14
+
15
+ CANONICAL_HOST = 'example.com'
16
+
17
+ Then, you can set up the middle are like this.
18
+
19
+ use Rack::CanonicalHost, CANONICAL_HOST if defined?(CANONICAL_HOST)
20
+
21
+ Alternatively, you can pass a block whose return value will be used to set the
22
+ canonical host.
23
+
24
+ use Rack::CanonicalHost do
25
+ case ENV['RACK_ENV'].to_sym
26
+ when :staging then 'example.com'
27
+ when :production then 'staging.example.com'
28
+ end
29
+ end
30
+
31
+
32
+ Copyright (c) 2009 Tyler Hunt. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/rdoctask'
4
+
5
+ begin
6
+ require 'jeweler'
7
+
8
+ Jeweler::Tasks.new do |gem|
9
+ gem.name = 'rack-canonical-host'
10
+ gem.summary = %Q{Rack middleware for defining a canonical host name.}
11
+ gem.description = %Q{Rack middleware for defining a canonical host name. It will redirect all requests to non-canonical hosts to the canonical host.}
12
+ gem.email = 'tyler@tylerhunt.com'
13
+ gem.homepage = 'http://github.com/tylerhunt/rack-canonical-host'
14
+ gem.authors = ['Tyler Hunt']
15
+ end
16
+
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts 'Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler'
20
+ end
21
+
22
+ Rake::RDocTask.new do |rdoc|
23
+ if File.exist?('VERSION')
24
+ version = File.read('VERSION')
25
+ else
26
+ version = ''
27
+ end
28
+
29
+ rdoc.rdoc_dir = 'rdoc'
30
+ rdoc.title = "rack-canonical-host #{version}"
31
+ rdoc.rdoc_files.include('README*')
32
+ rdoc.rdoc_files.include('LICENSE')
33
+ rdoc.rdoc_files.include('lib/**/*.rb')
34
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,24 @@
1
+ module Rack # :nodoc:
2
+ class CanonicalHost
3
+ def initialize(app, host=nil, &block)
4
+ @app = app
5
+ @host = (block_given? && block.call) || host
6
+ end
7
+
8
+ def call(env)
9
+ if url = url(env)
10
+ [301, { 'Location' => url }, ['Redirecting...']]
11
+ else
12
+ @app.call(env)
13
+ end
14
+ end
15
+
16
+ def url(env)
17
+ if @host && env['SERVER_NAME'] != @host
18
+ url = Rack::Request.new(env).url
19
+ url.sub(%r{\A(https?://)(.*?)(:\d+)?(/|$)}, "\\1#{@host}\\3/")
20
+ end
21
+ end
22
+ private :url
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-canonical-host
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tyler Hunt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-04 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Rack middleware for defining a canonical host name. It will redirect all requests to non-canonical hosts to the canonical host.
17
+ email: tyler@tylerhunt.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION
32
+ - lib/rack-canonical-host.rb
33
+ has_rdoc: true
34
+ homepage: http://github.com/tylerhunt/rack-canonical-host
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options:
39
+ - --charset=UTF-8
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.3.5
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: Rack middleware for defining a canonical host name.
61
+ test_files: []
62
+