fix url helper for reverse proxies. fixes #186.

This commit is contained in:
Konstantin Haase 2011-02-19 22:32:29 +01:00
parent 6dc3cd773b
commit fae7c01166
2 changed files with 26 additions and 1 deletions

View File

@ -29,6 +29,10 @@ module Sinatra
alias secure? ssl?
end
def forwarded?
@env.include? "HTTP_X_FORWARDED_HOST"
end
def route
@route ||= begin
path = Rack::Utils.unescape(path_info)
@ -104,7 +108,16 @@ module Sinatra
def uri(addr = nil, absolute = true, add_script_name = true)
return addr if addr =~ /^https?:\/\//
uri = [host = ""]
uri << request.host_with_port if absolute
if absolute
host << 'http'
host << 's' if request.secure?
host << "://"
if request.forwarded? or request.port != (request.secure? ? 443 : 80)
host << request.host_with_port
else
host << request.host
end
end
uri << request.script_name.to_s if add_script_name
uri << (addr ? addr : request.path_info).to_s
File.join uri

View File

@ -111,6 +111,18 @@ class HelpersTest < Test::Unit::TestCase
response = request.get('/', 'SERVER_PORT' => '444')
assert_equal 'http://example.org:444/foo', response['Location']
end
it 'works behind a reverse proxy' do
mock_app do
get '/' do
redirect '/foo'
end
end
request = Rack::MockRequest.new(@app)
response = request.get('/', 'HTTP_X_FORWARDED_HOST' => 'example.com', 'SERVER_PORT' => '8080')
assert_equal 'http://example.com/foo', response['Location']
end
end
describe 'error' do