Module: HTTP::Accept::Charsets

Defined in:
lib/http/accept/charsets.rb

Defined Under Namespace

Classes: Charset

Constant Summary collapse

QVALUE =
/0(\.[0-9]{0,3})?|1(\.[0]{0,3})?/
CHARSETS =
/(?<charset>#{TOKEN})(;q=(?<q>#{QVALUE}))?/
HTTP_ACCEPT_CHARSET =
'HTTP_ACCEPT_CHARSET'.freeze
WILDCARD_CHARSET =
Charset.new('*', nil).freeze

Class Method Summary collapse

Class Method Details

.browser_preferred_charsets(env) ⇒ Object

Parse the list of browser preferred charsets and return ordered by priority.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/http/accept/charsets.rb', line 69

def self.browser_preferred_charsets(env)
	if accept_charsets = env[HTTP_ACCEPT_CHARSET]&.strip
		if accept_charsets.empty?
			# https://tools.ietf.org/html/rfc7231#section-5.3.3 :
			#
			#    Accept-Charset = 1#( ( charset / "*" ) [ weight ] )
			#
			# Because of the `1#` rule, an empty header value is not considered valid.
			raise ParseError.new('Could not parse entire string!')
		else
			return HTTP::Accept::Charsets.parse(accept_charsets)
		end
	end
	
	# "A request without any Accept-Charset header field implies that the
	#  user agent will accept any charset in response."
	return [WILDCARD_CHARSET]
end

.parse(text) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/http/accept/charsets.rb', line 57

def self.parse(text)
	scanner = StringScanner.new(text)
	
	charsets = Charset.parse(scanner)
	
	return Sort.by_quality_factor(charsets)
end