File: cachekey.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 (811 lines) | stat: -rw-r--r-- 25,503 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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
/*
  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.
*/

/**
 * @file cachekey.cc
 * @brief Cache key manipulation.
 */

#include <cstring> /* strlen() */
#include <sstream> /* istringstream */
#include <utility>
#include "cachekey.h"

static void
append(String &target, unsigned n)
{
  char buf[sizeof("4294967295")];
  snprintf(buf, sizeof(buf), "%u", n);
  target.append(buf);
}

static void
appendEncoded(String &target, const char *s, size_t len)
{
  if (0 == len) {
    return;
  }

  char tmp[len * 3 + 1];
  size_t written;

  /* The default table does not encode the comma, so we need to use our own table here. */
  static const unsigned char map[32] = {
    0xFF, 0xFF, 0xFF,
    0xFF,       // control
    0xB4,       // space " # %
    0x08,       // ,
    0x00,       //
    0x0A,       // < >
    0x00, 0x00, //
    0x00,       //
    0x1E, 0x80, // [ \ ] ^ `
    0x00, 0x00, //
    0x1F,       // { | } ~ DEL
    0x00, 0x00, 0x00,
    0x00, // all non-ascii characters unmodified
    0x00, 0x00, 0x00,
    0x00, //               .
    0x00, 0x00, 0x00,
    0x00, //               .
    0x00, 0x00, 0x00,
    0x00 //               .
  };

  if (TSStringPercentEncode(s, len, tmp, sizeof(tmp), &written, map) == TS_SUCCESS) {
    target.append(tmp, written);
  } else {
    /* If the encoding fails (pretty unlikely), then just append what we have.
     * This is just a best-effort encoding anyway. */
    target.append(s, len);
  }
}

template <typename ContainerType, typename Iterator>
static String
containerToString(ContainerType &c, const String &sdelim, const String &delim)
{
  String result;
  for (Iterator arg(c.begin()); arg != c.end(); ++arg) {
    result.append(arg == c.begin() ? sdelim : delim);
    result.append(*arg);
  }
  return result;
}

static void
appendToContainer(StringSet &c, const String &s)
{
  c.insert(s);
}

static void
appendToContainer(StringList &c, const String &s)
{
  c.push_back(s);
}

template <typename T>
static String
getKeyQuery(const char *query, int length, const ConfigQuery &config)
{
  std::istringstream istr(String(query, length));
  String token;
  T container;

  while (std::getline(istr, token, '&')) {
    String::size_type pos(token.find_first_of('='));
    String param(token.substr(0, pos == String::npos ? token.size() : pos));

    if (config.toBeAdded(param)) {
      ::appendToContainer(container, token);
    }
  }

  return containerToString<T, typename T::const_iterator>(container, "?", "&");
}

static void
ltrim(String &target)
{
  String::size_type p(target.find_first_not_of(' '));

  if (p != target.npos) {
    target.erase(0, p);
  }
}

static TSMLoc
nextDuplicate(TSMBuffer buffer, TSMLoc hdr, TSMLoc field)
{
  TSMLoc next = TSMimeHdrFieldNextDup(buffer, hdr, field);
  TSHandleMLocRelease(buffer, hdr, field);
  return next;
}

/**
 * @brief Iterates through all User-Agent headers and fields and classifies them using provided classifier.
 * @param c classifier
 * @param buf marshal buffer from the request
 * @param hdrs headers handle from the request
 * @param classname reference to the string where the class name will be returned
 */
static bool
classifyUserAgent(const Classifier &c, TSMBuffer buf, TSMLoc hdrs, String &classname)
{
  TSMLoc field;
  bool matched = false;

  field = TSMimeHdrFieldFind(buf, hdrs, TS_MIME_FIELD_USER_AGENT, TS_MIME_LEN_USER_AGENT);
  while (field != TS_NULL_MLOC && !matched) {
    const char *value;
    int len;
    int count = TSMimeHdrFieldValuesCount(buf, hdrs, field);

    for (int i = 0; i < count; ++i) {
      value = TSMimeHdrFieldValueStringGet(buf, hdrs, field, i, &len);
      const String val(value, len);
      if (c.classify(val, classname)) {
        matched = true;
        break;
      }
    }

    field = ::nextDuplicate(buf, hdrs, field);
  }

  TSHandleMLocRelease(buf, hdrs, field);
  return matched;
}

