Class: Datadog::AppSec::Utils::HTTP::MediaRange

Inherits:
Object
  • Object
show all
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 =
'*'
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

Instance Method Summary collapse

Constructor Details

#initialize(media_range) ⇒ MediaRange

rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity

Raises:



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
137
138
# File 'lib/datadog/appsec/utils/http/media_range.rb', line 94

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_extObject (readonly)

Returns the value of attribute accept_ext.



92
93
94
# File 'lib/datadog/appsec/utils/http/media_range.rb', line 92

def accept_ext
  @accept_ext
end

#parametersObject (readonly)

Returns the value of attribute parameters.



92
93
94
# File 'lib/datadog/appsec/utils/http/media_range.rb', line 92

def parameters
  @parameters
end

#qualityObject (readonly)

Returns the value of attribute quality.



92
93
94
# File 'lib/datadog/appsec/utils/http/media_range.rb', line 92

def quality
  @quality
end

#subtypeObject (readonly)

Returns the value of attribute subtype.



92
93
94
# File 'lib/datadog/appsec/utils/http/media_range.rb', line 92

def subtype
  @subtype
end

#typeObject (readonly)

Returns the value of attribute type.



92
93
94
# File 'lib/datadog/appsec/utils/http/media_range.rb', line 92

def type
  @type
end

Instance Method Details

#<=>(other) ⇒ Object

Compare two MediaRange for ordering



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/datadog/appsec/utils/http/media_range.rb', line 141

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



170
171
172
173
174
175
176
# File 'lib/datadog/appsec/utils/http/media_range.rb', line 170

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

#specificityObject



178
179
180
# File 'lib/datadog/appsec/utils/http/media_range.rb', line 178

def specificity
  @parameters.count
end

#to_sObject



188
189
190
191
192
193
194
195
196
# File 'lib/datadog/appsec/utils/http/media_range.rb', line 188

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

Returns:

  • (Boolean)


182
183
184
185
186
# File 'lib/datadog/appsec/utils/http/media_range.rb', line 182

def wildcard?(field = nil)
  return wildcard?(:type) || wildcard?(:subtype) if field.nil?

  instance_variable_get(:"@#{field}") == WILDCARD
end