Module: HTTP::Accept::Encodings

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

Defined Under Namespace

Classes: ContentCoding

Constant Summary collapse

CONTENT_CODING =
TOKEN
QVALUE =
/0(\.[0-9]{0,3})?|1(\.[0]{0,3})?/
CODINGS =
/(?<encoding>#{CONTENT_CODING})(;q=(?<q>#{QVALUE}))?/
HTTP_ACCEPT_ENCODING =
'HTTP_ACCEPT_ENCODING'.freeze
WILDCARD_CONTENT_CODING =
ContentCoding.new('*', nil).freeze
IDENTITY_CONTENT_CODING =
ContentCoding.new('identity', nil).freeze

Class Method Summary collapse

Class Method Details

.browser_preferred_content_codings(env) ⇒ Object

Parse the list of browser preferred content codings and return ordered by priority. If no Accept-Encoding: header is specified, the behaviour is the same as if ‘Accept-Encoding: *` was provided, and if a blank Accept-Encoding: header value is specified, the behaviour is the same as if `Accept-Encoding: identity` was provided (according to RFC).



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/http/accept/encodings.rb', line 76

def self.browser_preferred_content_codings(env)
  if accept_content_codings = env[HTTP_ACCEPT_ENCODING]&.strip
    if accept_content_codings.empty?
      # "An Accept-Encoding header field with a combined field-value that is
      # empty implies that the user agent does not want any content-coding in
      # response."
      return [IDENTITY_CONTENT_CODING]
    else
      return HTTP::Accept::Encodings.parse(accept_content_codings)
    end
  end
  
  # "If no Accept-Encoding field is in the request, any content-coding
  #  is considered acceptable by the user agent."
  return [WILDCARD_CONTENT_CODING]
end

.parse(text) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/http/accept/encodings.rb', line 59

def self.parse(text)
  scanner = StringScanner.new(text)
  
  encodings = ContentCoding.parse(scanner)
  
  return Sort.by_quality_factor(encodings)
end