static String
getUri(TSMBuffer buf, TSMLoc url)
{
  String uri;
  int uriLen;
  const char *uriPtr = TSUrlStringGet(buf, url, &uriLen);
  if (nullptr != uriPtr && 0 != uriLen) {
    uri.assign(uriPtr, uriLen);
    TSfree((void *)uriPtr);
  } else {
    CacheKeyError("failed to get URI");
  }
  return uri;
}

static String
getCanonicalUrl(TSMBuffer buf, TSMLoc url, bool canonicalPrefix, bool provideDefaultKey)
{
  String canonicalUrl;

  String scheme;
  int schemeLen;
  const char *schemePtr = TSUrlSchemeGet(buf, url, &schemeLen);
  if (nullptr != schemePtr && 0 != schemeLen) {
    scheme.assign(schemePtr, schemeLen);
  } else {
    CacheKeyError("failed to get scheme");
    return canonicalUrl;
  }

  String host;
  int hostLen;
  const char *hostPtr = TSUrlHostGet(buf, url, &hostLen);
  if (nullptr != hostPtr && 0 != hostLen) {
    host.assign(hostPtr, hostLen);
  } else {
    CacheKeyError("failed to get host");
    return canonicalUrl;
  }

  String port;
  int portInt = TSUrlPortGet(buf, url);
  ::append(port, portInt);

  if (canonicalPrefix) {
    /* return the same for both regex input or default key, results in 'scheme://host:port' */
    canonicalUrl.assign(scheme).append("://").append(host).append(":").append(port);
  } else {
    if (provideDefaultKey) {
      /* return the key default - results in '/host/port' */
      canonicalUrl.assign("/").append(host).append("/").append(port);
    } else {
      /* return regex input string - results in 'host:port' (use-case kept for compatibility reasons) */
      canonicalUrl.assign(host).append(":").append(port);
    }
  }

  return canonicalUrl;
}

/**
 * @brief Constructor setting up the cache key prefix, initializing request info.
 * @param txn transaction handle.
 * @param separator cache key elements separator
 * @param uriType type of the URI used to create the cachekey ("remap" or "pristine")
 * @param rri remap request info
 */
CacheKey::CacheKey(TSHttpTxn txn, String separator, CacheKeyUriType uriType, CacheKeyKeyType keyType, TSRemapRequestInfo *rri)
  : _txn(txn), _separator(std::move(separator)), _uriType(uriType), _keyType(keyType)
{
  _key.reserve(512);

  _remap = (nullptr != rri);

  /* Get the URI and header to base the cachekey on.
   * @TODO it might make sense to add more supported URI types */

  CacheKeyDebug("setting %s from a %s plugin", getCacheKeyKeyTypeName(_keyType), _remap ? "remap" : "global");

  if (_remap) {
    if (PRISTINE == _uriType) {
      if (TS_SUCCESS != TSHttpTxnPristineUrlGet(_txn, &_buf, &_url)) {
        /* Failing here is unlikely. No action seems the only reasonable thing to do from within this plug-in */
        CacheKeyError("failed to get pristine URI handle");
        return;
      }
      CacheKeyDebug("using pristine uri '%s'", getUri(_buf, _url).c_str());
    } else {
      _buf = rri->requestBufp;
      _url = rri->requestUrl;
      CacheKeyDebug("using remap uri '%s'", getUri(_buf, _url).c_str());
    }
    _hdrs = rri->requestHdrp;
  } else {
    if (TS_SUCCESS != TSHttpTxnClientReqGet(_txn, &_buf, &_hdrs)) {
      /* Failing here is unlikely. No action seems the only reasonable thing to do from within this plug-in */
      CacheKeyError("failed to get client request handle");
      return;
    }

    if (PRISTINE == _uriType) {
      if (TS_SUCCESS != TSHttpTxnPristineUrlGet(_txn, &_buf, &_url)) {
        TSHandleMLocRelease(_buf, TS_NULL_MLOC, _hdrs);
        CacheKeyError("failed to get pristine URI handle");
        return;
      }
      CacheKeyDebug("using pristine uri '%s'", getUri(_buf, _url).c_str());
    } else {
      if (TS_SUCCESS != TSHttpHdrUrlGet(_buf, _hdrs, &_url)) {
        TSHandleMLocRelease(_buf, TS_NULL_MLOC, _hdrs);
        CacheKeyError("failed to get URI handle");
        return;
      }
      CacheKeyDebug("using post-remap uri '%s','", getUri(_buf, _url).c_str());
    }
  }
  _valid = true; /* success, we got all necessary elements - URI, headers, etc. */
}

