File: HttpCompat.cc

package info (click to toggle)
trafficserver 9.2.5%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 53,008 kB
  • sloc: cpp: 345,484; ansic: 31,134; python: 24,200; sh: 7,271; makefile: 3,045; perl: 2,261; java: 277; pascal: 119; sql: 94; xml: 2
file content (731 lines) | stat: -rw-r--r-- 25,281 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
91
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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
/** @file

  A brief file description

  @section license License

  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 */

#include "tscore/ink_platform.h"
#include "HttpCompat.h"
#include "HdrUtils.h"

//////////////////////////////////////////////////////////////////////////////
//
//      HttpCompat::parse_tok_list
//
//      Takes a string containing an HTTP list broken on the separator
//      character <sep>, and returns a StrList object containing a
//      dynamically allocated list of elements.  This is essentially a
//      fancy strtok that runs to completion and hands you back all tokens.
//
//      The routine either allocates and copies each string token, or
//      just maintains the point to the raw text token, depending on the
//      mode of the StrList object.
//
//////////////////////////////////////////////////////////////////////////////

void
HttpCompat::parse_tok_list(StrList *list, int trim_quotes, const char *string, char sep)
{
  if (string == nullptr) {
    return;
  }
  HttpCompat::parse_tok_list(list, trim_quotes, string, static_cast<int>(strlen(string)), sep);
}

void
HttpCompat::parse_tok_list(StrList *list, int trim_quotes, const char *string, int len, char sep)
{
  int in_quote;
  const char quot = '\"';
  const char *s, *e, *l, *s_before_skipping_ws;
  int index, byte_length, hit_sep;

  if ((string == nullptr) || (list == nullptr) || (sep == NUL)) {
    return;
  }

  s     = string;
  l     = s + len - 1;
  index = 0;

  hit_sep              = 0;
  s_before_skipping_ws = s;

  while (s <= l) {
    //////////////////////////////////////////////////////////
    // find the start of the first token, skipping over any //
    // whitespace or empty tokens, to leave <s> pointing at //
    // a NUL, a character, or a double quote.               //
    //////////////////////////////////////////////////////////

    while ((s <= l) && ParseRules::is_ws(*s)) {
      ++s; // skip whitespace
    }

    //////////////////////////////////////////////////////////
    // if we are pointing at a separator, this was an empty //
    // token, so add the empty token, and continue parsing. //
    //////////////////////////////////////////////////////////

    if ((s <= l) && (*s == sep)) {
      list->append_string(s_before_skipping_ws, 0);
      ++index;
      s_before_skipping_ws = s + 1;
      s                    = s_before_skipping_ws;
      hit_sep              = 1;
      continue;
    }
    //////////////////////////////////////////////////////////////////
    // at this point, <s> points to EOS, a double quote, or another //
    // character --- if EOS, then break out of the loop, and either //
    // tack on a final empty token if we had a trailing separator,  //
    // or just exit.                                                //
    //////////////////////////////////////////////////////////////////

    if (s > l) {
      break;
    }

    ///////////////////////////////////////////////////////////////////
    // we are pointing to the first character of a token now, either //
    // a character, or a double quote --- the next step is to scan   //
    // for the next separator or end of string, being careful not to //
    // include separators inside quotes.                             //
    ///////////////////////////////////////////////////////////////////

#define is_unquoted_separator(c) ((c == sep) && !in_quote)

    if (*s == quot) {
      in_quote = 1;
      e        = s + 1; // start after quote
      if (trim_quotes) {
        ++s; // trim starting quote
      }
    } else {
      in_quote = 0;
      e        = s;
    }

    while ((e <= l) && !is_unquoted_separator(*e)) {
      if (*e == quot) {
        in_quote = !in_quote;
      }
      e++;
    }

    ///////////////////////////////////////////////////////////////////////
    // we point one char past the last character of string, or an        //
    // unquoted separator --- so back up into any previous whitespace or //
    // quote, leaving <e> pointed 1 char after the last token character. //
    ///////////////////////////////////////////////////////////////////////

    hit_sep = (e <= l); // must have hit a separator if still inside string

    s_before_skipping_ws = e + 1; // where to start next time
    while ((e > s) && ParseRules::is_ws(*(e - 1))) {
      --e; // eat trailing ws
    }
    if ((e > s) && (*(e - 1) == quot) && trim_quotes) {
      --e; // eat trailing quote
    }

    /////////////////////////////////////////////////////////////////////
    // now <e> points to the character AFTER the last character of the //
    // field, either a separator, a quote, or a NUL (other other char  //
    // after the last char in the string.                              //
    /////////////////////////////////////////////////////////////////////

    byte_length = static_cast<int>(e - s);
    ink_assert(byte_length >= 0);

    ///////////////////////////////////////////
    // add the text to the list, and move on //
    ///////////////////////////////////////////

    list->append_string(s, byte_length);
    s = s_before_skipping_ws; // where to start next time
    ++index;
  }

  ////////////////////////////////////////////////////////////////////////////
  // fall out of loop when at end of string --- three possibilities:        //
  //   (1) at end of string after final token ("a,b,c" or "a,b,c   ")       //
  //   (2) at end of string after final separator ("a,b,c," or "a,b,c,   ") //
  //   (3) at end of string before any tokens ("" or "   ")                 //
  // for cases (2) & (3), we want to return an empty token                  //
  ////////////////////////////////////////////////////////////////////////////

  if (hit_sep || (index == 0)) {
    ink_assert(s == l + 1);
    list->append_string(s_before_skipping_ws, 0);
    ++index;
  }
}

