Skip to content

Commit 88605d0

Browse files
committed
Merge pull request jaswope#3 from waterlink/neopoly/host_port
Omit port in Host header for default ports
2 parents 80a5f7f + 026fd7e commit 88605d0

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

lib/rack/reverse_proxy.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ def proxy(env, source_request, matcher)
4747
target_request_headers = extract_http_request_headers(source_request.env)
4848

4949
if options[:preserve_host]
50-
target_request_headers['HOST'] = "#{uri.host}:#{uri.port}"
50+
if uri.port == uri.default_port
51+
target_request_headers['HOST'] = uri.host
52+
else
53+
target_request_headers['HOST'] = "#{uri.host}:#{uri.port}"
54+
end
5155
end
5256

5357
if options[:x_forwarded_host]

spec/rack/reverse_proxy_spec.rb

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ def app
3737
last_response.body.should == "Proxied App2"
3838
end
3939

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

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

52+
describe "with non-default port" do
53+
def app
54+
Rack::ReverseProxy.new(dummy_app) do
55+
reverse_proxy '/test', 'http://example.com:8080/'
56+
end
57+
end
58+
59+
it "should set the Host header including non-default port" do
60+
stub_request(:any, 'example.com:8080/test/stuff')
61+
get '/test/stuff'
62+
a_request(:get, 'http://example.com:8080/test/stuff').with(:headers => {"Host" => "example.com:8080"}).should have_been_made
63+
end
64+
end
65+
5266
describe "with preserve host turned off" do
5367
def app
5468
Rack::ReverseProxy.new(dummy_app) do
@@ -194,6 +208,25 @@ def app
194208
last_response.body.should == "Proxied Secure App"
195209
end
196210

211+
it "should set the Host header w/o default port" do
212+
stub_request(:any, 'https://example.com/test/stuff')
213+
get '/test/stuff'
214+
a_request(:get, 'https://example.com/test/stuff').with(:headers => {"Host" => "example.com"}).should have_been_made
215+
end
216+
end
217+
218+
describe "with a https route on non-default port" do
219+
def app
220+
Rack::ReverseProxy.new(dummy_app) do
221+
reverse_proxy '/test', 'https://example.com:8443'
222+
end
223+
end
224+
225+
it "should set the Host header including non-default port" do
226+
stub_request(:any, 'https://example.com:8443/test/stuff')
227+
get '/test/stuff'
228+
a_request(:get, 'https://example.com:8443/test/stuff').with(:headers => {"Host" => "example.com:8443"}).should have_been_made
229+
end
197230
end
198231

199232
describe "with a route as a string" do

0 commit comments

Comments
 (0)