diff options
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_controller/cgi_process.rb | 32 | ||||
-rwxr-xr-x | actionpack/test/controller/cgi_test.rb | 21 |
2 files changed, 38 insertions, 15 deletions
diff --git a/actionpack/lib/action_controller/cgi_process.rb b/actionpack/lib/action_controller/cgi_process.rb index 508dc016ac..b710f05d7c 100644 --- a/actionpack/lib/action_controller/cgi_process.rb +++ b/actionpack/lib/action_controller/cgi_process.rb @@ -75,22 +75,28 @@ module ActionController #:nodoc: @cgi.cookies.freeze end + def host_with_port + if forwarded = env["HTTP_X_FORWARDED_HOST"] + forwarded.split(/,\s?/).last + elsif http_host = env['HTTP_HOST'] + http_host + elsif server_name = env['SERVER_NAME'] + server_name + else + "#{env['SERVER_ADDR']}:#{env['SERVER_PORT']}" + end + end + def host - if @env["HTTP_X_FORWARDED_HOST"] - @env["HTTP_X_FORWARDED_HOST"].split(/,\s?/).last - elsif @env['HTTP_HOST'] =~ /^(.*):\d+$/ - $1 - else - @cgi.host.to_s.split(":").first || '' - end + host_with_port[/^[^:]+/] end - + def port - @env["HTTP_X_FORWARDED_HOST"] ? standard_port : (port_from_http_host || super) - end - - def port_from_http_host - $1.to_i if @env['HTTP_HOST'] && /:(\d+)$/ =~ @env['HTTP_HOST'] + if host_with_port =~ /:(\d+)$/ + $1.to_i + else + standard_port + end end def session diff --git a/actionpack/test/controller/cgi_test.rb b/actionpack/test/controller/cgi_test.rb index a38f2519ec..ddf68fdf5b 100755 --- a/actionpack/test/controller/cgi_test.rb +++ b/actionpack/test/controller/cgi_test.rb @@ -330,9 +330,27 @@ class CGIRequestTest < Test::Unit::TestCase @request_hash['HTTP_X_FORWARDED_HOST'] = "www.firsthost.org, www.secondhost.org" assert_equal "www.secondhost.org", @request.host - end + def test_http_host_with_default_port_overrides_server_port + @request_hash.delete "HTTP_X_FORWARDED_HOST" + @request_hash['HTTP_HOST'] = "rubyonrails.org" + assert_equal "rubyonrails.org", @request.host_with_port + end + + def test_host_with_port_defaults_to_server_name_if_no_host_headers + @request_hash.delete "HTTP_X_FORWARDED_HOST" + @request_hash.delete "HTTP_HOST" + assert_equal "glu.ttono.us:8007", @request.host_with_port + end + + def test_host_with_port_falls_back_to_server_addr_if_necessary + @request_hash.delete "HTTP_X_FORWARDED_HOST" + @request_hash.delete "HTTP_HOST" + @request_hash.delete "SERVER_NAME" + assert_equal "207.7.108.53:8007", @request.host_with_port + end + def test_cookie_syntax_resilience cookies = CGI::Cookie::parse(@request_hash["HTTP_COOKIE"]); assert_equal ["c84ace84796670c052c6ceb2451fb0f2"], cookies["_session_id"] @@ -342,5 +360,4 @@ class CGIRequestTest < Test::Unit::TestCase assert_equal ["c84ace84796670c052c6ceb2451fb0f2"], alt_cookies["_session_id"] assert_equal ["yes"], alt_cookies["is_admin"] end - end |