//////////////////////////////////////////////////////////////////////////////
//
//      bool HttpCompat::lookup_param_in_strlist(
//          StrList *param_list, char *param_name,
//          char *param_val, int param_val_length)
//
//      Takes a list of parameter strings, and searches each parameter list
//      element for the name <param_name>, and if followed by '=' and a value,
//      the value string is stored in <param_val> up to <param_val_length>
//      bytes minus 1 character for trailing NUL.
//
//      This routine can be used to search for charset=XXX, Q=XXX, and other
//      kinds of parameters.  The param list can be constructed using the
//      parse_comma_list and parse_semicolon_list functions.
//
//      The routine returns true if there was a match, false otherwise.
//
//////////////////////////////////////////////////////////////////////////////

bool
HttpCompat::lookup_param_in_strlist(StrList *param_list, const char *param_name, char *param_val, int param_val_length)
{
  int cnt;
  const char *s, *t;
  Str *param;
  bool is_match;

  for (param = param_list->head; param != nullptr; param = param->next) {
    /////////////////////////////////////////////////////
    // compare this parameter to the target param_name //
    /////////////////////////////////////////////////////

    s = param->str; // source str
    t = param_name; // target str
    while (*s && *t && (ParseRules::ink_tolower(*s) == ParseRules::ink_tolower(*t))) {
      ++s;
      ++t;
    }

    ////////////////////////////////////////////////////////////////
    // match if target string empty, and if current string empty,  //
    // or points to space or '=' character.                       //
    ////////////////////////////////////////////////////////////////

    is_match = ((!*t) && ((!*s) || ParseRules::is_ws(*s) || (*s == '=')));

    /////////////////////////////////////////////////////////////
    // copy text after '=' into param_val, up to length limits //
    /////////////////////////////////////////////////////////////

    if (is_match) {
      param_val[0] = '\0';

      while (*s && ParseRules::is_ws(*s)) {
        s++; // skip white
      }
      if (*s == '=') {
        ++s; // skip '='
        while (*s && ParseRules::is_ws(*s)) {
          s++; // skip white
        }

        for (cnt = 0; *s && (cnt < param_val_length - 1); s++, cnt++) {
          param_val[cnt] = *s;
        }
        if (cnt < param_val_length) {
          param_val[cnt++] = '\0';
        }
      }
      return (true);
    }
  }

  return (false);
}