CacheKey::~CacheKey()
{
  if (_valid) {
    /* free resources only if valid, if not valid it is assumed nothing was allocated or was freed */
    if (_remap) {
      /* _buf and _hdrs are assigned from remap info - no need to release here. */
      if (PRISTINE == _uriType) {
        if (TS_SUCCESS != TSHandleMLocRelease(_buf, TS_NULL_MLOC, _url)) {
          CacheKeyError("failed to release pristine URI handle");
        }
      }
    } else {
      if (TS_SUCCESS != TSHandleMLocRelease(_buf, TS_NULL_MLOC, _hdrs) &&
          TS_SUCCESS != TSHandleMLocRelease(_buf, TS_NULL_MLOC, _url)) {
        CacheKeyError("failed to release URI and headers handle");
      }
    }
  }
}

/**
 * @brief Append unsigned integer to the key.
 * @param number unsigned integer
 */
void
CacheKey::append(unsigned n)
{
  _key.append(_separator);
  ::append(_key, n);
}

/**
 * @brief Append a string to the key.
 * @param s string
 */
void
CacheKey::append(const String &s)
{
  _key.append(_separator);
  ::appendEncoded(_key, s.data(), s.size());
}

void
CacheKey::append(const String &s, bool useSeparator)
{
  if (useSeparator) {
    append(s);
  } else {
    _key.append(s);
  }
}

/**
 * @brief Append null-terminated C-style string to the key.
 * @param s null-terminated C-style string.
 */
void
CacheKey::append(const char *s)
{
  _key.append(_separator);
  ::appendEncoded(_key, s, strlen(s));
}

/**
 * @brief Append first n characters from array if characters pointed by s.
 * @param n number of characters
 * @param s character array pointer
 */
void
CacheKey::append(const char *s, unsigned n)
{
  _key.append(_separator);
  ::appendEncoded(_key, s, n);
}

/**
 * @brief Append to the cache key a custom prefix, capture from hots:port, capture from URI or default to host:port part of the
 * URI.
 * @note This is the only cache key component from the key which is always available.
 * @param prefix if not empty string will append the static prefix to the cache key.
 * @param prefixCapture if not empty will append regex capture/replacement from the host:port.
 * @param prefixCaptureUri if not empty will append regex capture/replacement from the whole URI.
 * @param canonicalPrefix false - use 'host:port' as starting point of all transformations, true - use 'scheme://host:port'
 * @note if both prefix and pattern are not empty prefix will be added first, followed by the results from pattern.
 */
void
CacheKey::appendPrefix(const String &prefix, Pattern &prefixCapture, Pattern &prefixCaptureUri, bool canonicalPrefix)
{
  // "true" would mean that the plugin config meant to override the default prefix, "false" means use default.
  bool customPrefix = false;

  /* For all the following operations if a canonical prefix is required then append to the key with no separator
   * to leave the door open for potential valid host name formed in the final resulting cache key. */

  if (!prefix.empty()) {
    customPrefix = true;
    append(prefix, /* useSeparator */ !canonicalPrefix);
    CacheKeyDebug("added static prefix, key: '%s'", _key.c_str());
  }

  if (!prefixCapture.empty()) {
    customPrefix = true;

    StringVector captures;
    if (prefixCapture.process(getCanonicalUrl(_buf, _url, canonicalPrefix, /* provideDefaultKey */ false), captures)) {
      for (auto &capture : captures) {
        append(capture, /* useSeparator */ !canonicalPrefix);
      }
      CacheKeyDebug("added host:port capture prefix, key: '%s'", _key.c_str());
    }
  }

  if (!prefixCaptureUri.empty()) {
    customPrefix = true;

    String uri = getUri(_buf, _url);
    if (!uri.empty()) {
      StringVector captures;
      if (prefixCaptureUri.process(uri, captures)) {
        for (auto &capture : captures) {
          append(capture, /* useSeparator */ !canonicalPrefix);
        }
        CacheKeyDebug("added URI capture prefix, key: '%s'", _key.c_str());
      }
    }
  }

  if (!customPrefix) {
    /* nothing was customized => default prefix */
    append(getCanonicalUrl(_buf, _url, canonicalPrefix, /* provideDefaultKey */ true), /* useSeparator */ false);
    CacheKeyDebug("added default prefix, key: '%s'", _key.c_str());
  }
}

