diff --git a/lib/restclient/utils.rb b/lib/restclient/utils.rb index 99078f8..bbcf1d3 100644 --- a/lib/restclient/utils.rb +++ b/lib/restclient/utils.rb @@ -4,25 +4,28 @@ module RestClient # Return encoding from an HTTP header hash. # + # We use the RFC 7231 specification and do not impose a default encoding on + # text. This differs from the older RFC 2616 behavior, which specifies + # using ISO-8859-1 for text/* content types without a charset. + # + # Strings will effectively end up using `Encoding.default_external` when + # this method returns nil. + # # @param headers [Hash] # - # @return [String] encoding + # @return [String, nil] encoding Return the string encoding or nil if no + # header is found. # def self.get_encoding_from_headers(headers) type_header = headers[:content_type] return nil unless type_header - content_type, params = cgi_parse_header(type_header) + _content_type, params = cgi_parse_header(type_header) if params.include?('charset') return params.fetch('charset').gsub(/(\A["']*)|(["']*\z)/, '') end - # HTTP typically defaults to ISO-8859-1 when not specified. - if content_type.include?('text') - return 'ISO-8859-1' - end - nil end @@ -69,7 +72,7 @@ module RestClient pdict = {} begin - while p = parts.next + while (p = parts.next) i = p.index('=') if i name = p[0...i].strip.downcase diff --git a/spec/unit/utils_spec.rb b/spec/unit/utils_spec.rb index 05e7430..f29ab57 100644 --- a/spec/unit/utils_spec.rb +++ b/spec/unit/utils_spec.rb @@ -2,10 +2,10 @@ require 'spec_helper' describe RestClient::Utils do describe '.get_encoding_from_headers' do - it 'assumes ISO-8859-1 by default for text' do + it 'assumes no encoding by default for text' do headers = {:content_type => 'text/plain'} RestClient::Utils.get_encoding_from_headers(headers). - should eq 'ISO-8859-1' + should eq nil end it 'returns nil on failures' do