Skip to content

Commit 611c775

Browse files
committed
Merge pull request #12 from jjthrash/master
Add support for X-Forwarded-Host header
2 parents c174271 + 93c91f5 commit 611c775

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

lib/rack/reverse_proxy.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class ReverseProxy
66
def initialize(app = nil, &b)
77
@app = app || lambda {|env| [404, [], []] }
88
@matchers = []
9-
@global_options = {:preserve_host => true, :matching => :all, :verify_ssl => true}
9+
@global_options = {:preserve_host => true, :x_forwarded_host => true, :matching => :all, :verify_ssl => true}
1010
instance_eval &b if block_given?
1111
end
1212

@@ -24,7 +24,8 @@ def call(env)
2424
end
2525
}
2626
headers['HOST'] = uri.host if all_opts[:preserve_host]
27-
27+
headers['X-Forwarded-Host'] = rackreq.host if all_opts[:x_forwarded_host]
28+
2829
session = Net::HTTP.new(uri.host, uri.port)
2930
session.read_timeout=all_opts[:timeout] if all_opts[:timeout]
3031

spec/rack/reverse_proxy_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ def app
5656
a_request(:get, 'http://example.com/test/stuff').with(:headers => {"Host" => "example.com"}).should have_been_made
5757
end
5858

59+
it "should set the X-Forwarded-Host header to the proxying host by default" do
60+
stub_request(:any, 'example.com/test/stuff')
61+
get '/test/stuff'
62+
a_request(:get, 'http://example.com/test/stuff').with(:headers => {'X-Forwarded-Host' => 'example.org'}).should have_been_made
63+
end
64+
5965
describe "with preserve host turned off" do
6066
def app
6167
Rack::ReverseProxy.new(dummy_app) do
@@ -71,6 +77,22 @@ def app
7177
end
7278
end
7379

80+
describe "with x_forwarded_host turned off" do
81+
def app
82+
Rack::ReverseProxy.new(dummy_app) do
83+
reverse_proxy_options :x_forwarded_host => false
84+
reverse_proxy '/test', 'http://example.com/'
85+
end
86+
end
87+
88+
it "should not set the X-Forwarded-Host header to the proxying host" do
89+
stub_request(:any, 'example.com/test/stuff')
90+
get '/test/stuff'
91+
a_request(:get, 'http://example.com/test/stuff').with(:headers => {'X-Forwarded-Host' => 'example.org'}).should_not have_been_made
92+
a_request(:get, 'http://example.com/test/stuff').should have_been_made
93+
end
94+
end
95+
7496
describe "with basic auth turned on" do
7597
def app
7698
Rack::ReverseProxy.new(dummy_app) do

0 commit comments

Comments
 (0)