/**
 * @brief Appends to the cache key the path from the URI (default), regex capture/replacement from the URI path,
 * regex capture/replacement from URI as whole.
 * @note A path is always defined for a URI, though the defined path may be empty (zero length) (RFC 3986)
 * @param pathCapture if not empty will append regex capture/replacement from the URI path
 * @param pathCaptureUri if not empty will append regex capture/replacement from the URI as a whole
 * @todo enhance, i.e. /<regex>/<replace>/
 */
void
CacheKey::appendPath(Pattern &pathCapture, Pattern &pathCaptureUri)
{
  // "true" would mean that the plugin config meant to override the default path.
  bool customPath = false;
  String path;

  int pathLen;
  const char *pathPtr = TSUrlPathGet(_buf, _url, &pathLen);
  if (nullptr != pathPtr && 0 != pathLen) {
    path.assign(pathPtr, pathLen);
  }

  if (!pathCaptureUri.empty()) {
    customPath = true;

    String uri = getUri(_buf, _url);
    if (!uri.empty()) {
      StringVector captures;
      if (pathCaptureUri.process(uri, captures)) {
        for (auto &capture : captures) {
          append(capture);
        }
        CacheKeyDebug("added URI capture (path), key: '%s'", _key.c_str());
      }
    }
  }

  if (!pathCapture.empty()) {
    customPath = true;

    // If path is empty don't even try to capture/replace.
    if (!path.empty()) {
      StringVector captures;
      if (pathCapture.process(path, captures)) {
        for (auto &capture : captures) {
          append(capture);
        }
        CacheKeyDebug("added path capture, key: '%s'", _key.c_str());
      }
    }
  }

  if (!customPath && !path.empty()) {
    append(path);
  }
}

template <class T>
void
CacheKey::processHeader(const String &name, const ConfigHeaders &config, T &dst,
                        void (*fun)(const ConfigHeaders &config, const String &name_s, const String &value_s, T &captures))
{
  TSMLoc field;

  for (field = TSMimeHdrFieldFind(_buf, _hdrs, name.c_str(), name.size()); field != TS_NULL_MLOC;
       field = ::nextDuplicate(_buf, _hdrs, field)) {
    const char *value;
    int vlen;
    int count = TSMimeHdrFieldValuesCount(_buf, _hdrs, field);

    for (int i = 0; i < count; ++i) {
      value = TSMimeHdrFieldValueStringGet(_buf, _hdrs, field, i, &vlen);
      if (value == nullptr || vlen == 0) {
        CacheKeyDebug("missing value %d for header %s", i, name.c_str());
        continue;
      }

      String value_s(value, vlen);
      fun(config, name, value_s, dst);
    }
  }
}

template <class T>
void
captureWholeHeaders(const ConfigHeaders &config, const String &name, const String &value, T &captures)
{
  CacheKeyDebug("processing header %s", name.c_str());
  if (config.toBeAdded(name)) {
    String header;
    header.append(name).append(":").append(value);
    captures.insert(header);
    CacheKeyDebug("adding header '%s: %s'", name.c_str(), value.c_str());
  } else {
    CacheKeyDebug("failed to find header '%s'", name.c_str());
  }
}

template <class T>
void
captureFromHeaders(const ConfigHeaders &config, const String &name, const String &value, T &captures)
{
  CacheKeyDebug("processing capture from header %s", name.c_str());
  auto itMp = config.getCaptures().find(name);
  if (config.getCaptures().end() != itMp) {
    itMp->second->process(value, captures);
    CacheKeyDebug("found capture pattern for header '%s'", name.c_str());
  } else {
    CacheKeyDebug("failed to find header '%s'", name.c_str());
  }
}

/**
 * @brief Append headers by following the rules specified in the header configuration object.
 * @param config header-related configuration containing information about which headers need to be appended to the key.
 * @note Add the headers to hier-part (RFC 3986), always sort them in the cache key.
 */
