From 31e2a2d9bbe3a375912223c133ba793b72600a9f Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 7 Nov 2007 14:57:51 +0000 Subject: Fixed handling of non-domain hosts (closes #9479) [purp] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8108 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 2 ++ actionpack/lib/action_controller/request.rb | 8 ++++++-- actionpack/test/controller/request_test.rb | 15 +++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index e75a65e3f4..0538943f3b 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fixed handling of non-domain hosts #9479 [purp] + * Fix syntax error in documentation example for cycle method. Closes #8735 [foca] * Document :with option for link_to_remote. Closes #8765 [ryanb] diff --git a/actionpack/lib/action_controller/request.rb b/actionpack/lib/action_controller/request.rb index 935e3e08ae..8cfb6d3354 100755 --- a/actionpack/lib/action_controller/request.rb +++ b/actionpack/lib/action_controller/request.rb @@ -191,7 +191,7 @@ module ActionController # Returns the domain part of a host, such as rubyonrails.org in "www.rubyonrails.org". You can specify # a different tld_length, such as 2 to catch rubyonrails.co.uk in "www.rubyonrails.co.uk". def domain(tld_length = 1) - return nil if !/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/.match(host).nil? or host.nil? + return nil unless named_host?(host) host.split('.').last(1 + tld_length).join('.') end @@ -200,7 +200,7 @@ module ActionController # 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) - return [] unless host + return [] unless named_host?(host) parts = host.split('.') parts[0..-(tld_length+2)] end @@ -387,6 +387,10 @@ module ActionController "backtrace" => e.backtrace } end + def named_host?(host) + !(host.nil? || /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host)) + end + class << self def parse_query_parameters(query_string) return {} if query_string.blank? diff --git a/actionpack/test/controller/request_test.rb b/actionpack/test/controller/request_test.rb index 27b262d994..0fc8889db9 100644 --- a/actionpack/test/controller/request_test.rb +++ b/actionpack/test/controller/request_test.rb @@ -51,6 +51,12 @@ class RequestTest < Test::Unit::TestCase @request.host = "192.168.1.200" assert_nil @request.domain + @request.host = "foo.192.168.1.200" + assert_nil @request.domain + + @request.host = "192.168.1.200.com" + assert_equal "200.com", @request.domain + @request.host = nil assert_nil @request.domain end @@ -68,6 +74,15 @@ class RequestTest < Test::Unit::TestCase @request.host = "foobar.foobar.com" assert_equal %w( foobar ), @request.subdomains + @request.host = "192.168.1.200" + assert_equal [], @request.subdomains + + @request.host = "foo.192.168.1.200" + assert_equal [], @request.subdomains + + @request.host = "192.168.1.200.com" + assert_equal %w( 192 168 1 ), @request.subdomains + @request.host = nil assert_equal [], @request.subdomains end -- cgit v1.2.3