Class: Datadog::AppSec::Utils::HTTP::MediaType
- Inherits:
-
Object
- Object
- Datadog::AppSec::Utils::HTTP::MediaType
- Defined in:
- lib/datadog/appsec/utils/http/media_type.rb
Overview
Implementation of media type for HTTP headers
See:
Constant Summary collapse
- ParseError =
steep:ignore IncompatibleAssignment
Class.new(StandardError)
- WILDCARD =
'*'- TOKEN_RE =
/[-#$%&'*+.^_`|~A-Za-z0-9]+/.freeze
- PARAMETER_RE =
%r{ (?: (?<parameter_name>#{TOKEN_RE}) = (?: (?<parameter_value>#{TOKEN_RE}) | "(?<parameter_value>[^"]+)" ) ) }ix.freeze
- MEDIA_TYPE_RE =
%r{ \A (?<type>#{TOKEN_RE})/(?<subtype>#{TOKEN_RE}) (?<parameters> (?: \s*;\s* #{PARAMETER_RE} )* ) \Z }ix.freeze
Instance Attribute Summary collapse
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
-
#subtype ⇒ Object
readonly
Returns the value of attribute subtype.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(media_type) ⇒ MediaType
constructor
A new instance of MediaType.
- #to_s ⇒ Object
Constructor Details
#initialize(media_type) ⇒ MediaType
Returns a new instance of MediaType.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/datadog/appsec/utils/http/media_type.rb', line 61 def initialize(media_type) match = MEDIA_TYPE_RE.match(media_type) raise ParseError, media_type.inspect if match.nil? @type = match['type'] || WILDCARD @type.downcase! @subtype = match['subtype'] || WILDCARD @subtype.downcase! @parameters = {} parameters = match['parameters'] return if parameters.nil? || parameters.empty? parameters.scan(PARAMETER_RE) do |name, unquoted_value, quoted_value| # NOTE: Order of unquoted_value and quoted_value does not matter, # as they are mutually exclusive by the regex. # @type var value: ::String? value = unquoted_value || quoted_value next if name.nil? || value.nil? # See https://github.com/soutaro/steep/issues/2051 name.downcase! # steep:ignore NoMethod value.downcase! # See https://github.com/soutaro/steep/issues/2051 @parameters[name] = value # steep:ignore ArgumentTypeMismatch end end |
Instance Attribute Details
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
46 47 48 |
# File 'lib/datadog/appsec/utils/http/media_type.rb', line 46 def parameters @parameters end |
#subtype ⇒ Object (readonly)
Returns the value of attribute subtype.
46 47 48 |
# File 'lib/datadog/appsec/utils/http/media_type.rb', line 46 def subtype @subtype end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
46 47 48 |
# File 'lib/datadog/appsec/utils/http/media_type.rb', line 46 def type @type end |
Class Method Details
.json?(media_type) ⇒ Boolean
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/datadog/appsec/utils/http/media_type.rb', line 48 def self.json?(media_type) return false if media_type.nil? || media_type.empty? match = MEDIA_TYPE_RE.match(media_type) return false if match.nil? subtype = match['subtype'] return false if subtype.nil? || subtype.empty? subtype.downcase! subtype == 'json' || subtype.end_with?('+json') end |
Instance Method Details
#to_s ⇒ Object
92 93 94 95 96 |
# File 'lib/datadog/appsec/utils/http/media_type.rb', line 92 def to_s return "#{@type}/#{@subtype}" if @parameters.empty? "#{@type}/#{@subtype};#{@parameters.map { |k, v| "#{k}=#{v}" }.join(";")}" end |