Skip to content

Commit

Permalink
Merge pull request jaswope#27 from cookstream-workspace/master
Browse files Browse the repository at this point in the history
Remove Accept-Encoding header
  • Loading branch information
waterlink committed Mar 2, 2016
2 parents 751e443 + 2a0c813 commit 48e9ae9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ run app
* `:force_ssl` redirects to ssl version, if not already using it (requires `:replace_response_host`). Default: false.
* `:verify_mode` the `OpenSSL::SSL` verify mode passed to Net::HTTP. Default: `OpenSSL::SSL::VERIFY_PEER`.
* `:x_forwarded_headers` sets up proper `X-Forwarded-*` headers. Default: true.
* `:preserve_encoding` Set to true to pass Accept-Encoding header to proxy server. Default: false.

### Sample usage in a Ruby on Rails app

Expand Down
1 change: 1 addition & 0 deletions lib/rack_reverse_proxy/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def initialize(app = nil, &b)
@rules = []
@global_options = {
:preserve_host => true,
:preserve_encoding => false,
:x_forwarded_headers => true,
:matching => :all,
:replace_response_host => false
Expand Down
6 changes: 6 additions & 0 deletions lib/rack_reverse_proxy/roundtrip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ def preserve_host
target_request_headers["HOST"] = host_header
end

def preserve_encoding
return if options[:preserve_encoding]
target_request_headers.delete("Accept-Encoding")
end

def host_header
return uri.host if uri.port == uri.default_port
"#{uri.host}:#{uri.port}"
Expand Down Expand Up @@ -178,6 +183,7 @@ def need_replace_location?

def setup_request
preserve_host
preserve_encoding
set_forwarded_headers
initialize_http_header
set_basic_auth
Expand Down
34 changes: 34 additions & 0 deletions spec/rack/reverse_proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,20 @@ def app
expect(last_response.headers["Content-Length"]).to eq(body.length.to_s)
end

it "does not include Accept-Encoding header" do
stub_request(:any, "http://example.com/test")

get "/test", {}, "HTTP_ACCEPT_ENCODING" => "gzip, deflate"

expect(
a_request(:get, "http://example.com/test").with(
:headers => { "Accept-Encoding" => "gzip, deflate" }
)
).not_to have_been_made

expect(a_request(:get, "http://example.com/test")).to have_been_made
end

describe "with non-default port" do
def app
Rack::ReverseProxy.new(dummy_app) do
Expand Down Expand Up @@ -186,6 +200,26 @@ def app
end
end

describe "with preserve encoding turned on" do
def app
Rack::ReverseProxy.new(dummy_app) do
reverse_proxy "/test", "http://example.com/", :preserve_encoding => true
end
end

it "sets the Accept-Encoding header" do
stub_request(:any, "http://example.com/test")

get "/test", {}, "HTTP_ACCEPT_ENCODING" => "gzip, deflate"

expect(
a_request(:get, "http://example.com/test").with(
:headers => { "Accept-Encoding" => "gzip, deflate" }
)
).to have_been_made
end
end

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

0 comments on commit 48e9ae9

Please sign in to comment.