Skip to content

Commit

Permalink
Add configuration option for tld length
Browse files Browse the repository at this point in the history
  • Loading branch information
simonjefford authored and wycats committed Sep 10, 2010
1 parent 5949e70 commit 7fae0aa
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
10 changes: 6 additions & 4 deletions actionpack/lib/action_dispatch/http/url.rb
@@ -1,7 +1,9 @@
module ActionDispatch
module Http
module URL
# Returns the complete \URL used for this request.
mattr_accessor :tld_length

# Returns the complete URL used for this request.
def url
protocol + host_with_port + fullpath
end
Expand Down Expand Up @@ -85,13 +87,13 @@ def domain(tld_length = 1)
# returned for "dev.www.rubyonrails.org". You can specify a different <tt>tld_length</tt>,
# such as 2 to catch <tt>["www"]</tt> instead of <tt>["www", "rubyonrails"]</tt>
# in "www.rubyonrails.co.uk".
def subdomains(tld_length = 1)
def subdomains(tld_length = @@tld_length)
return [] unless named_host?(host)
parts = host.split('.')
parts[0..-(tld_length+2)]
end

def subdomain(tld_length = 1)
def subdomain(tld_length = @@tld_length)
subdomains(tld_length).join('.')
end

Expand All @@ -102,4 +104,4 @@ def named_host?(host)
end
end
end
end
end
10 changes: 10 additions & 0 deletions actionpack/lib/action_dispatch/railtie.rb
Expand Up @@ -8,5 +8,15 @@ class Railtie < Rails::Railtie
config.action_dispatch.ip_spoofing_check = true
config.action_dispatch.show_exceptions = true
config.action_dispatch.best_standards_support = true
config.action_dispatch.tld_length = 1

# Prepare dispatcher callbacks and run 'prepare' callbacks
initializer "action_dispatch.prepare_dispatcher" do |app|
ActionDispatch::Callbacks.to_prepare { app.routes_reloader.execute_if_updated }
end

This comment has been minimized.

Copy link
@simonjefford

simonjefford Sep 11, 2010

Author Contributor

Looks like I made a mistake here when rebasing my patch. The action_dispatch.prepare_dispatcher initializer was removed in 48bf667 and probably needs to be taken out again. Sorry...

This comment has been minimized.

Copy link
@mikel

mikel Sep 11, 2010

Member

Thanks for alerting us, I'll fix this now

This comment has been minimized.

initializer "action_dispatch.configure" do |app|
ActionDispatch::Http::URL.tld_length = app.config.action_dispatch.tld_length
end
end
end
5 changes: 5 additions & 0 deletions actionpack/test/dispatch/request_test.rb
Expand Up @@ -116,6 +116,9 @@ class RequestTest < ActiveSupport::TestCase
request = stub_request 'HTTP_HOST' => "dev.www.rubyonrails.co.uk"
assert_equal %w( dev www ), request.subdomains(2)

request = stub_request 'HTTP_HOST' => "dev.www.rubyonrails.co.uk", :tld_length => 2
assert_equal %w( dev www ), request.subdomains

request = stub_request 'HTTP_HOST' => "foobar.foobar.com"
assert_equal %w( foobar ), request.subdomains

Expand Down Expand Up @@ -472,7 +475,9 @@ class RequestTest < ActiveSupport::TestCase
def stub_request(env = {})
ip_spoofing_check = env.key?(:ip_spoofing_check) ? env.delete(:ip_spoofing_check) : true
ip_app = ActionDispatch::RemoteIp.new(Proc.new { }, ip_spoofing_check, @trusted_proxies)
tld_length = env.key?(:tld_length) ? env.delete(:tld_length) : 1
ip_app.call(env)
ActionDispatch::Http::URL.tld_length = tld_length
ActionDispatch::Request.new(env)
end

Expand Down
7 changes: 7 additions & 0 deletions railties/test/application/initializers/frameworks_test.rb
Expand Up @@ -65,6 +65,13 @@ def notify
assert_equal ["notify"], Foo.action_methods
end

# AD
test "action_dispatch extensions are applied to ActionDispatch" do
add_to_config "config.action_dispatch.tld_length = 2"
require "#{app_path}/config/environment"
assert_equal 2, ActionDispatch::Http::URL.tld_length
end

# AS
test "if there's no config.active_support.bare, all of ActiveSupport is required" do
use_frameworks []
Expand Down

0 comments on commit 7fae0aa

Please sign in to comment.