Skip to content

Commit b40205c

Browse files
committed
do not include accept encoding by default + added ability to preserve encoding
1 parent 751e443 commit b40205c

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

lib/rack_reverse_proxy/roundtrip.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +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")
90+
end
91+
8792
def host_header
8893
return uri.host if uri.port == uri.default_port
8994
"#{uri.host}:#{uri.port}"
@@ -178,6 +183,7 @@ def need_replace_location?
178183

179184
def setup_request
180185
preserve_host
186+
preserve_encoding
181187
set_forwarded_headers
182188
initialize_http_header
183189
set_basic_auth

spec/rack/reverse_proxy_spec.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,20 @@ def app
147147
expect(last_response.headers["Content-Length"]).to eq(body.length.to_s)
148148
end
149149

150+
it "does not include Accept-Encoding header" do
151+
stub_request(:any, "http://example.com/test")
152+
153+
get "/test", {}, "HTTP_ACCEPT_ENCODING" => "gzip, deflate"
154+
155+
expect(
156+
a_request(:get, "http://example.com/test").with(
157+
:headers => { "Accept-Encoding" => "gzip, deflate" }
158+
)
159+
).not_to have_been_made
160+
161+
expect(a_request(:get, "http://example.com/test")).to have_been_made
162+
end
163+
150164
describe "with non-default port" do
151165
def app
152166
Rack::ReverseProxy.new(dummy_app) do
@@ -186,6 +200,26 @@ def app
186200
end
187201
end
188202

203+
describe "with preserve encoding turned on" do
204+
def app
205+
Rack::ReverseProxy.new(dummy_app) do
206+
reverse_proxy "/test", "http://example.com/", :preserve_encoding => true
207+
end
208+
end
209+
210+
it "sets the Accept-Encoding header" do
211+
stub_request(:any, "http://example.com/test")
212+
213+
get "/test", {}, "HTTP_ACCEPT_ENCODING" => "gzip, deflate"
214+
215+
expect(
216+
a_request(:get, "http://example.com/test").with(
217+
:headers => { "Accept-Encoding" => "gzip, deflate" }
218+
)
219+
).to have_been_made
220+
end
221+
end
222+
189223
describe "with x_forwarded_headers turned off" do
190224
def app
191225
Rack::ReverseProxy.new(dummy_app) do

0 commit comments

Comments
 (0)