//////////////////////////////////////////////////////////////////////////////
//
//      bool HttpCompat::lookup_param_in_semicolon_string(
//          char *semicolon_string, int semicolon_string_len,
//          char *param_name, char *param_val, int param_val_length)
//
//      Takes a semicolon-separated string of parameters, and searches
//      for a parameter named <param_name>, as in lookup_param_in_strlist.
//
//      The routine returns true if there was a match, false otherwise.
//      If multiple parameters will be searched for in the same string,
//      use lookup_param_in_strlist(), so the string is not tokenized
//      multiple times.
//
//////////////////////////////////////////////////////////////////////////////

bool
HttpCompat::lookup_param_in_semicolon_string(const char *semicolon_string, int semicolon_string_len, const char *param_name,
                                             char *param_val, int param_val_length)
{
  StrList l;
  bool result;

  parse_semicolon_list(&l, semicolon_string, semicolon_string_len);
  result = lookup_param_in_strlist(&l, param_name, param_val, param_val_length);
  return (result);
}

//////////////////////////////////////////////////////////////////////////////
//
//      void HttpCompat::parse_mime_type(
//          char *mime_string, char *type, char *subtype,
//          int type_len, int subtype_len)
//
//      This routine takes a pointer to a MIME type, and decomposes it
//      into type and subtype fields, skipping over spaces, and placing
//      the decomposed values into <type> and <subtype>.  The length
//      fields describe the lengths of the type and subtype buffers,
//      including the trailing NUL characters.
//
//////////////////////////////////////////////////////////////////////////////

void
HttpCompat::parse_mime_type(const char *mime_string, char *type, char *subtype, int type_len, int subtype_len)
{
  const char *s, *e;
  char *d;

  *type = *subtype = '\0';

  /////////////////////
  // skip whitespace //
  /////////////////////

  for (s = mime_string; *s && ParseRules::is_ws(*s); s++) {
    ;
  }

  ///////////////////////////////////////////////////////////////////////
  // scan type (until NUL, out of room, comma/semicolon, space, slash) //
  ///////////////////////////////////////////////////////////////////////

  d = type;
  e = type + type_len;
  while (*s && (d < e - 1) && (!ParseRules::is_ws(*s)) && (*s != ';') && (*s != ',') && (*s != '/')) {
    *d++ = *s++;
  }
  *d++ = '\0';

  //////////////////////////////////////////////////////////////
  // skip remainder of text and space, then slash, then space //
  //////////////////////////////////////////////////////////////

  while (*s && (*s != ';') && (*s != ',') && (*s != '/')) {
    ++s;
  }
  if (*s == '/') {
    ++s;
  }
  while (*s && ParseRules::is_ws(*s)) {
    ++s;
  }

  //////////////////////////////////////////////////////////////////////////
  // scan subtype (until NUL, out of room, comma/semicolon, space, slash) //
  //////////////////////////////////////////////////////////////////////////

  d = subtype;
  e = subtype + subtype_len;
  while (*s && (d < e - 1) && (!ParseRules::is_ws(*s)) && (*s != ';') && (*s != ',') && (*s != '/')) {
    *d++ = *s++;
  }
  *d++ = '\0';
}

