Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Omit port in Host header for default ports #3

Merged
merged 1 commit into from Jul 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/rack/reverse_proxy.rb
Expand Up @@ -47,7 +47,11 @@ def proxy(env, source_request, matcher)
target_request_headers = extract_http_request_headers(source_request.env)

if options[:preserve_host]
target_request_headers['HOST'] = "#{uri.host}:#{uri.port}"
if uri.port == uri.default_port
target_request_headers['HOST'] = uri.host
else
target_request_headers['HOST'] = "#{uri.host}:#{uri.port}"
end
end

if options[:x_forwarded_host]
Expand Down
37 changes: 35 additions & 2 deletions spec/rack/reverse_proxy_spec.rb
Expand Up @@ -37,10 +37,10 @@ def app
last_response.body.should == "Proxied App2"
end

it "should set the Host header" do
it "should set the Host header w/o default port" do
stub_request(:any, 'example.com/test/stuff')
get '/test/stuff'
a_request(:get, 'http://example.com/test/stuff').with(:headers => {"Host" => "example.com:80"}).should have_been_made
a_request(:get, 'http://example.com/test/stuff').with(:headers => {"Host" => "example.com"}).should have_been_made
end

it "should set the X-Forwarded-Host header to the proxying host by default" do
Expand All @@ -49,6 +49,20 @@ def app
a_request(:get, 'http://example.com/test/stuff').with(:headers => {'X-Forwarded-Host' => 'example.org'}).should have_been_made
end

describe "with non-default port" do
def app
Rack::ReverseProxy.new(dummy_app) do
reverse_proxy '/test', 'http://example.com:8080/'
end
end

it "should set the Host header including non-default port" do
stub_request(:any, 'example.com:8080/test/stuff')
get '/test/stuff'
a_request(:get, 'http://example.com:8080/test/stuff').with(:headers => {"Host" => "example.com:8080"}).should have_been_made
end
end

describe "with preserve host turned off" do
def app
Rack::ReverseProxy.new(dummy_app) do
Expand Down Expand Up @@ -194,6 +208,25 @@ def app
last_response.body.should == "Proxied Secure App"
end

it "should set the Host header w/o default port" do
stub_request(:any, 'https://example.com/test/stuff')
get '/test/stuff'
a_request(:get, 'https://example.com/test/stuff').with(:headers => {"Host" => "example.com"}).should have_been_made
end
end

describe "with a https route on non-default port" do
def app
Rack::ReverseProxy.new(dummy_app) do
reverse_proxy '/test', 'https://example.com:8443'
end
end

it "should set the Host header including non-default port" do
stub_request(:any, 'https://example.com:8443/test/stuff')
get '/test/stuff'
a_request(:get, 'https://example.com:8443/test/stuff').with(:headers => {"Host" => "example.com:8443"}).should have_been_made
end
end

describe "with a route as a string" do
Expand Down