Skip to content

Commit 344c01c

Browse files
committed
Merge pull request jaswope#5 from waterlink/ekosz/fix/double-status
[FIX] Capitalize headers correctly
2 parents 2f4816d + 6857030 commit 344c01c

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

lib/rack/reverse_proxy.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def initialize(app = nil, &b)
1313
@app = app || lambda {|env| [404, [], []] }
1414
@matchers = []
1515
@global_options = {:preserve_host => true, :x_forwarded_host => true, :matching => :all, :replace_response_host => false}
16-
instance_eval &b if block_given?
16+
instance_eval(&b) if block_given?
1717
end
1818

1919
def call(env)
@@ -82,15 +82,15 @@ def proxy(env, source_request, matcher)
8282
target_response.use_ssl = "https" == uri.scheme
8383

8484
# Let rack set the transfer-encoding header
85-
response_headers = target_response.headers
86-
response_headers.delete('transfer-encoding')
85+
response_headers = format_headers(target_response.headers)
86+
response_headers.delete('Transfer-Encoding')
8787

8888
# Replace the location header with the proxy domain
89-
if response_headers['location'] && options[:replace_response_host]
90-
response_location = URI(response_headers['location'][0])
89+
if response_headers['Location'] && options[:replace_response_host]
90+
response_location = URI(response_headers['Location'][0])
9191
response_location.host = source_request.host
9292
response_location.port = source_request.port
93-
response_headers['location'] = response_location.to_s
93+
response_headers['Location'] = response_location.to_s
9494
end
9595

9696
[target_response.status, response_headers, target_response.body]
@@ -138,5 +138,13 @@ def reverse_proxy(matcher, url=nil, opts={})
138138
raise GenericProxyURI.new(url) if matcher.is_a?(String) && url.is_a?(String) && URI(url).class == URI::Generic
139139
@matchers << ReverseProxyMatcher.new(matcher,url,opts)
140140
end
141+
142+
def format_headers(headers)
143+
headers.reduce({}) do |acc, (key, val)|
144+
formated_key = key.split('-').map(&:capitalize).join('-')
145+
acc[formated_key] = Array(val)
146+
acc
147+
end
148+
end
141149
end
142150
end

spec/rack/reverse_proxy_spec.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,26 @@ 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+
it 'should format the headers correctly to avoid duplicates' do
53+
stub_request(:get, 'http://example.com/2test').to_return({:status => 301, :headers => {:status => '301 Moved Permanently'}})
54+
55+
get '/2test'
56+
57+
headers = last_response.headers.to_hash
58+
headers['Status'].should == "301 Moved Permanently"
59+
headers['status'].should be_nil
60+
end
61+
62+
it 'should format the headers with dashes correctly' do
63+
stub_request(:get, 'http://example.com/2test').to_return({:status => 301, :headers => {:status => '301 Moved Permanently', :"x-additional-info" => "something"}})
64+
65+
get '/2test'
66+
67+
headers = last_response.headers.to_hash
68+
headers['X-Additional-Info'].should == "something"
69+
headers['x-additional-info'].should be_nil
70+
end
71+
5272
describe "with non-default port" do
5373
def app
5474
Rack::ReverseProxy.new(dummy_app) do
@@ -162,7 +182,7 @@ def app
162182
Rack::ReverseProxy.new(dummy_app) do
163183
reverse_proxy_options :matching => :all
164184
reverse_proxy '/test', 'http://example.com/'
165-
reverse_proxy /^\/test/, 'http://example.com/'
185+
reverse_proxy(/^\/test/, 'http://example.com/')
166186
end
167187
end
168188

@@ -176,7 +196,7 @@ def app
176196
Rack::ReverseProxy.new(dummy_app) do
177197
reverse_proxy_options :matching => :first
178198
reverse_proxy '/test', 'http://example1.com/'
179-
reverse_proxy /^\/test/, 'http://example2.com/'
199+
reverse_proxy(/^\/test/, 'http://example2.com/')
180200
end
181201
end
182202

0 commit comments

Comments
 (0)