Class: Datadog::AppSec::Utils::HTTP::MediaRange
- Inherits:
-
Object
- Object
- Datadog::AppSec::Utils::HTTP::MediaRange
- Defined in:
- lib/datadog/appsec/utils/http/media_range.rb
Overview
Implementation of media range for content negotiation
Defined Under Namespace
Classes: ParseError
Constant Summary collapse
- WILDCARD =
'*'.freeze
- WILDCARD_RE =
::Regexp.escape(WILDCARD)
- TOKEN_RE =
/[-#$%&'*+.^_`|~A-Za-z0-9]+/.freeze
- PARAMETER_RE =
%r{ # rubocop:disable Style/RegexpLiteral (?: (?<parameter_name>#{TOKEN_RE}) = (?: (?<parameter_value>#{TOKEN_RE}) | "(?<parameter_value>[^"]+)" ) ) }ix.freeze
- ACCEPT_EXT_RE =
%r{ # rubocop:disable Style/RegexpLiteral (?: (?<ext_name>#{TOKEN_RE}) = (?: (?<ext_value>#{TOKEN_RE}) | "(?<ext_value>[^"]+)" ) ) }ix.freeze
- QVALUE_RE =
%r{ # rubocop:disable Style/RegexpLiteral 0(?:\.\d{1,3})? | 1(?:\.0{1,3})? }ix.freeze
- MEDIA_RANGE_RE =
%r{ \A (?: (?<type>#{WILDCARD_RE})/(?<subtype>#{WILDCARD_RE}) | (?<type>#{TOKEN_RE})/(?<subtype>#{WILDCARD_RE}) | (?<type>#{TOKEN_RE})/(?<subtype>#{TOKEN_RE}) ) (?<parameters> (?: \s*;\s* (?!q=) #{PARAMETER_RE} )* ) (?<accept_params> (?<weight> \s*;\s* (?:q= (?<quality> #{QVALUE_RE} ) ) ) (?<accept_exts> (?<accept_ext> (?: \s*;\s* (?!q=) #{ACCEPT_EXT_RE} )* ) ) )? \Z }ix.freeze
Instance Attribute Summary collapse
-
#accept_ext ⇒ Object
readonly
Returns the value of attribute accept_ext.
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
-
#quality ⇒ Object
readonly
Returns the value of attribute quality.
-
#subtype ⇒ Object
readonly
Returns the value of attribute subtype.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compare two MediaRange for ordering.
-
#===(other) ⇒ Object
Compare with a MediaType for match.
-
#initialize(media_range) ⇒ MediaRange
constructor
rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity.
- #specificity ⇒ Object
- #to_s ⇒ Object
- #wildcard?(field = nil) ⇒ Boolean
Constructor Details
#initialize(media_range) ⇒ MediaRange
rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/datadog/appsec/utils/http/media_range.rb', line 92 def initialize(media_range) # rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity media_range_match = MEDIA_RANGE_RE.match(media_range) raise ParseError, media_range.inspect if media_range_match.nil? @type = (media_range_match['type'] || WILDCARD).downcase @subtype = (media_range_match['subtype'] || WILDCARD).downcase @quality = (media_range_match['quality'] || 1.0).to_f @parameters = {} @accept_ext = {} parameters = media_range_match['parameters'] return if parameters.nil? parameters.split(';').map(&:strip).each do |parameter| parameter_match = PARAMETER_RE.match(parameter) next if parameter_match.nil? parameter_name = parameter_match['parameter_name'] parameter_value = parameter_match['parameter_value'] next if parameter_name.nil? || parameter_value.nil? @parameters[parameter_name.downcase] = parameter_value.downcase end accept_exts = media_range_match['accept_exts'] return if accept_exts.nil? accept_exts.split(';').map(&:strip).each do |ext| ext_match = ACCEPT_EXT_RE.match(ext) next if ext_match.nil? ext_name = ext_match['ext_name'] ext_value = ext_match['ext_value'] next if ext_name.nil? || ext_value.nil? @accept_ext[ext_name.downcase] = ext_value.downcase end end |
Instance Attribute Details
#accept_ext ⇒ Object (readonly)
Returns the value of attribute accept_ext.
90 91 92 |
# File 'lib/datadog/appsec/utils/http/media_range.rb', line 90 def accept_ext @accept_ext end |
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
90 91 92 |
# File 'lib/datadog/appsec/utils/http/media_range.rb', line 90 def parameters @parameters end |
#quality ⇒ Object (readonly)
Returns the value of attribute quality.
90 91 92 |
# File 'lib/datadog/appsec/utils/http/media_range.rb', line 90 def quality @quality end |
#subtype ⇒ Object (readonly)
Returns the value of attribute subtype.
90 91 92 |
# File 'lib/datadog/appsec/utils/http/media_range.rb', line 90 def subtype @subtype end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
90 91 92 |
# File 'lib/datadog/appsec/utils/http/media_range.rb', line 90 def type @type end |
Instance Method Details
#<=>(other) ⇒ Object
Compare two MediaRange for ordering
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/datadog/appsec/utils/http/media_range.rb', line 139 def <=>(other) unless (q = quality <=> other.quality) == 0 || q.nil? return q end if (s = specificity <=> other.specificity) != 0 return s end unless wildcard?(:type) if wildcard?(:subtype) && !other.wildcard?(:subtype) return -1 elsif !wildcard?(:subtype) && other.wildcard?(:subtype) return 1 end end if wildcard?(:type) && !other.wildcard?(:type) return -1 elsif !wildcard?(:type) && other.wildcard?(:type) return 1 end 0 end |
#===(other) ⇒ Object
Compare with a MediaType for match
returns true if the MediaType is accepted by this MediaRange
168 169 170 171 172 173 174 |
# File 'lib/datadog/appsec/utils/http/media_range.rb', line 168 def ===(other) return self === MediaType.new(other) if other.is_a?(::String) type == other.type && subtype == other.subtype && other.parameters.all? { |k, v| parameters[k] == v } || type == other.type && wildcard?(:subtype) || wildcard?(:type) && wildcard?(:subtype) end |
#specificity ⇒ Object
176 177 178 |
# File 'lib/datadog/appsec/utils/http/media_range.rb', line 176 def specificity @parameters.count end |
#to_s ⇒ Object
186 187 188 189 190 191 192 193 194 |
# File 'lib/datadog/appsec/utils/http/media_range.rb', line 186 def to_s s = "#{@type}/#{@subtype}" s << ';' << @parameters.map { |k, v| "#{k}=#{v}" }.join(';') if @parameters.count > 0 s << ";q=#{@quality}" if @quality < 1.0 s << ';' << @accept_ext.map { |k, v| "#{k}=#{v}" }.join(';') if @accept_ext.count > 0 s end |
#wildcard?(field = nil) ⇒ Boolean
180 181 182 183 184 |
# File 'lib/datadog/appsec/utils/http/media_range.rb', line 180 def wildcard?(field = nil) return wildcard?(:type) || wildcard?(:subtype) if field.nil? instance_variable_get(:"@#{field}") == WILDCARD end |