module HTTP::Accept

Constants

HTTP_ACCEPT
HTTP_ACCEPT_CHARSET
HTTP_ACCEPT_ENCODING
IDENTITY_CONTENT_CODING
QUOTED_STRING
TOKEN

According to tools.ietf.org/html/rfc7231#appendix-C

VERSION
WILDCARD_CHARSET
WILDCARD_CONTENT_CODING
WILDCARD_MEDIA_RANGE

Public Class Methods

browser_preferred_charsets(env) click to toggle source

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

# 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
browser_preferred_content_codings(env) click to toggle source

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).

# 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
browser_preferred_media_types(env) click to toggle source

Parse the list of browser preferred content types and return ordered by priority. If no `Accept:` header is specified, the behaviour is the same as if `Accept: /` was provided (according to RFC).

# File lib/http/accept/media_types.rb, line 127
def self.browser_preferred_media_types(env)
        if accept_content_types = env[HTTP_ACCEPT]&.strip
                unless accept_content_types.empty?
                        return HTTP::Accept::MediaTypes.parse(accept_content_types)
                end
        end
        
        # According to http://tools.ietf.org/html/rfc7231#section-5.3.2:
        # A request without any Accept header field implies that the user agent will accept any media type in response.
        # You should treat a non-existent Accept header as */*.
        return [WILDCARD_MEDIA_RANGE]
end
parse(text) click to toggle source
# 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