void
HttpCompat::parse_mime_type_with_len(const char *mime_string, int mime_string_len, char *type, char *subtype, int type_len,
                                     int subtype_len)
{
  const char *s, *s_toofar, *e;
  char *d;

  *type = *subtype = '\0';
  s_toofar         = mime_string + mime_string_len;

  /////////////////////
  // skip whitespace //
  /////////////////////

  for (s = mime_string; (s < s_toofar) && ParseRules::is_ws(*s); s++) {
    ;
  }

  ///////////////////////////////////////////////////////////////////////
  // scan type (until NUL, out of room, comma/semicolon, space, slash) //
  ///////////////////////////////////////////////////////////////////////

  d = type;
  e = type + type_len;
  while ((s < s_toofar) && (d < e - 1) && (!ParseRules::is_ws(*s)) && (*s != ';') && (*s != ',') && (*s != '/')) {
    *d++ = *s++;
  }
  *d++ = '\0';

  //////////////////////////////////////////////////////////////
  // skip remainder of text and space, then slash, then space //
  //////////////////////////////////////////////////////////////

  while ((s < s_toofar) && (*s != ';') && (*s != ',') && (*s != '/')) {
    ++s;
  }
  if ((s < s_toofar) && (*s == '/')) {
    ++s;
  }
  while ((s < s_toofar) && ParseRules::is_ws(*s)) {
    ++s;
  }

  //////////////////////////////////////////////////////////////////////////
  // scan subtype (until NUL, out of room, comma/semicolon, space, slash) //
  //////////////////////////////////////////////////////////////////////////

  d = subtype;
  e = subtype + subtype_len;
  while ((s < s_toofar) && (d < e - 1) && (!ParseRules::is_ws(*s)) && (*s != ';') && (*s != ',') && (*s != '/')) {
    *d++ = *s++;
  }
  *d++ = '\0';
}

//////////////////////////////////////////////////////////////////////////////
//
//      bool HttpCompat::do_vary_header_values_match(MIMEField *hv1, MIMEField *hv2)
//
//      This routine takes two HTTP header fields and determines
//      if their values "match", as in section 14.43 of RFC2068:
//
//        "When the cache receives a subsequent request whose Request-URI
//         specifies one or more cache entries including a Vary header, the
//         cache MUST NOT use such a cache entry to construct a response to
//         the new request unless all of the headers named in the cached
//         Vary header are present in the new request, and all of the stored
//         selecting request-headers from the previous request match the
//         corresponding headers in the new request.
//
//         The selecting request-headers from two requests are defined to
//         match if and only if the selecting request-headers in the first
//         request can be transformed to the selecting request-headers in
//         the second request by adding or removing linear whitespace (LWS)
//         at places where this is allowed by the corresponding BNF, and/or
//         combining multiple message-header fields with the same field
//         name following the rules about message headers in section 4.2."
//
//////////////////////////////////////////////////////////////////////////////
bool
HttpCompat::do_vary_header_values_match(MIMEField *hdr1, MIMEField *hdr2)
{
  // If both headers are missing, the headers match.
  if (!hdr1 && !hdr2) {
    return true;
  }

  // If one header is missing, the headers do not match.
  if (!hdr1 || !hdr2) {
    return false;
  }

  // Make sure both headers have the same number of comma-
  // separated elements.
  HdrCsvIter iter1, iter2;
  if (iter1.count_values(hdr1) != iter2.count_values(hdr2)) {
    return false;
  }

  int hdr1_val_len, hdr2_val_len;
  const char *hdr1_val = iter1.get_first(hdr1, &hdr1_val_len);
  const char *hdr2_val = iter2.get_first(hdr2, &hdr2_val_len);

  while (hdr1_val || hdr2_val) {
    if (!hdr1_val || !hdr2_val || hdr1_val_len != hdr2_val_len ||
        ParseRules::strncasecmp_eow(hdr1_val, hdr2_val, hdr1_val_len) == false) {
      return false;
    }

    hdr1_val = iter1.get_next(&hdr1_val_len);
    hdr2_val = iter2.get_next(&hdr2_val_len);
  }

  return true;
}

//////////////////////////////////////////////////////////////////////////////
//
//      float HttpCompat::find_Q_param_in_strlist(StrList *strlist);
//
//      Takes a StrList formed from semicolon-parsing a value, and returns
//      the value of the Q directive, or 1.0 by default.
//
//////////////////////////////////////////////////////////////////////////////

