Method: HTTP::Negotiate#negotiate

Defined in:
lib/http/negotiate.rb

#negotiate(request, variants, add_langs: false, all: false, cmp: nil) ⇒ Object

The variant mapping takes the following form: { variant => [weight, type, encoding, charset, language, size] }

(Alternatively this array can be a hash with the same keys as symbols.)

  • the variant can be anything, including the actual variant
  • the weight is a number between 0 and 1 denoting the initial preference for the variant
  • type, encoding, charset, language are all strings containing their respective standardized tokens (note "encoding" is like gzip, not like utf-8: that's "charset")
  • size is the number of bytes, an integer

Returns either the winning variant or all variants if requested, sorted by the algorithm, or nil or the empty array if none are selected.

Parameters:

  • request (Hash, Rack::Request, #env)

    Anything roughly a hash of headers

  • variants (Hash)

    the variants, described above

  • add_langs (false, true) (defaults to: false)

    whether to supplement language tags

  • all (false, true) (defaults to: false)

    whether to return a sorted list or not

  • cmp (Proc) (defaults to: nil)

    a secondary comparison of variants as a tiebreaker



129
130
131
132
133
134
135
136
137
138
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/http/negotiate.rb', line 129

def negotiate request, variants, add_langs: false, all: false, cmp: nil
  accept = parse_headers request, add_langs: add_langs

  # convert variants to array
  variants = variants.transform_values do |v|
    v.is_a?(Hash) ? v.values_at(*KEYS) : v
  end

  # check if any of the variants specify a language, since this will
  # affect the scoring
  any_lang = variants.values.any? { |v| v[4] }

  # chosen will be { variant => value }
  scores = {}
  variants.keys.each do |var|
    qs, type, encoding, charset, language, size = variants[var]
    # some defaults
    qs   ||= 1
    type ||= ''
    size ||= 0

    # coerce encoding
    encoding = encoding.to_s.strip.downcase if encoding
    # coerce charset
    charset = charset.to_s.strip.downcase if charset
    # coerce language to canonical form
    language = language.to_s.strip.downcase.tr_s '_-', '-' if language

    # calculate encoding quality
    qe = 1
    if accept[:encoding] and encoding
      qe = 0
      qe = accept[:encoding][encoding][:q] if accept[:encoding][encoding]
      qe = accept[:encoding][?*][:q] if accept[:encoding][?*] and
        accept[:encoding][?*][:q] > qe
    end

    # calculate charset quality
    qc = 1
    if accept[:charset] and charset and charset != 'us-ascii'
      qc = 0
      qc = accept[:charset][charset][:q] if accept[:charset][charset]
      qc = accept[:charset][?*][:q] if accept[:charset][?*] and
        accept[:charset][?*][:q] > qc
    end

    # calculate the language quality
    ql = 1
    if accept[:language]
      if language
        ql = 0.001 # initial value is very low but not zero
        lang = language.split(/-+/)
        (0..lang.length).to_a.reverse.each do |i|
          # apparently there is no wildcard for accept-language? no
          # wait there is: rfc4647 $2.1 via rfc7231 $5.3.5
          test = i > 0 ? lang.slice(0, i).join(?-) : ?*
          if accept[:language][test]
            al = accept[:language][test][:q]
            # *;q=0 will override
            if al == 0 and test != ?*
              ql = 0
              break
            elsif al > ql
              ql = al
            end
          end
        end
      elsif any_lang
        # XXX not sure if language-less variants in the same pool
        # with language-y ones
        ql = 0.5
      end
    end

    # calculate the type quality
    qt = 1
    if accept[:type] and type
      type = type.to_s
      qt = 0
      at = {}
      accept[:type].each do |k, v|
        maj, min = k.split(/\//)
        x = at[maj] ||= {}
        x[min] = v
      end

      # XXX we do not actually try to do the params this time
      mt, *params = type.split(/;+/)
      maj, min    = mt.split(/\/+/)
      params = params.map { |p| p.split(/=+/, 2) }

      # warn maj.inspect, min.inspect

      # XXX match params at some point
      qt = if at.fetch(maj, {})[min]
             at[maj][min][:q]
           elsif at.fetch(maj, {})[?*]
             at[maj][?*][:q]
           elsif at.fetch(?*, {})[?*]
             at[?*][?*][:q]
           else
             0.1
           end

    end

    scores[var] = [qs * qe * qc * ql * qt, size]
  end

  # XXX do something smarter here for secondary comparison
  cmp ||= -> a, b { 0 }

  chosen = scores.sort do |a, b|
    c = b.last.first <=> a.last.first        # first compare scores
    c = cmp.call(a.first, b.first) if c == 0 # then secondary cmp
    c == 0 ? a.last.last <=> b.last.last : c # then finally by size
  end.map(&:first)

  all ? chosen : chosen.first
end