Per RFC 7231, don't default to ISO-8859-1.

This commit is contained in:
Andy Brody 2015-03-13 16:11:33 -07:00
parent b860da40d5
commit 6d7818f517
2 changed files with 13 additions and 10 deletions

View File

@ -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

View File

@ -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