float
HttpCompat::find_Q_param_in_strlist(StrList *strlist)
{
  float f, this_q;
  char q_string[8];

  this_q = 1.0;
  if (HttpCompat::lookup_param_in_strlist(strlist, (char *)"q", q_string, sizeof(q_string))) {
    // coverity[secure_coding]
    if (sscanf(q_string, "%f", &f) == 1) { // parse q
      this_q = (f < 0 ? 0 : (f > 1 ? 1 : f));
    }
  }

  return (this_q);
}

//////////////////////////////////////////////////////////////////////////////
//
//      float HttpCompat::match_accept_language
//
//      This routine returns the resulting Q factor from matching the
//      content language tag <lang_str> against the Accept-Language value
//      string <acpt_str>.
//
//      It also returns the index of the particular accept list piece
//      that matches, and the length of the accept list piece that matches,
//      in case you later want to resolve quality ties by position in the
//      list, or by length of match.  In general, you want to sort the
//      results of this call first by chosen Q, then by matching_length
//      (longer is better), then by matching_index (lower is better).
//      The first matching_index value is index 1.
//
//////////////////////////////////////////////////////////////////////////////

static inline bool
does_language_range_match(const char *pattern, int pattern_len, const char *tag, int tag_len)
{
  bool match;

  while (pattern_len && tag_len && (ParseRules::ink_tolower(*pattern) == ParseRules::ink_tolower(*tag))) {
    ++pattern;
    ++tag;
    --pattern_len;
    --tag_len;
  }

  // matches if range equals tag, or if range is a lang prefix of tag
  if ((((pattern_len == 0) && (tag_len == 0)) || ((pattern_len == 0) && (*tag == '-')))) {
    match = true;
  } else {
    match = false;
  }

  return (match);
}

float
HttpCompat::match_accept_language(const char *lang_str, int lang_len, StrList *acpt_lang_list, int *matching_length,
                                  int *matching_index, bool ignore_wildcards)
{
  float Q, Q_wild;
  Str *a_value;

  Q                     = -1; // will never be returned as -1
  Q_wild                = -1; // will never be returned as -1
  int match_count       = 0;
  int wild_match_count  = 0;
  int longest_match_len = 0;

  int index        = 0;
  int Q_index      = 0;
  int Q_wild_index = 0;

  *matching_index  = 0;
  *matching_length = 0;

  ///////////////////////////////////////////////////////
  // rip the accept string into comma-separated values //
  ///////////////////////////////////////////////////////
  if (acpt_lang_list->count == 0) {
    return (0.0);
  }

  ////////////////////////////////////////
  // loop over each Accept-Language tag //
  ////////////////////////////////////////
  for (a_value = acpt_lang_list->head; a_value; a_value = a_value->next) {
    ++index;

    if (a_value->len == 0) {
      continue; // blank tag
    }

    ///////////////////////////////////////////////////////////
    // now rip the Accept-Language tag into head and Q parts //
    ///////////////////////////////////////////////////////////
    StrList a_param_list(false);
    HttpCompat::parse_semicolon_list(&a_param_list, a_value->str, static_cast<int>(a_value->len));
    if (!a_param_list.head) {
      continue;
    }

    /////////////////////////////////////////////////////////////////////
    // This algorithm is a bit weird --- the resulting Q factor is     //
    // the Q value corresponding to the LONGEST range field that       //
    // matched, or if none matched, then the Q value of any asterisk.  //
    // Also, if the lang value is "", meaning that no Content-Language //
    // was specified, this document matches all accept headers.        //
    /////////////////////////////////////////////////////////////////////
    const char *atag_str = a_param_list.head->str;
    int atag_len         = static_cast<int>(a_param_list.head->len);

    float tq = HttpCompat::find_Q_param_in_strlist(&a_param_list);

    if ((atag_len == 1) && (atag_str[0] == '*')) // wildcard
    {
      ++wild_match_count;
      if (tq > Q_wild) {
        Q_wild       = tq;
        Q_wild_index = index;
      }
    } else if (does_language_range_match(atag_str, atag_len, lang_str, lang_len)) {
      ++match_count;
      if (atag_len > longest_match_len) {
        longest_match_len = atag_len;
        Q                 = tq;
        Q_index           = index;
      } else if (atag_len == longest_match_len) // if tie, pick higher Q
      {
        if (tq > Q) {
          Q       = tq;
          Q_index = index;
        }
      }
    }
  }

  if ((ignore_wildcards == false) && wild_match_count && !match_count) {
    *matching_index  = Q_wild_index;
    *matching_length = 1;
    return (Q_wild);
  } else if (match_count > 0) // real match
  {
    *matching_index  = Q_index;
    *matching_length = longest_match_len;
    return (Q);
  } else // no match
  {
    *matching_index  = 0;
    *matching_length = 0;
    return (0.0);
  }
}

