diff --git a/lib/httparty/request.rb b/lib/httparty/request.rb index 1ee3387..b50ce60 100644 --- a/lib/httparty/request.rb +++ b/lib/httparty/request.rb @@ -137,7 +137,6 @@ module HTTParty end end - handle_deflation unless http_method == Net::HTTP::Head handle_host_redirection if response_redirects? handle_response(chunked_body, &block) end @@ -184,7 +183,17 @@ module HTTParty @raw_request = http_method.new(request_uri(uri)) @raw_request.body = body if body @raw_request.body_stream = options[:body_stream] if options[:body_stream] - @raw_request.initialize_http_header(options[:headers].to_hash) if options[:headers].respond_to?(:to_hash) + if options[:headers].respond_to?(:to_hash) + headers_hash = options[:headers].to_hash + @raw_request.initialize_http_header(headers_hash) + # If the caller specified a header of 'Accept-Encoding', assume they want to + # deal with encoding of content. Disable the internal logic in Net:HTTP + # that handles encoding, if the platform supports it. + if @raw_request.respond_to?(:decode_content) && (headers_hash.key?('Accept-Encoding') || headers_hash.key?('accept-encoding')) + # Using the '[]=' sets decode_content to false + @raw_request['accept-encoding'] = @raw_request['accept-encoding'] + end + end @raw_request.basic_auth(username, password) if options[:basic_auth] && send_authorization_header? setup_digest_auth if options[:digest_auth] end @@ -308,22 +317,6 @@ module HTTParty end end - # Inspired by Ruby 1.9 - def handle_deflation - return if response_redirects? - return if last_response.body.nil? - - case last_response["content-encoding"] - when "gzip", "x-gzip" - body_io = StringIO.new(last_response.body) - last_response.body.replace Zlib::GzipReader.new(body_io).read - last_response.delete('content-encoding') - when "deflate" - last_response.body.replace Zlib::Inflate.inflate(last_response.body) - last_response.delete('content-encoding') - end - end - def handle_host_redirection check_duplicate_location_header redirect_path = options[:uri_adapter].parse last_response['location'] diff --git a/spec/httparty/request_spec.rb b/spec/httparty/request_spec.rb index 16fc86d..5ba21a4 100644 --- a/spec/httparty/request_spec.rb +++ b/spec/httparty/request_spec.rb @@ -1059,54 +1059,6 @@ RSpec.describe HTTParty::Request do end end - describe "#handle_deflation" do - context "context-encoding" do - before do - @request.options[:format] = :html - @last_response = double - allow(@last_response).to receive(:body).and_return('') - end - - it "should inflate the gzipped body with content-encoding: gzip" do - allow(@last_response).to receive(:[]).with("content-encoding").and_return("gzip") - allow(@request).to receive(:last_response).and_return(@last_response) - expect(Zlib::GzipReader).to receive(:new).and_return(StringIO.new('')) - expect(@request.last_response).to receive(:delete).with('content-encoding') - @request.send(:handle_deflation) - end - - it "should inflate the gzipped body with content-encoding: x-gzip" do - allow(@last_response).to receive(:[]).with("content-encoding").and_return("x-gzip") - allow(@request).to receive(:last_response).and_return(@last_response) - expect(Zlib::GzipReader).to receive(:new).and_return(StringIO.new('')) - expect(@request.last_response).to receive(:delete).with('content-encoding') - @request.send(:handle_deflation) - end - - it "should inflate the deflated body" do - allow(@last_response).to receive(:[]).with("content-encoding").and_return("deflate") - allow(@request).to receive(:last_response).and_return(@last_response) - expect(Zlib::Inflate).to receive(:inflate).and_return('') - expect(@request.last_response).to receive(:delete).with('content-encoding') - @request.send(:handle_deflation) - end - - it "should not inflate a redirected response with content-encoding: gzip" do - allow(@last_response).to receive(:[]).with("content-encoding").and_return("gzip") - allow(@request).to receive(:last_response).and_return(@last_response) - allow(@request).to receive(:response_redirects?).and_return(true) - @request.send(:handle_deflation) - end - - it "should not inflate a redirected response with content-encoding: deflate" do - allow(@last_response).to receive(:[]).with("content-encoding").and_return("deflate") - allow(@request).to receive(:last_response).and_return(@last_response) - allow(@request).to receive(:response_redirects?).and_return(true) - @request.send(:handle_deflation) - end - end - end - describe "#send_authorization_header?" do context "basic_auth" do before do @@ -1202,4 +1154,18 @@ RSpec.describe HTTParty::Request do }.to raise_error(HTTParty::RedirectionTooDeep, 'HTTP redirects too deep') end end + + context 'with Accept-Encoding header' do + it 'should disable content decoding if present' do + request = HTTParty::Request.new(Net::HTTP::Get, 'http://api.foo.com/v1', headers:{'Accept-Encoding' => 'custom'}) + request.send(:setup_raw_request) + expect(request.instance_variable_get(:@raw_request).decode_content).to eq(false) + end + + it 'should disable content decoding if present' do + request = HTTParty::Request.new(Net::HTTP::Get, 'http://api.foo.com/v1') + request.send(:setup_raw_request) + expect(request.instance_variable_get(:@raw_request).decode_content).to eq(true) + end + end end