rack-domain_redirect 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,24 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ # As we are using jeweler we generate gemspec file automatically using
23
+ # rake build
24
+ *.gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Michal Pawlowski, itsudo ltd
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,45 @@
1
+ = Rack::DomainRedirect
2
+
3
+ - Code: http://github.com/misza222/rack-domain_redirect
4
+ - Build: http://runcoderun.com/misza222/rack-domain_redirect
5
+
6
+ == Description
7
+
8
+ Rack::DomainRedirect is a tiny rack middleware for redirecting to the
9
+ configurable domain. If user request's the service from domain other than that
10
+ configured it redirects to the first domain on the configuration list. If no
11
+ domain is configured it does noting and passes requests to another application
12
+ in the chain.
13
+
14
+ == Features
15
+
16
+ * Configurable accepted domains,
17
+ * if not configured it degrades nicely and does nothing,
18
+ * preserves path and query string when redirecting.
19
+
20
+ == Example
21
+
22
+ # This example allows requests for example.com and localhost.
23
+ # If any other domain is requested it redirects to the first domain on the
24
+ # list - example.com preserving path and query string from the first request.
25
+ require "rack/domain_redirect"
26
+
27
+ # This is how you use and configure Rack::DomainRedirect middleware
28
+ use Rack::DomainRedirect, ['example.com', 'localhost']
29
+
30
+ run lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['Hello world!']] }
31
+
32
+ == Install
33
+
34
+ To install the latest release as a gem:
35
+
36
+ sudo gem install rack-domain_redirect
37
+
38
+ == Credits
39
+
40
+ Many thanks to brynary for http://github.com/brynary/rack-test
41
+
42
+ == Copyright
43
+
44
+ Copyright (c) 2010 Michal Pawlowski, itsudo ltd
45
+ See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,60 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "rack-domain_redirect"
8
+ gem.summary = %Q{Rack::DomainRedirect redirects to the configurable domain.}
9
+ gem.description = %Q{
10
+ Rack::DomainRedirect is a tiny rack middleware for redirecting to the
11
+ configurable domain. If user request's the service from domain other than that
12
+ configured it redirects to the first domain on the configuration list. If no
13
+ domain is configured it does noting and passes requests to another application
14
+ in the chain.}
15
+ gem.email = "misza222@gmail.com"
16
+ gem.homepage = "http://github.com/misza222/rack-domain_redirect"
17
+ gem.authors = ["Michal Pawlowski"]
18
+ gem.add_development_dependency "shoulda", ">= 2"
19
+ gem.add_development_dependency "rack-test", ">= 0.5"
20
+ gem.add_dependency "rack", ">= 1.1"
21
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
22
+ end
23
+ Jeweler::GemcutterTasks.new
24
+ rescue LoadError
25
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
26
+ end
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ begin
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ end
42
+ rescue LoadError
43
+ task :rcov do
44
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
45
+ end
46
+ end
47
+
48
+ task :test => :check_dependencies
49
+
50
+ task :default => :test
51
+
52
+ require 'rake/rdoctask'
53
+ Rake::RDocTask.new do |rdoc|
54
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
55
+
56
+ rdoc.rdoc_dir = 'rdoc'
57
+ rdoc.title = "domain_redirect #{version}"
58
+ rdoc.rdoc_files.include('README*')
59
+ rdoc.rdoc_files.include('lib/**/*.rb')
60
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
data/example/config.ru ADDED
@@ -0,0 +1,9 @@
1
+ # This example allows requests for example.com and another-example.com.
2
+ # If any other domain is requested it redirects to the first domain on the
3
+ # list - example.com preserving path and query string from the first request.
4
+ require "rack/domain_redirect"
5
+
6
+ # This is how you use and configure Rack::DomainRedirect middleware
7
+ use Rack::DomainRedirect, ['example.com', 'localhost']
8
+
9
+ run lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['Hello world!']] }
@@ -0,0 +1,30 @@
1
+ module Rack
2
+
3
+ # Automatically redirects to the configurable domain
4
+ #
5
+ # If request comes from other than specified domains it redirects to the first
6
+ # domain from the list
7
+ class DomainRedirect
8
+
9
+ def initialize(app, hosts = [])
10
+ @app = app
11
+ @hosts = hosts
12
+ end
13
+
14
+ def call(env)
15
+ req = Rack::Request.new(env)
16
+
17
+ if @hosts.nil? or @hosts.empty? or @hosts.include?(req.host)
18
+ @app.call(env)
19
+ else
20
+ url = "http://#{@hosts[0]}"
21
+ # url << ":#{req.port}" unless req.port == 80
22
+ url << "#{req.path}"
23
+ url << "?#{req.query_string}" unless req.query_string.empty?
24
+ res = Rack::Response.new
25
+ res.redirect(url)
26
+ res.finish
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,76 @@
1
+ require 'rubygems'
2
+ require 'rack/test'
3
+ require 'rack/mock'
4
+ require 'shoulda'
5
+ require 'rack/domain_redirect'
6
+
7
+ class TestDomainRedirect < Test::Unit::TestCase
8
+ include Rack::Test::Methods
9
+
10
+ def app
11
+ testing_app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, [@body]] }
12
+ Rack::DomainRedirect.new(testing_app, @allowed_hosts)
13
+ end
14
+
15
+ def build_query_string(hash_query)
16
+ hash_query.map do |key, value|
17
+ "#{key}=#{value}"
18
+ end.join('&')
19
+ end
20
+
21
+ context "DomainRedirect middleware" do
22
+ setup do
23
+ @body = 'Hello world!'
24
+ @allowed_hosts = ['example1.com','example2.com']
25
+ @not_allowed_hosts = ['disallowed.com']
26
+ end
27
+
28
+ should "not redirect if host within allowed_hosts" do
29
+ get "", {}, Rack::MockRequest.env_for("/", "SERVER_NAME" => @allowed_hosts[0])
30
+
31
+ assert ! last_response.redirect?
32
+ assert last_response.ok?
33
+ assert_equal @body, last_response.body
34
+
35
+ get "", {}, Rack::MockRequest.env_for("/", "SERVER_NAME" => @allowed_hosts[1])
36
+
37
+ assert ! last_response.redirect?
38
+ assert last_response.ok?
39
+ assert_equal @body, last_response.body
40
+ end
41
+
42
+ should "redirect to the first host from the allowed_hosts list" do
43
+
44
+ query_hash = {'query' => 'string', 'another' => 'one'}
45
+ path = "path/on/the/server?" << build_query_string(query_hash)
46
+
47
+ get "", {}, Rack::MockRequest.env_for("/" << path, "SERVER_NAME" => @not_allowed_hosts[0])
48
+ follow_redirect!
49
+
50
+ assert_equal @allowed_hosts[0], last_request.host
51
+ assert_equal "/path/on/the/server", last_request.path
52
+ assert_equal build_query_string(query_hash), last_request.query_string
53
+ assert last_response.ok?
54
+ end
55
+
56
+ should "degrade if not configured (allowed hosts is nil)" do
57
+ @allowed_hosts = nil
58
+
59
+ get "", {}, Rack::MockRequest.env_for("/", "SERVER_NAME" => @not_allowed_hosts[0])
60
+
61
+ assert ! last_response.redirect?
62
+ assert last_response.ok?
63
+ assert_equal @body, last_response.body
64
+ end
65
+
66
+ should "degrade if not configured (allowed hosts is an empty array)" do
67
+ @allowed_hosts = []
68
+
69
+ get "", {}, Rack::MockRequest.env_for("/", "SERVER_NAME" => @not_allowed_hosts[0])
70
+
71
+ assert ! last_response.redirect?
72
+ assert last_response.ok?
73
+ assert_equal @body, last_response.body
74
+ end
75
+ end
76
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-domain_redirect
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Michal Pawlowski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-14 00:00:00 +00:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "2"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: rack-test
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0.5"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: rack
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "1.1"
44
+ version:
45
+ description: |-
46
+
47
+ Rack::DomainRedirect is a tiny rack middleware for redirecting to the
48
+ configurable domain. If user request's the service from domain other than that
49
+ configured it redirects to the first domain on the configuration list. If no
50
+ domain is configured it does noting and passes requests to another application
51
+ in the chain.
52
+ email: misza222@gmail.com
53
+ executables: []
54
+
55
+ extensions: []
56
+
57
+ extra_rdoc_files:
58
+ - LICENSE
59
+ - README.rdoc
60
+ files:
61
+ - .document
62
+ - .gitignore
63
+ - LICENSE
64
+ - README.rdoc
65
+ - Rakefile
66
+ - VERSION
67
+ - example/config.ru
68
+ - lib/rack/domain_redirect.rb
69
+ - test/test_domain_redirect.rb
70
+ has_rdoc: true
71
+ homepage: http://github.com/misza222/rack-domain_redirect
72
+ licenses: []
73
+
74
+ post_install_message:
75
+ rdoc_options:
76
+ - --charset=UTF-8
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: "0"
84
+ version:
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: "0"
90
+ version:
91
+ requirements: []
92
+
93
+ rubyforge_project:
94
+ rubygems_version: 1.3.5
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: Rack::DomainRedirect redirects to the configurable domain.
98
+ test_files:
99
+ - test/test_domain_redirect.rb