//////////////////////////////////////////////////////////////////////////////
//
//      float HttpCompat::match_accept_charset
//
//      This routine returns the resulting Q factor from matching the
//      content language tag <lang_str> against the Accept-Language value
//      string <acpt_str>.
//
//      It also returns the index of the particular accept list piece
//      that matches, and the length of the accept list piece that matches,
//      in case you later want to resolve quality ties by position in the
//      list, or by length of match.  In general, you want to sort the
//      results of this call first by chosen Q, then by matching_length
//      (longer is better), then by matching_index (lower is better).
//      The first matching_index value is index 1.
//
//////////////////////////////////////////////////////////////////////////////

// FIX: not implemented!

float
HttpCompat::match_accept_charset(const char *charset_str, int charset_len, StrList *acpt_charset_list, int *matching_index,
                                 bool ignore_wildcards)
{
  float Q, Q_wild;
  Str *a_value;

  Q                    = -1; // will never be returned as -1
  Q_wild               = -1; // will never be returned as -1
  int match_count      = 0;
  int wild_match_count = 0;

  int index        = 0;
  int Q_index      = 0;
  int Q_wild_index = 0;

  *matching_index = 0;

  ///////////////////////////////////////////////////////
  // rip the accept string into comma-separated values //
  ///////////////////////////////////////////////////////
  if (acpt_charset_list->count == 0) {
    return (0.0);
  }

  ///////////////////////////////////////
  // loop over each Accept-Charset tag //
  ///////////////////////////////////////
  for (a_value = acpt_charset_list->head; a_value; a_value = a_value->next) {
    ++index;
    if (a_value->len == 0) {
      continue; // blank tag
    }

    //////////////////////////////////////////////////////////
    // now rip the Accept-Charset tag into head and Q parts //
    //////////////////////////////////////////////////////////
    StrList a_param_list(false);
    HttpCompat::parse_semicolon_list(&a_param_list, a_value->str, static_cast<int>(a_value->len));
    if (!a_param_list.head) {
      continue;
    }

    ///////////////////////////////////////////////////////////////
    // see if the Accept-Charset tag matches the current charset //
    ///////////////////////////////////////////////////////////////
    const char *atag_str = a_param_list.head->str;
    int atag_len         = static_cast<int>(a_param_list.head->len);
    float tq             = HttpCompat::find_Q_param_in_strlist(&a_param_list);

    if ((atag_len == 1) && (atag_str[0] == '*')) // wildcard
    {
      ++wild_match_count;
      if (tq > Q_wild) {
        Q_wild       = tq;
        Q_wild_index = index;
      }
    } else if ((atag_len == charset_len) && (strncasecmp(atag_str, charset_str, charset_len) == 0)) {
      ++match_count;
      if (tq > Q) {
        Q       = tq;
        Q_index = index;
      }
    }
  }

  if ((ignore_wildcards == false) && wild_match_count && !match_count) {
    *matching_index = Q_wild_index;
    return (Q_wild);
  } else if (match_count > 0) // real match
  {
    *matching_index = Q_index;
    return (Q);
  } else // no match
  {
    *matching_index = 0;
    return (0.0);
  }
}