mirror of
https://github.com/rest-client/rest-client.git
synced 2022-11-09 13:49:40 -05:00
Per RFC 7231, don't default to ISO-8859-1.
This commit is contained in:
parent
b860da40d5
commit
6d7818f517
2 changed files with 13 additions and 10 deletions
|
@ -4,25 +4,28 @@ module RestClient
|
||||||
|
|
||||||
# Return encoding from an HTTP header hash.
|
# 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]
|
# @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)
|
def self.get_encoding_from_headers(headers)
|
||||||
type_header = headers[:content_type]
|
type_header = headers[:content_type]
|
||||||
return nil unless type_header
|
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')
|
if params.include?('charset')
|
||||||
return params.fetch('charset').gsub(/(\A["']*)|(["']*\z)/, '')
|
return params.fetch('charset').gsub(/(\A["']*)|(["']*\z)/, '')
|
||||||
end
|
end
|
||||||
|
|
||||||
# HTTP typically defaults to ISO-8859-1 when not specified.
|
|
||||||
if content_type.include?('text')
|
|
||||||
return 'ISO-8859-1'
|
|
||||||
end
|
|
||||||
|
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -69,7 +72,7 @@ module RestClient
|
||||||
pdict = {}
|
pdict = {}
|
||||||
|
|
||||||
begin
|
begin
|
||||||
while p = parts.next
|
while (p = parts.next)
|
||||||
i = p.index('=')
|
i = p.index('=')
|
||||||
if i
|
if i
|
||||||
name = p[0...i].strip.downcase
|
name = p[0...i].strip.downcase
|
||||||
|
|
|
@ -2,10 +2,10 @@ require 'spec_helper'
|
||||||
|
|
||||||
describe RestClient::Utils do
|
describe RestClient::Utils do
|
||||||
describe '.get_encoding_from_headers' 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'}
|
headers = {:content_type => 'text/plain'}
|
||||||
RestClient::Utils.get_encoding_from_headers(headers).
|
RestClient::Utils.get_encoding_from_headers(headers).
|
||||||
should eq 'ISO-8859-1'
|
should eq nil
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns nil on failures' do
|
it 'returns nil on failures' do
|
||||||
|
|
Loading…
Reference in a new issue