aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorNicholas Seckar <nseckar@gmail.com>2005-10-15 01:00:25 +0000
committerNicholas Seckar <nseckar@gmail.com>2005-10-15 01:00:25 +0000
commite0e8f0a3027f3777b961df9f9164d0bdd2fc0fbd (patch)
tree969f515f768cc0caf7537501c557dafd290fe7f4 /actionpack
parent70869931611667d5b732ae91704313115e4baa10 (diff)
downloadrails-e0e8f0a3027f3777b961df9f9164d0bdd2fc0fbd.tar.gz
rails-e0e8f0a3027f3777b961df9f9164d0bdd2fc0fbd.tar.bz2
rails-e0e8f0a3027f3777b961df9f9164d0bdd2fc0fbd.zip
Fix Request#host_with_port to use the standard port when Rails is behind a proxy.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2596 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/CHANGELOG2
-rw-r--r--actionpack/lib/action_controller/cgi_process.rb10
-rwxr-xr-xactionpack/lib/action_controller/request.rb12
-rwxr-xr-xactionpack/test/controller/cgi_test.rb25
-rw-r--r--actionpack/test/controller/request_test.rb4
5 files changed, 46 insertions, 7 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index 0e20b46fbb..c44a98355f 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Fix Request#host_with_port to use the standard port when Rails is behind a proxy. [Nicholas Seckar]
+
* Escape query strings in the href attribute of URLs created by url_helper. #2333 [Michael Schuerig <michael@schuerig.de>]
* Improved line number reporting for template errors [Nicholas Seckar]
diff --git a/actionpack/lib/action_controller/cgi_process.rb b/actionpack/lib/action_controller/cgi_process.rb
index 4413a29791..7f05e21cb8 100644
--- a/actionpack/lib/action_controller/cgi_process.rb
+++ b/actionpack/lib/action_controller/cgi_process.rb
@@ -79,7 +79,15 @@ module ActionController #:nodoc:
end
def host
- env["HTTP_X_FORWARDED_HOST"] || @cgi.host.to_s.split(":").first || ''
+ env["HTTP_X_FORWARDED_HOST"] || ($1 if env['HTTP_HOST'] && /^(.*):\d+$/ =~ env['HTTP_HOST']) || @cgi.host.to_s.split(":").first || ''
+ 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']
end
def session
diff --git a/actionpack/lib/action_controller/request.rb b/actionpack/lib/action_controller/request.rb
index e5dc424476..aace415943 100755
--- a/actionpack/lib/action_controller/request.rb
+++ b/actionpack/lib/action_controller/request.rb
@@ -174,17 +174,25 @@ module ActionController
def port
@port_as_int ||= env['SERVER_PORT'].to_i
end
+
+ # Returns the standard port number for this request's protocol
+ def standard_port
+ case protocol
+ when 'https://' then 443
+ else 80
+ end
+ end
# Returns a port suffix like ":8080" if the port number of this request
# is not the default HTTP port 80 or HTTPS port 443.
def port_string
- (protocol == 'http://' && port == 80) || (protocol == 'https://' && port == 443) ? '' : ":#{port}"
+ (port == standard_port) ? '' : ":#{port}"
end
# Returns a host:port string for this request, such as example.com or
# example.com:8080.
def host_with_port
- env['HTTP_HOST'] || host + port_string
+ host + port_string
end
def path_parameters=(parameters)
diff --git a/actionpack/test/controller/cgi_test.rb b/actionpack/test/controller/cgi_test.rb
index 0ad0b889d7..02fd133988 100755
--- a/actionpack/test/controller/cgi_test.rb
+++ b/actionpack/test/controller/cgi_test.rb
@@ -1,4 +1,9 @@
require File.dirname(__FILE__) + '/../abstract_unit'
+require 'action_controller/cgi_process'
+require 'action_controller/cgi_ext/cgi_ext'
+
+
+require 'stringio'
class CGITest < Test::Unit::TestCase
def setup
@@ -303,3 +308,23 @@ class MultipartCGITest < Test::Unit::TestCase
$stdin = old_stdin
end
end
+
+
+class CGIRequestTest < Test::Unit::TestCase
+ def setup
+ @request_hash = {"HTTP_MAX_FORWARDS"=>"10", "SERVER_NAME"=>"glu.ttono.us:8007", "FCGI_ROLE"=>"RESPONDER", "HTTP_X_FORWARDED_HOST"=>"glu.ttono.us", "HTTP_ACCEPT_ENCODING"=>"gzip, deflate", "HTTP_USER_AGENT"=>"Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/312.5.1 (KHTML, like Gecko) Safari/312.3.1", "PATH_INFO"=>"", "HTTP_ACCEPT_LANGUAGE"=>"en", "HTTP_HOST"=>"glu.ttono.us:8007", "SERVER_PROTOCOL"=>"HTTP/1.1", "REDIRECT_URI"=>"/dispatch.fcgi", "SCRIPT_NAME"=>"/dispatch.fcgi", "SERVER_ADDR"=>"207.7.108.53", "REMOTE_ADDR"=>"207.7.108.53", "SERVER_SOFTWARE"=>"lighttpd/1.4.5", "HTTP_COOKIE"=>"_session_id=c84ace84796670c052c6ceb2451fb0f2; is_admin=yes", "HTTP_X_FORWARDED_SERVER"=>"glu.ttono.us", "REQUEST_URI"=>"/admin", "DOCUMENT_ROOT"=>"/home/kevinc/sites/typo/public", "SERVER_PORT"=>"8007", "QUERY_STRING"=>"", "REMOTE_PORT"=>"63137", "GATEWAY_INTERFACE"=>"CGI/1.1", "HTTP_X_FORWARDED_FOR"=>"65.88.180.234", "HTTP_ACCEPT"=>"*/*", "SCRIPT_FILENAME"=>"/home/kevinc/sites/typo/public/dispatch.fcgi", "REDIRECT_STATUS"=>"200", "REQUEST_METHOD"=>"GET"}
+ @fake_cgi = Struct.new(:env_table).new(@request_hash)
+ @request = ActionController::CgiRequest.new(@fake_cgi)
+ end
+
+ def test_proxy_request
+ assert_equal 'glu.ttono.us', @request.host_with_port
+ end
+
+ def test_http_host
+ @request_hash.delete "HTTP_X_FORWARDED_HOST"
+ @request_hash['HTTP_HOST'] = "rubyonrails.org:8080"
+ assert_equal "rubyonrails.org:8080", @request.host_with_port
+ end
+
+end
diff --git a/actionpack/test/controller/request_test.rb b/actionpack/test/controller/request_test.rb
index 49ce7b80bb..ecc056960c 100644
--- a/actionpack/test/controller/request_test.rb
+++ b/actionpack/test/controller/request_test.rb
@@ -201,10 +201,6 @@ class RequestTest < Test::Unit::TestCase
def test_host_with_port
- @request.env['HTTP_HOST'] = "rubyonrails.org:8080"
- assert_equal "rubyonrails.org:8080", @request.host_with_port
- @request.env['HTTP_HOST'] = nil
-
@request.host = "rubyonrails.org"
@request.port = 80
assert_equal "rubyonrails.org", @request.host_with_port