Skip to content

Commit d0a73bb

Browse files
committed
Remove Accept-Encoding header on redirect
Prior to this commit, Puppet would copy all request headers in an HTTP redirect, including Accept-Encoding. In some cases when HTTP compression was enabled, the response would fail to get decompressed, then would fail to get parsed and trigger a vague error. This commit strips the Accept-Encoding headers on redirect, allowing Ruby's built-in Net::HTTP to both compress and decompress the traffic.
1 parent ce909ae commit d0a73bb

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

lib/puppet/http/redirector.rb

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ def redirect_to(request, response, redirects)
5656
next if header.casecmp('Authorization').zero? && request.uri.host.casecmp(location.host) != 0
5757
next if header.casecmp('Cookie').zero? && request.uri.host.casecmp(location.host) != 0
5858
end
59+
# Allow Net::HTTP to set its own Accept-Encoding header to avoid errors with HTTP compression.
60+
# See https://github.com/puppetlabs/puppet/issues/9143
61+
next if header.casecmp('Accept-Encoding').zero?
62+
5963
new_request[header] = value
6064
end
6165

spec/unit/http/client_spec.rb

+11
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,17 @@ def redirect_to(status: 302, url:)
820820
response = client.get(https)
821821
expect(response).to be_success
822822
end
823+
824+
it "does not preserve accept-encoding header when redirecting" do
825+
headers = { 'Accept-Encoding' => 'unwanted-encoding'}
826+
827+
stub_request(:get, start_url).with(headers: headers).to_return(redirect_to(url: other_host))
828+
stub_request(:get, other_host).to_return(status: 200)
829+
830+
client.get(start_url, headers: headers)
831+
expect(a_request(:get, other_host).
832+
with{ |req| req.headers['Accept-Encoding'] != 'unwanted-encoding' }).to have_been_made
833+
end
823834
end
824835

825836
context "when response indicates an overloaded server" do

0 commit comments

Comments
 (0)