void
CacheKey::appendHeaders(const ConfigHeaders &config)
{
  if (!config.toBeRemoved() && !config.toBeSkipped()) {
    /* Iterating header by header is not efficient according to comments inside traffic server API,
     * Iterate over an 'include'-kind of list or the capture definitions to avoid header by header iteration.
     * @todo: revisit this when (if?) adding regex matching for headers. */

    /* Adding whole headers, iterate over "--include-header" list */
    StringSet hdrSet; /* Sort and uniquify the header list in the cache key. */
    for (auto it = config.getInclude().begin(); it != config.getInclude().end(); ++it) {
      processHeader(*it, config, hdrSet, captureWholeHeaders);
    }

    /* Append to the cache key. It doesn't make sense to have the headers unordered in the cache key. */
    String headers_key = containerToString<StringSet, StringSet::const_iterator>(hdrSet, "", _separator);
    if (!headers_key.empty()) {
      append(headers_key);
    }
  }

  if (!config.getCaptures().empty()) {
    /* Adding captures from headers, iterate over "--capture-header" definitions */
    StringVector hdrCaptures;
    for (auto it = config.getCaptures().begin(); it != config.getCaptures().end(); ++it) {
      processHeader(it->first, config, hdrCaptures, captureFromHeaders);
    }

    /* Append to the cache key. Add the captures in the order capture definitions are captured / specified */
    for (auto &capture : hdrCaptures) {
      append(capture);
    }
  }
}

/**
 * @brief Append cookies by following the rules specified in the cookies config object.
 * @param config cookies-related configuration containing information about which cookies need to be appended to the key.
 * @note Add the cookies to "hier-part" (RFC 3986), always sort them in the cache key.
 */
void
CacheKey::appendCookies(const ConfigCookies &config)
{
  if (config.toBeRemoved() || config.toBeSkipped()) {
    /* Don't append any cookies to the cache key. */
    return;
  }

  TSMLoc field;
  StringSet cset; /* sort and uniquify the cookies list in the cache key */

  for (field = TSMimeHdrFieldFind(_buf, _hdrs, TS_MIME_FIELD_COOKIE, TS_MIME_LEN_COOKIE); field != TS_NULL_MLOC;
       field = ::nextDuplicate(_buf, _hdrs, field)) {
    int count = TSMimeHdrFieldValuesCount(_buf, _hdrs, field);

    for (int i = 0; i < count; ++i) {
      const char *value;
      int len;

      value = TSMimeHdrFieldValueStringGet(_buf, _hdrs, field, i, &len);
      if (value == nullptr || len == 0) {
        continue;
      }

      std::istringstream istr(String(value, len));
      String cookie;

      while (std::getline(istr, cookie, ';')) {
        ::ltrim(cookie); // Trim leading spaces.

        String::size_type pos(cookie.find_first_of('='));
        String name(cookie.substr(0, pos == String::npos ? cookie.size() : pos));

        /* We only add it to the cache key it is in the cookie set. */
        if (config.toBeAdded(name)) {
          cset.insert(cookie);
        }
      }
    }
  }

  /* We are iterating over the cookies in client order,
   * but the cache key needs a stable ordering, so we sort via std::set. */
  String cookies_keys = containerToString<StringSet, StringSet::const_iterator>(cset, "", ";");
  if (!cookies_keys.empty()) {
    append(cookies_keys);
  }
}

/**
 * @brief Append query parameters by following the rules specified in the query configuration object.
 * @param config query configuration containing information about which query parameters need to be appended to the key.
 * @note Keep the query parameters in the "query part" (RFC 3986).
 */
void
CacheKey::appendQuery(const ConfigQuery &config)
{
  /* No query parameters in the cache key? */
  if (config.toBeRemoved()) {
    return;
  }

  const char *query;
  int length;

  query = TSUrlHttpQueryGet(_buf, _url, &length);
  if (query == nullptr || length == 0) {
    return;
  }

  /* If need to skip all other rules just append the whole query to the key. */
  if (config.toBeSkipped()) {
    _key.append("?");
    _key.append(query, length);
    return;
  }

  /* Use the corresponding container based on whether we need
   * to sort the parameters (set) or keep the order (list) */
  String keyQuery;
  if (config.toBeSorted()) {
    keyQuery = getKeyQuery<StringSet>(query, length, config);
  } else {
    keyQuery = getKeyQuery<StringList>(query, length, config);
  }

  if (!keyQuery.empty()) {
    _key.append(keyQuery);
  }
}

/**
 * @brief Append User-Agent header captures specified in the Pattern configuration object.
 *
 * Apply given PCRE pattern/replacement to the first User-Agent value, and append any captured portions to cache key.
 * @param config PCRE pattern which contains capture groups.
 * @todo: TBD if ignoring the comma in the header as a field separator is generic enough.
 * @note Add the UA captures to hier-part (RFC 3986) in the original order.
 */
