quantipay-foreign-domain-routing 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +126 -0
- data/VERSION.yml +4 -0
- data/lib/foreign_domain_routing.rb +34 -0
- data/lib/foreign_domain_routing/routing_extensions.rb +55 -0
- data/test/foreign_domain_routing_test.rb +135 -0
- data/test/test_helper.rb +12 -0
- metadata +60 -0
data/README.rdoc
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
Foreign Domain Routing +
|
2
|
+
Request Routing Plugin for Ruby on Rails
|
3
|
+
-----------------------------------------
|
4
|
+
=> Foreign Domain Request Routing
|
5
|
+
|
6
|
+
-------------------------------------------------------------------------------
|
7
|
+
Foreign Domain Routing provides simple handling of foreign domains in Rails.
|
8
|
+
It borrows largely from SubdomainFu and some other snippets found around the web.
|
9
|
+
|
10
|
+
Request routing allows you to define routing conditions that test
|
11
|
+
methods/properties of the request object such as subdomain, domain,
|
12
|
+
port. You can test them either against a value or with a Regexp
|
13
|
+
(assuming the method returns a string)
|
14
|
+
|
15
|
+
Merging these two plugins allows them to work together and enhance the routing
|
16
|
+
capability of Rails.
|
17
|
+
|
18
|
+
Documentation from the original plugins follows...
|
19
|
+
|
20
|
+
-------------------------------------------------------------------------------
|
21
|
+
Foreign Domain Routing
|
22
|
+
-------------------------------------------------------------------------------
|
23
|
+
|
24
|
+
Installation
|
25
|
+
============
|
26
|
+
|
27
|
+
Foreign Domain Routing is available as a plugin. To install it with Rails 2.1
|
28
|
+
or later):
|
29
|
+
|
30
|
+
script/plugin install git://github.com/brianmulloy/foreign-domain-routing.git
|
31
|
+
|
32
|
+
Examples
|
33
|
+
========
|
34
|
+
|
35
|
+
Foreign Domain Routing extends Rails's routing mechanisms to provide a way to
|
36
|
+
redirect non-native domains.
|
37
|
+
|
38
|
+
Let's say my rails app domain is 'mydomain.com' and I am creating an app that
|
39
|
+
allows users to add a CNAME record to map their subdomain so that
|
40
|
+
'foo.usersdomain.com' would actually point to 'mydomain.com/users/1234'.
|
41
|
+
|
42
|
+
The route at the top of config/routes.rb would look like:
|
43
|
+
|
44
|
+
map.connect '*path', :controller => 'users',
|
45
|
+
:action => 'index', :conditions => { :foreign_domain => true }
|
46
|
+
|
47
|
+
And in the users controller:
|
48
|
+
|
49
|
+
@user = User.find_by_foreign_domain(request.host.downcase)
|
50
|
+
# this example would require a database field called foreign_domain
|
51
|
+
|
52
|
+
Configuration
|
53
|
+
=============
|
54
|
+
|
55
|
+
You will need to configure Foreign Domain Routing based on your native hostnames.
|
56
|
+
|
57
|
+
native_domains
|
58
|
+
--------
|
59
|
+
|
60
|
+
A hash of arrays for the native domain names for each relevant environment.
|
61
|
+
|
62
|
+
Create the file config/initializers/native_domains.rb and put something like:
|
63
|
+
|
64
|
+
ForeignDomainRouting.init_native_domains = {
|
65
|
+
:development => ['localhost'],
|
66
|
+
:staging => ['staging.example.com'],
|
67
|
+
:production => ['example.com', 'example.org', 'example.net']
|
68
|
+
} # set all at once (also the defaults)
|
69
|
+
|
70
|
+
|
71
|
+
Or set the native domains on the fly with:
|
72
|
+
|
73
|
+
ForeignDomainRouting.native_domains =
|
74
|
+
['example.com', 'example.org', 'example.net'] # sets for current environment
|
75
|
+
|
76
|
+
Resources
|
77
|
+
=========
|
78
|
+
|
79
|
+
* GitHub Repository: http://github.com/brianmulloy/foreign_domain_routing
|
80
|
+
* 2008 by Brian Mulloy (http://landlessness.net/). Released under the MIT license.
|
81
|
+
|
82
|
+
|
83
|
+
*******************************************************************************
|
84
|
+
|
85
|
+
-------------------------------------------------------------------------------
|
86
|
+
Request Routing Plugin for Ruby on Rails
|
87
|
+
-------------------------------------------------------------------------------
|
88
|
+
(c) Dan Webb 2006 (dan@vivabit.com)
|
89
|
+
|
90
|
+
Plugin that allows you to define routing conditions that test
|
91
|
+
methods/properties of the request object such as subdomain, domain,
|
92
|
+
port. You can test them either against a value or with a Regexp
|
93
|
+
(assuming the method returns a string)
|
94
|
+
|
95
|
+
*UPDATE* Now works with the new routing code as implemented in edge rails. Note the
|
96
|
+
change in API: use :conditions instead of :requirements.
|
97
|
+
|
98
|
+
== Installation
|
99
|
+
|
100
|
+
ruby script/plugin install http://svn.vivabit.net/external/rubylibs/request_routing/
|
101
|
+
|
102
|
+
== Usage
|
103
|
+
|
104
|
+
In routes.rb you can specify use the :requirements hash with request properties:
|
105
|
+
|
106
|
+
map.connect '', :controller => 'main', :conditions => { :subdomain => 'www' }
|
107
|
+
|
108
|
+
map.connect 'admin', :controller => 'admin', :conditions => { :remote_ip => /^127\.0\.0\.[0-9]$/ }
|
109
|
+
|
110
|
+
You can also, of course, use the conditions hash in map.with_options calls.
|
111
|
+
|
112
|
+
The allowed properties are:
|
113
|
+
|
114
|
+
:subdomain (only checks the first subdomain)
|
115
|
+
:domain (only accurate for single tld domain names at the moment)
|
116
|
+
:method (a symbol)
|
117
|
+
:port (a number)
|
118
|
+
:remote_ip
|
119
|
+
:content_type (content type of the post body)
|
120
|
+
:accepts
|
121
|
+
:request_uri (the entire request uri)
|
122
|
+
:protocol (either http:// or https://)
|
123
|
+
|
124
|
+
== Copyright
|
125
|
+
|
126
|
+
Copyright (c) 2009 Joe Scharf. See LICENSE for details.
|
data/VERSION.yml
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'foreign_domain_routing/routing_extensions'
|
2
|
+
|
3
|
+
module ForeignDomainRouting
|
4
|
+
DEFAULT_NATIVE_DOMAINS = {:development => ['localhost:3000'], :test => ['test.host'], :production => ['example.com'] }
|
5
|
+
mattr_accessor :init_native_domains
|
6
|
+
@@init_native_domains = DEFAULT_NATIVE_DOMAINS.dup
|
7
|
+
|
8
|
+
def self.native_domains
|
9
|
+
init_native_domains[RAILS_ENV.to_sym]
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.native_domains=(value)
|
13
|
+
init_native_domains[RAILS_ENV.to_sym] = value
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.foreign_domain?(host)
|
17
|
+
native_domains.each do |domain|
|
18
|
+
return false if host =~ /#{domain}\Z/i
|
19
|
+
end
|
20
|
+
true
|
21
|
+
end
|
22
|
+
|
23
|
+
module Controller
|
24
|
+
def self.included(controller)
|
25
|
+
controller.helper_method(:foreign_domain?)
|
26
|
+
end
|
27
|
+
|
28
|
+
protected
|
29
|
+
|
30
|
+
def foreign_domain?
|
31
|
+
ForeignDomainRouting.foreign_domain?(request.host)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module ForeignDomainRouting
|
2
|
+
module RouteExtensions
|
3
|
+
|
4
|
+
TESTABLE_REQUEST_METHODS = [:subdomain, :domain, :method, :port, :remote_ip,
|
5
|
+
:content_type, :accepts, :request_uri, :protocol]
|
6
|
+
|
7
|
+
def self.included(base)
|
8
|
+
base.alias_method_chain :recognition_conditions, :foreign_domain
|
9
|
+
end
|
10
|
+
|
11
|
+
def recognition_conditions_with_foreign_domain
|
12
|
+
result = recognition_conditions_without_foreign_domain
|
13
|
+
result << "ForeignDomainRouting.foreign_domain?(env[:host])" if conditions[:foreign_domain] == true
|
14
|
+
result << "!ForeignDomainRouting.foreign_domain?(env[:host])" if conditions[:foreign_domain] == false
|
15
|
+
|
16
|
+
conditions.each do |method, value|
|
17
|
+
if TESTABLE_REQUEST_METHODS.include? method
|
18
|
+
result << if value.is_a? Regexp
|
19
|
+
"conditions[#{method.inspect}] =~ env[#{method.inspect}]"
|
20
|
+
else
|
21
|
+
"conditions[#{method.inspect}] === env[#{method.inspect}]"
|
22
|
+
end
|
23
|
+
else
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
result
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
module RouteSetExtensions
|
33
|
+
def self.included(base)
|
34
|
+
base.alias_method_chain :extract_request_environment, :foreign_domain
|
35
|
+
end
|
36
|
+
|
37
|
+
def extract_request_environment_with_foreign_domain(request)
|
38
|
+
extract_request_environment_without_foreign_domain(request).merge({
|
39
|
+
:host => request.host,
|
40
|
+
:method => request.method,
|
41
|
+
:subdomain => request.subdomains.first.to_s,
|
42
|
+
:domain => request.domain,
|
43
|
+
:port => request.port,
|
44
|
+
:remote_ip => request.remote_ip,
|
45
|
+
:content_type => request.content_type,
|
46
|
+
:accepts => request.accepts.map(&:to_s).join(','),
|
47
|
+
:request_uri => request.request_uri,
|
48
|
+
:protocol => request.protocol
|
49
|
+
})
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
ActionController::Routing::RouteSet.send :include, ForeignDomainRouting::RouteSetExtensions
|
55
|
+
ActionController::Routing::Route.send :include, ForeignDomainRouting::RouteExtensions
|
@@ -0,0 +1,135 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
require File.dirname(__FILE__) + "/../init"
|
4
|
+
RAILS_ENV = :test
|
5
|
+
|
6
|
+
class TestController < Class.new(ActionController::Base)
|
7
|
+
def thing
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class OtherTestController < Class.new(ActionController::Base)
|
12
|
+
def thing
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class MockRequest < Struct.new(:path, :subdomains, :method, :remote_ip, :protocol, :path_parameters, :domain, :port, :content_type, :accepts, :request_uri, :host)
|
17
|
+
end
|
18
|
+
|
19
|
+
class ForeignDomainRoutingTest < ActionController::TestCase
|
20
|
+
attr_reader :rs
|
21
|
+
def setup
|
22
|
+
@rs = ::ActionController::Routing::RouteSet.new
|
23
|
+
ActionController::Routing.use_controllers! %w(test) if ActionController::Routing.respond_to? :use_controllers!
|
24
|
+
@rs.draw {|m| m.connect ':controller/:action/:id' }
|
25
|
+
@request = MockRequest.new(
|
26
|
+
'',
|
27
|
+
['www'],
|
28
|
+
:post,
|
29
|
+
'1.2.3.4',
|
30
|
+
'http://',
|
31
|
+
'',
|
32
|
+
'thing.com',
|
33
|
+
3432,
|
34
|
+
'text/html',
|
35
|
+
['*/*'],
|
36
|
+
'/',
|
37
|
+
'www.example.com'
|
38
|
+
)
|
39
|
+
end
|
40
|
+
|
41
|
+
test "should route normally" do
|
42
|
+
assert_raise(ActionController::RoutingError) do
|
43
|
+
@rs.recognize(@request)
|
44
|
+
end
|
45
|
+
|
46
|
+
@request.path = '/test/thing'
|
47
|
+
assert(@rs.recognize(@request))
|
48
|
+
end
|
49
|
+
|
50
|
+
test "should route conditionally on subdomain" do
|
51
|
+
@rs.draw { |m| m.connect 'thing', :controller => 'test', :conditions => { :subdomain => 'www' } }
|
52
|
+
@request.path = '/thing'
|
53
|
+
assert(@rs.recognize(@request))
|
54
|
+
@request.subdomains = ['sdkg']
|
55
|
+
assert_raise(ActionController::RoutingError) do
|
56
|
+
@rs.recognize(@request)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
test "should route conditionally on protocol" do
|
61
|
+
@rs.draw { |m| m.connect 'thing', :controller => 'test', :conditions => { :protocol => /^https:/ } }
|
62
|
+
@request.path = '/thing'
|
63
|
+
assert_raise(ActionController::RoutingError) do
|
64
|
+
@rs.recognize(@request)
|
65
|
+
end
|
66
|
+
|
67
|
+
@request.protocol = "https://"
|
68
|
+
assert(@rs.recognize(@request))
|
69
|
+
end
|
70
|
+
|
71
|
+
test "should route conditionally on alternate conditionals" do
|
72
|
+
@rs.draw { |m|
|
73
|
+
m.connect 'thing', :controller => 'test', :conditions => { :remote_ip => '1.2.3.4' }
|
74
|
+
m.connect 'thing', :controller => 'other_test', :conditions => { :remote_ip => '1.2.3.5' }
|
75
|
+
}
|
76
|
+
|
77
|
+
@request.path = '/thing'
|
78
|
+
assert(@rs.recognize(@request))
|
79
|
+
|
80
|
+
@request.remote_ip = '1.2.3.5'
|
81
|
+
assert(@rs.recognize(@request))
|
82
|
+
end
|
83
|
+
|
84
|
+
test "should route conditionally on foreign domain" do
|
85
|
+
ForeignDomainRouting.init_native_domains = {
|
86
|
+
:development => ['localhost'],
|
87
|
+
:test => ['www.example.com'],
|
88
|
+
:production => ['example.com', 'example.org', 'example.net']
|
89
|
+
}
|
90
|
+
|
91
|
+
@rs.draw { |m| m.connect 'thing', :controller => 'test', :conditions => { :foreign_domain => false } }
|
92
|
+
@request.path = '/thing'
|
93
|
+
assert(@rs.recognize(@request))
|
94
|
+
@request.host = ['foreign.domain.com']
|
95
|
+
assert_raise(ActionController::RoutingError) do
|
96
|
+
@rs.recognize(@request)
|
97
|
+
end
|
98
|
+
@rs.draw { |m| m.connect 'thing', :controller => 'test', :conditions => { :foreign_domain => true } }
|
99
|
+
@request.path = '/thing'
|
100
|
+
assert(@rs.recognize(@request))
|
101
|
+
end
|
102
|
+
|
103
|
+
test "should route conditionally on foreign domain and protocol" do
|
104
|
+
ForeignDomainRouting.init_native_domains = {
|
105
|
+
:development => ['localhost'],
|
106
|
+
:test => ['www.example.com'],
|
107
|
+
:production => ['example.com', 'example.org', 'example.net']
|
108
|
+
}
|
109
|
+
|
110
|
+
@rs.draw { |m| m.connect 'thing', :controller => 'test', :conditions => { :foreign_domain => false, :protocol => /^http:/ } }
|
111
|
+
@request.path = '/thing'
|
112
|
+
# :foreign_domain => false, :protocol => http:// (MATCH)
|
113
|
+
assert(@rs.recognize(@request))
|
114
|
+
|
115
|
+
# :foreign_domain => false, :protocol => https:// (NO MATCH)
|
116
|
+
@request.protocol = "https://"
|
117
|
+
assert_raise(ActionController::RoutingError) do
|
118
|
+
@rs.recognize(@request)
|
119
|
+
end
|
120
|
+
|
121
|
+
# :foreign_domain => true, :protocol => http:// (NO MATCH)
|
122
|
+
@request.host = ['foreign.domain.com']
|
123
|
+
@request.protocol = "http://"
|
124
|
+
assert_raise(ActionController::RoutingError) do
|
125
|
+
@rs.recognize(@request)
|
126
|
+
end
|
127
|
+
|
128
|
+
# :foreign_domain => true, :protocol => https:// (NO MATCH)
|
129
|
+
@request.protocol = "https://"
|
130
|
+
assert_raise(ActionController::RoutingError) do
|
131
|
+
@rs.recognize(@request)
|
132
|
+
end
|
133
|
+
|
134
|
+
end
|
135
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'shoulda'
|
4
|
+
require 'action_controller'
|
5
|
+
|
6
|
+
|
7
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
8
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
9
|
+
require 'foreign_domain_routing'
|
10
|
+
|
11
|
+
class Test::Unit::TestCase
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: quantipay-foreign-domain-routing
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joe Scharf
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-04-05 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: joe@quantipay.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
files:
|
25
|
+
- README.rdoc
|
26
|
+
- VERSION.yml
|
27
|
+
- lib/foreign_domain_routing
|
28
|
+
- lib/foreign_domain_routing/routing_extensions.rb
|
29
|
+
- lib/foreign_domain_routing.rb
|
30
|
+
- test/foreign_domain_routing_test.rb
|
31
|
+
- test/test_helper.rb
|
32
|
+
has_rdoc: true
|
33
|
+
homepage: http://github.com/quantipay/foreign-domain-routing-gem
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options:
|
36
|
+
- --inline-source
|
37
|
+
- --charset=UTF-8
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
version:
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
requirements: []
|
53
|
+
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 1.2.0
|
56
|
+
signing_key:
|
57
|
+
specification_version: 2
|
58
|
+
summary: This version of foreign_domain_routing merges foreign_domain_routing with request_routing
|
59
|
+
test_files: []
|
60
|
+
|