From 7c91f2d4fce4991d776cfe42d2fe2bcb9f6874f2 Mon Sep 17 00:00:00 2001 From: Simon Jefford Date: Tue, 27 Jul 2010 15:39:28 +0100 Subject: [PATCH] Add configuration option for tld length --- actionpack/lib/action_dispatch/http/url.rb | 6 ++++-- actionpack/lib/action_dispatch/railtie.rb | 5 +++++ actionpack/test/dispatch/request_test.rb | 5 +++++ .../application/initializers/frameworks_test.rb | 7 +++++++ 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/actionpack/lib/action_dispatch/http/url.rb b/actionpack/lib/action_dispatch/http/url.rb index b64a83c..1cde0d9 100644 --- a/actionpack/lib/action_dispatch/http/url.rb +++ b/actionpack/lib/action_dispatch/http/url.rb @@ -1,6 +1,8 @@ module ActionDispatch module Http module URL + mattr_accessor :tld_length + # Returns the complete URL used for this request. def url protocol + host_with_port + fullpath @@ -75,13 +77,13 @@ module ActionDispatch # returned for "dev.www.rubyonrails.org". You can specify a different tld_length, # such as 2 to catch ["www"] instead of ["www", "rubyonrails"] # 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 diff --git a/actionpack/lib/action_dispatch/railtie.rb b/actionpack/lib/action_dispatch/railtie.rb index a3af379..c202fee 100644 --- a/actionpack/lib/action_dispatch/railtie.rb +++ b/actionpack/lib/action_dispatch/railtie.rb @@ -8,10 +8,15 @@ module ActionDispatch 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 + + initializer "action_dispatch.configure" do |app| + ActionDispatch::Http::URL.tld_length = app.config.action_dispatch.tld_length + end end end diff --git a/actionpack/test/dispatch/request_test.rb b/actionpack/test/dispatch/request_test.rb index c8947aa..d329777 100644 --- a/actionpack/test/dispatch/request_test.rb +++ b/actionpack/test/dispatch/request_test.rb @@ -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 @@ -436,7 +439,9 @@ protected 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 diff --git a/railties/test/application/initializers/frameworks_test.rb b/railties/test/application/initializers/frameworks_test.rb index d8e916f..079fbb1 100644 --- a/railties/test/application/initializers/frameworks_test.rb +++ b/railties/test/application/initializers/frameworks_test.rb @@ -64,6 +64,13 @@ module ApplicationTests 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 [] -- 1.7.1.1