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

adding :strip_headers option to matcher and reverse_proxy_options #22

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
1 change: 1 addition & 0 deletions .rvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rvm use ruby-1.9.3@rack-reverse-proxy --create
10 changes: 5 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
PATH
remote: .
specs:
rack-reverse-proxy (0.4.1)
rack-reverse-proxy (0.4.5)
rack (>= 1.0.0)

GEM
remote: http://rubygems.org/
specs:
addressable (2.2.6)
crack (0.1.8)
rack (1.3.2)
addressable (2.3.2)
crack (0.3.1)
rack (1.4.1)
rack-test (0.5.7)
rack (>= 1.0)
rake (0.8.7)
Expand All @@ -22,7 +22,7 @@ PLATFORMS
ruby

DEPENDENCIES
bundler (~> 1.0.15)
bundler (~> 1.1.4)
rack-reverse-proxy!
rack-test (~> 0.5.7)
rake (~> 0.8.7)
Expand Down
2 changes: 2 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# A sample Guardfile
# More info at https://github.com/guard/guard#readme
3 changes: 2 additions & 1 deletion README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Below is an example for configuring the middleware:

use Rack::ReverseProxy do
# Set :preserve_host to true globally (default is true already)
reverse_proxy_options :preserve_host => true
reverse_proxy_options :preserve_host => true, :strip_headers => ['REFERER']

# Forward the path /test* to http://example.com/test*
reverse_proxy '/test', 'http://example.com/'
Expand All @@ -40,6 +40,7 @@ reverse_proxy_options sets global options for all reverse proxies. Available opt
* :password password for basic auth
* :matching is a global only option, if set to :first the first matched url will be requested (no ambigous error). Default: :all.
* :timeout seconds to timout the requests
* :strip_headers removes the specified non-prefixed headers ("HTTP_HEADER_NAME" becomes "HEADER_NAME") from the proxy request. Default: []

== Note on Patches/Pull Requests

Expand Down
3 changes: 2 additions & 1 deletion lib/rack/reverse_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ def call(env)

uri = matcher.get_uri(rackreq.fullpath,env)
all_opts = @global_options.dup.merge(matcher.options)
all_opts[:strip_headers] ||= []
headers = Rack::Utils::HeaderHash.new
env.each { |key, value|
if key =~ /HTTP_(.*)/
headers[$1] = value
headers[$1] = value unless all_opts[:strip_headers].include?($1)
end
}
headers['HOST'] = uri.host if all_opts[:preserve_host]
Expand Down
4 changes: 2 additions & 2 deletions rack-reverse-proxy.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = %q{rack-reverse-proxy}
s.version = "0.4.4"
s.version = "0.4.5"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Jon Swope"]
Expand Down Expand Up @@ -33,7 +33,7 @@ Gem::Specification.new do |s|
]

s.add_development_dependency "rspec", "~> 1.3.2"
s.add_development_dependency "bundler", "~> 1.0.15"
s.add_development_dependency "bundler", "~> 1.1.4"
s.add_development_dependency "rake", "~> 0.8.7"
s.add_development_dependency "rack-test", "~> 0.5.7"
s.add_development_dependency "webmock", "~> 1.5.0"
Expand Down
29 changes: 29 additions & 0 deletions spec/rack/reverse_proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,35 @@ def app
a_request(:get, 'http://example.com/test/stuff').with(:headers => {'X-Forwarded-Host' => 'example.org'}).should have_been_made
end

describe "with :strip_headers specified in the matcher" do
def app
Rack::ReverseProxy.new(dummy_app) do
reverse_proxy '/test', 'http://example.com/', {:strip_headers => ['REFERER']}
end
end

it "should strip the specified headers" do
stub_request(:any, 'example.com/test/stuff')
get '/test/stuff', {}, {"HTTP_REFERER" => "http://something.bogus"}
a_request(:get, 'http://example.com/test/stuff').with(:headers => {"Referer" => "http://something.bogus"}).should_not have_been_made
end
end

describe "with :strip_headers specified in the config" do
def app
Rack::ReverseProxy.new(dummy_app) do
reverse_proxy '/test', 'http://example.com/'
reverse_proxy_options :strip_headers => ['REFERER']
end
end

it "should strip the specified headers" do
stub_request(:any, 'example.com/test/stuff')
get '/test/stuff', {}, {"HTTP_REFERER" => "http://something.bogus"}
a_request(:get, 'http://example.com/test/stuff').with(:headers => {"Referer" => "http://something.bogus"}).should_not have_been_made
end
end

describe "with preserve host turned off" do
def app
Rack::ReverseProxy.new(dummy_app) do
Expand Down