Skip to content

Commit 223b861

Browse files
authored
Merge pull request jaswope#52 from jjb/stripped-headers-option
new stripped_headers option & retire preserve_encoding option
2 parents 6eaa981 + 870b18f commit 223b861

File tree

5 files changed

+55
-33
lines changed

5 files changed

+55
-33
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
## 1.0.0 (UNRELEASED)
44

55
- Breaking Change: Never modify Location headers that are only paths without hosts. [John Bachir](https://github.com/jjb) [#46](https://github.com/waterlink/rack-reverse-proxy/pull/46)
6+
- Breaking Change: Previously, the Accept-Encoding header was stripped by default, unless the
7+
`preserve_encoding` option was set to true. Now, no headers are stripped by default, and an array
8+
of headers that should be stripped can be specified with the `stripped_headers` option.
69
- Bugfix: Fix rack response body for https redirects [John Bachir](https://github.com/jjb) [#43](https://github.com/waterlink/rack-reverse-proxy/pull/43)
710

811
## 0.12.0

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ Right now if more than one rule matches any given route, it throws an exception
8888
* `:force_ssl` redirects to ssl version, if not already using it (requires `:replace_response_host`). Default: false.
8989
* `:verify_mode` the `OpenSSL::SSL` verify mode passed to Net::HTTP. Default: `OpenSSL::SSL::VERIFY_PEER`.
9090
* `:x_forwarded_headers` sets up proper `X-Forwarded-*` headers. Default: true.
91-
* `:preserve_encoding` Set to true to pass Accept-Encoding header to proxy server. Default: false.
91+
* `:stripped_headers` Array of headers that should be stripped before forwarding reqeust. Default: nil.
92+
e.g. `stripped_headers: ["Accept-Encoding", "Foo-Bar"]`
9293

9394
## Note on Patches/Pull Requests
9495
* Fork the project.

lib/rack_reverse_proxy/middleware.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def initialize(app = nil, &b)
1313
@rules = []
1414
@global_options = {
1515
:preserve_host => true,
16-
:preserve_encoding => false,
16+
:stripped_headers => nil,
1717
:x_forwarded_headers => true,
1818
:matching => :all,
1919
:replace_response_host => false

lib/rack_reverse_proxy/roundtrip.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,11 @@ def preserve_host
8484
target_request_headers["HOST"] = host_header
8585
end
8686

87-
def preserve_encoding
88-
return if options[:preserve_encoding]
89-
target_request_headers.delete("Accept-Encoding")
87+
def strip_headers
88+
return unless options[:stripped_headers]
89+
options[:stripped_headers].each do |header|
90+
target_request_headers.delete(header)
91+
end
9092
end
9193

9294
def host_header
@@ -184,7 +186,7 @@ def need_replace_location?
184186

185187
def setup_request
186188
preserve_host
187-
preserve_encoding
189+
strip_headers
188190
set_forwarded_headers
189191
initialize_http_header
190192
set_basic_auth

spec/rack/reverse_proxy_spec.rb

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -159,20 +159,6 @@ def app
159159
expect(last_response.headers["Content-Length"]).to eq(body.length.to_s)
160160
end
161161

162-
it "does not include Accept-Encoding header" do
163-
stub_request(:any, "http://example.com/test")
164-
165-
get "/test", {}, "HTTP_ACCEPT_ENCODING" => "gzip, deflate"
166-
167-
expect(
168-
a_request(:get, "http://example.com/test").with(
169-
:headers => { "Accept-Encoding" => "gzip, deflate" }
170-
)
171-
).not_to have_been_made
172-
173-
expect(a_request(:get, "http://example.com/test")).to have_been_made
174-
end
175-
176162
describe "with non-default port" do
177163
def app
178164
Rack::ReverseProxy.new(dummy_app) do
@@ -212,23 +198,53 @@ def app
212198
end
213199
end
214200

215-
describe "with preserve encoding turned on" do
216-
def app
217-
Rack::ReverseProxy.new(dummy_app) do
218-
reverse_proxy "/test", "http://example.com/", :preserve_encoding => true
219-
end
201+
context "stripped_headers option" do
202+
subject do
203+
stub_request(:any, "http://example.com/test")
204+
get "/test", {}, "HTTP_ACCEPT_ENCODING" => "gzip, deflate", "HTTP_FOO_BAR" => "baz"
220205
end
221206

222-
it "sets the Accept-Encoding header" do
223-
stub_request(:any, "http://example.com/test")
207+
describe "with stripped_headers not set" do
208+
def app
209+
Rack::ReverseProxy.new(dummy_app) do
210+
reverse_proxy "/test", "http://example.com/"
211+
end
212+
end
224213

225-
get "/test", {}, "HTTP_ACCEPT_ENCODING" => "gzip, deflate"
214+
it "forwards the headers" do
215+
subject
216+
expect(
217+
a_request(:get, "http://example.com/test").with(
218+
:headers => { "Accept-Encoding" => "gzip, deflate", "Foo-Bar" => "baz" }
219+
)
220+
).to have_been_made
221+
end
222+
end
226223

227-
expect(
228-
a_request(:get, "http://example.com/test").with(
229-
:headers => { "Accept-Encoding" => "gzip, deflate" }
230-
)
231-
).to have_been_made
224+
describe "with stripped_headers set" do
225+
before do
226+
@stripped_headers = ["Accept-Encoding", "Foo-Bar"]
227+
def app
228+
# so the value is constant in the closure below
229+
stripped_headers = @stripped_headers
230+
Rack::ReverseProxy.new(dummy_app) do
231+
reverse_proxy "/test", "http://example.com/", :stripped_headers => stripped_headers
232+
end
233+
end
234+
end
235+
236+
it "removes the stripped headers" do
237+
subject
238+
expect(
239+
a_request(:get, "http://example.com/test").with{ |req|
240+
req.headers.each do |header, value|
241+
if @stripped_headers.include?(header)
242+
fail "expected #{header} to not be present"
243+
end
244+
end
245+
}
246+
).to have_been_made
247+
end
232248
end
233249
end
234250

0 commit comments

Comments
 (0)