Permalink
Browse files

Fixed proxy support for lighttpd (closes #3267) [stephen_purcell@yaho…

…o.com]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3894 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 065b3af commit 4e15bf787e80f3a7ad1d3fa31338eff2cfbdd920 @dhh dhh committed Mar 16, 2006
Showing with 38 additions and 15 deletions.
  1. +19 −13 actionpack/lib/action_controller/cgi_process.rb
  2. +19 −2 actionpack/test/controller/cgi_test.rb
@@ -75,22 +75,28 @@ def cookies
@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
@@ -330,9 +330,27 @@ def test_http_host
@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 @@ def test_cookie_syntax_resilience
assert_equal ["c84ace84796670c052c6ceb2451fb0f2"], alt_cookies["_session_id"]
assert_equal ["yes"], alt_cookies["is_admin"]
end
-
end

0 comments on commit 4e15bf7

Please sign in to comment.