void
CacheKey::appendUaCaptures(Pattern &config)
{
  if (config.empty()) {
    return;
  }

  TSMLoc field;
  const char *value;
  int len;

  field = TSMimeHdrFieldFind(_buf, _hdrs, TS_MIME_FIELD_USER_AGENT, TS_MIME_LEN_USER_AGENT);
  if (field == TS_NULL_MLOC) {
    CacheKeyDebug("missing %.*s header", TS_MIME_LEN_USER_AGENT, TS_MIME_FIELD_USER_AGENT);
    return;
  }

  /* Now, strictly speaking, the User-Agent header should not contain a comma,
   * because that's really a field separator (RFC 2616). Unfortunately, the
   * iOS apps will send an embedded comma and we have to deal with it as if
   * it was a single header. */
  value = TSMimeHdrFieldValueStringGet(_buf, _hdrs, field, -1, &len);
  if (value && len) {
    String val(value, len);
    StringVector captures;

    if (config.process(val, captures)) {
      for (auto &capture : captures) {
        append(capture);
      }
    }
  }

  TSHandleMLocRelease(_buf, _hdrs, field);
}

/**
 * @brief Append the class name based on the User-Agent classification using the provided classifier.
 * @param classifier User-Agent header classifier which will return a single class name to be added to the key.
 * @return true if classification successful, false if no match was found.
 * @note Add the class to hier-part (RFC 3986).
 */
bool
CacheKey::appendUaClass(Classifier &classifier)
{
  String classname;
  bool matched = ::classifyUserAgent(classifier, _buf, _hdrs, classname);

  if (matched) {
    append(classname);
  } else {
    /* @todo: TBD do we need a default class name to be added to the key? */
  }

  return matched;
}

/**
 * @brief Update cache key.
 * @return true if success, false if failed to set the cache key.
 */
bool
CacheKey::finalize() const
{
  bool res = false;
  String msg;

  CacheKeyDebug("finalizing %s '%s' from a %s plugin", getCacheKeyKeyTypeName(_keyType), _key.c_str(),
                (_remap ? "remap" : "global"));
  switch (_keyType) {
  case CACHE_KEY: {
    if (TS_SUCCESS == TSCacheUrlSet(_txn, &(_key[0]), _key.size())) {
      /* Set cache key successfully */
      msg.assign("set cache key to ").append(_key);
      res = true;
    } else {
      if (_remap) {
        /* Remap instance. Always runs first by design (before TS_HTTP_POST_REMAP_HOOK) */
        msg.assign("failed to set cache key");
      } else {
        /* Global instance. We would fail and get here if a per-remap instance has already set the cache key
         * (currently TSCacheUrlSet() can be called only once successfully). Don't error, just debug.
         * @todo avoid the consecutive attempts and error only on unexpected failures. */
        msg.assign("failed to set cache key");
      }
    }
  } break;
  case PARENT_SELECTION_URL: {
    /* parent selection */
    const char *start = _key.c_str();
    const char *end   = _key.c_str() + _key.length();
    TSMLoc new_url_loc;
    if (TS_SUCCESS == TSUrlCreate(_buf, &new_url_loc)) {
      if (TS_PARSE_DONE == TSUrlParse(_buf, new_url_loc, &start, end)) {
        if (TS_SUCCESS == TSHttpTxnParentSelectionUrlSet(_txn, _buf, new_url_loc)) {
          msg.assign("set parent selection URL to ").append(_key);
          res = true;
        } else {
          msg.assign("failed to set parent selection URL");
        }
      } else {
        msg.assign("failed to parse parent selection URL");
      }
      TSHandleMLocRelease(_buf, TS_NULL_MLOC, new_url_loc);
    } else {
      msg.assign("failed to create parent selection URL");
    }
  } break;
  default: {
    msg.assign("unknown target URI type");
  } break;
  }

  /* Report status - debug level in case of success, error in case of failure.
   * Since getting effective URI is expensive add it only in case of failure */
  if (res) {
    CacheKeyDebug("%.*s", static_cast<int>(msg.length()), msg.c_str());
  } else {
    int len;
    char *url = TSHttpTxnEffectiveUrlStringGet(_txn, &len);
    if (nullptr != url) {
      msg.append(" for url ").append(url, len);
      TSfree(url);
    }
    CacheKeyError("%.*s", static_cast<int>(msg.length()), msg.c_str());
  }
  return res;
}