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.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/http/accept/charsets.rb', line 53

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



41
42
43
44
45
46
47
# File 'lib/http/accept/charsets.rb', line 41

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