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
This commit is contained in:
David Heinemeier Hansson 2007-11-07 14:57:51 +00:00
parent f770b829f4
commit 31e2a2d9bb
3 changed files with 23 additions and 2 deletions

View File

@ -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]

View File

@ -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 <tt>tld_length</tt>, 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 <tt>tld_length</tt>, 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?

View File

@ -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