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

[FIX] Capitalize headers correctly #49

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
19 changes: 13 additions & 6 deletions lib/rack/reverse_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def initialize(app = nil, &b)
@app = app || lambda {|env| [404, [], []] }
@matchers = []
@global_options = {:preserve_host => true, :x_forwarded_host => true, :matching => :all, :replace_response_host => false}
instance_eval &b if block_given?
instance_eval(&b) if block_given?
end

def call(env)
Expand Down Expand Up @@ -74,14 +74,14 @@ def proxy(env, source_request, matcher)
target_response.use_ssl = "https" == uri.scheme

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

# Replace the location header with the proxy domain
if response_headers['location'] && options[:replace_response_host]
response_location = URI(response_headers['location'][0])
if response_headers['Location'] && options[:replace_response_host]
response_location = URI(response_headers['Location'][0])
response_location.host = source_request.host
response_headers['location'] = response_location.to_s
response_headers['Location'] = response_location.to_s
end

[target_response.status, response_headers, target_response.body]
Expand Down Expand Up @@ -129,5 +129,12 @@ def reverse_proxy(matcher, url=nil, opts={})
raise GenericProxyURI.new(url) if matcher.is_a?(String) && url.is_a?(String) && URI(url).class == URI::Generic
@matchers << ReverseProxyMatcher.new(matcher,url,opts)
end

def format_headers(headers)
headers.each_with_object({}).each do |(key, val), acc|
formated_key = key.split('-').map(&:capitalize).join('-')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is not well tested. I have added the commit on top: waterlink/rack-reverse-proxy@ba4b9eb

acc[formated_key] = Array(val)
end
end
end
end
14 changes: 12 additions & 2 deletions spec/rack/reverse_proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ def app
a_request(:get, 'http://example.com/test/stuff').with(:headers => {'X-Forwarded-Host' => 'example.org'}).should have_been_made
end

it 'should format the headers correctly to avoid duplicates' do
stub_request(:get, 'http://example.com/2test').to_return({:status => 301, :headers => {:status => '301 Moved Permanently'}})

get '/2test'

headers = last_response.headers.to_hash
headers['Status'].should == "301 Moved Permanently"
headers['status'].should be_nil
end

describe "with preserve host turned off" do
def app
Rack::ReverseProxy.new(dummy_app) do
Expand Down Expand Up @@ -114,7 +124,7 @@ def app
Rack::ReverseProxy.new(dummy_app) do
reverse_proxy_options :matching => :all
reverse_proxy '/test', 'http://example.com/'
reverse_proxy /^\/test/, 'http://example.com/'
reverse_proxy(/^\/test/, 'http://example.com/')
end
end

Expand All @@ -128,7 +138,7 @@ def app
Rack::ReverseProxy.new(dummy_app) do
reverse_proxy_options :matching => :first
reverse_proxy '/test', 'http://example1.com/'
reverse_proxy /^\/test/, 'http://example2.com/'
reverse_proxy(/^\/test/, 'http://example2.com/')
end
end

Expand Down