Skip to content

Commit

Permalink
Omit port in Host header for default ports
Browse files Browse the repository at this point in the history
Setting default port in Host header break some web servers
like "Apache Coyote".

Most clients don't set this default port too:
* request/request#515 (comment)
* https://github.com/ruby/ruby/blob/d46336e71731aa64d71d4573b2741b7de43ec340/lib/net/http/generic_request.rb#L18
  • Loading branch information
Peter Suschlik authored and splattael committed Jun 9, 2015
1 parent 859f10d commit 65fca49
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
6 changes: 5 additions & 1 deletion lib/rack/reverse_proxy.rb
Expand Up @@ -46,7 +46,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 @@ -166,6 +180,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

0 comments on commit 65fca49

Please sign in to comment.