1 /*
2   Licensed to the Apache Software Foundation (ASF) under one
3   or more contributor license agreements.  See the NOTICE file
4   distributed with this work for additional information
5   regarding copyright ownership.  The ASF licenses this file
6   to you under the Apache License, Version 2.0 (the
7   "License"); you may not use this file except in compliance
8   with the License.  You may obtain a copy of the License at
9 
10   http://www.apache.org/licenses/LICENSE-2.0
11 
12   Unless required by applicable law or agreed to in writing, software
13   distributed under the License is distributed on an "AS IS" BASIS,
14   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   See the License for the specific language governing permissions and
16   limitations under the License.
17 */
18 
19 /**
20  * @file cachekey.cc
21  * @brief Cache key manipulation.
22  */
23 
24 #include <cstring> /* strlen() */
25 #include <sstream> /* istringstream */
26 #include <utility>
27 #include "cachekey.h"
28 
29 static void
append(String & target,unsigned n)30 append(String &target, unsigned n)
31 {
32   char buf[sizeof("4294967295")];
33   snprintf(buf, sizeof(buf), "%u", n);
34   target.append(buf);
35 }
36 
37 static void
appendEncoded(String & target,const char * s,size_t len)38 appendEncoded(String &target, const char *s, size_t len)
39 {
40   if (0 == len) {
41     return;
42   }
43 
44   char tmp[len * 2];
45   size_t written;
46 
47   /* The default table does not encode the comma, so we need to use our own table here. */
48   static const unsigned char map[32] = {
49     0xFF, 0xFF, 0xFF,
50     0xFF,       // control
51     0xB4,       // space " # %
52     0x08,       // ,
53     0x00,       //
54     0x0A,       // < >
55     0x00, 0x00, //
56     0x00,       //
57     0x1E, 0x80, // [ \ ] ^ `
58     0x00, 0x00, //
59     0x1F,       // { | } ~ DEL
60     0x00, 0x00, 0x00,
61     0x00, // all non-ascii characters unmodified
62     0x00, 0x00, 0x00,
63     0x00, //               .
64     0x00, 0x00, 0x00,
65     0x00, //               .
66     0x00, 0x00, 0x00,
67     0x00 //               .
68   };
69 
70   if (TSStringPercentEncode(s, len, tmp, sizeof(tmp), &written, map) == TS_SUCCESS) {
71     target.append(tmp, written);
72   } else {
73     /* If the encoding fails (pretty unlikely), then just append what we have.
74      * This is just a best-effort encoding anyway. */
75     target.append(s, len);
76   }
77 }
78 
79 template <typename ContainerType, typename Iterator>
80 static String
containerToString(ContainerType & c,const String & sdelim,const String & delim)81 containerToString(ContainerType &c, const String &sdelim, const String &delim)
82 {
83   String result;
84   for (Iterator arg(c.begin()); arg != c.end(); ++arg) {
85     result.append(arg == c.begin() ? sdelim : delim);
86     result.append(*arg);
87   }
88   return result;
89 }
90 
91 static void
appendToContainer(StringSet & c,const String & s)92 appendToContainer(StringSet &c, const String &s)
93 {
94   c.insert(s);
95 }
96 
97 static void
appendToContainer(StringList & c,const String & s)98 appendToContainer(StringList &c, const String &s)
99 {
100   c.push_back(s);
101 }
102 
103 template <typename T>
104 static String
getKeyQuery(const char * query,int length,const ConfigQuery & config)105 getKeyQuery(const char *query, int length, const ConfigQuery &config)
106 {
107   std::istringstream istr(String(query, length));
108   String token;
109   T container;
110 
111   while (std::getline(istr, token, '&')) {
112     String::size_type pos(token.find_first_of('='));
113     String param(token.substr(0, pos == String::npos ? token.size() : pos));
114 
115     if (config.toBeAdded(param)) {
116       ::appendToContainer(container, token);
117     }
118   }
119 
120   return containerToString<T, typename T::const_iterator>(container, "?", "&");
121 }
122 
123 static void
ltrim(String & target)124 ltrim(String &target)
125 {
126   String::size_type p(target.find_first_not_of(' '));
127 
128   if (p != target.npos) {
129     target.erase(0, p);
130   }
131 }
132 
133 static TSMLoc
nextDuplicate(TSMBuffer buffer,TSMLoc hdr,TSMLoc field)134 nextDuplicate(TSMBuffer buffer, TSMLoc hdr, TSMLoc field)
135 {
136   TSMLoc next = TSMimeHdrFieldNextDup(buffer, hdr, field);
137   TSHandleMLocRelease(buffer, hdr, field);
138   return next;
139 }
140 
141 /**
142  * @brief Iterates through all User-Agent headers and fields and classifies them using provided classifier.
143  * @param c classifier
144  * @param buf marshal buffer from the request
145  * @param hdrs headers handle from the request
146  * @param classname reference to the string where the class name will be returned
147  */
148 static bool
classifyUserAgent(const Classifier & c,TSMBuffer buf,TSMLoc hdrs,String & classname)149 classifyUserAgent(const Classifier &c, TSMBuffer buf, TSMLoc hdrs, String &classname)
150 {
151   TSMLoc field;
152   bool matched = false;
153 
154   field = TSMimeHdrFieldFind(buf, hdrs, TS_MIME_FIELD_USER_AGENT, TS_MIME_LEN_USER_AGENT);
155   while (field != TS_NULL_MLOC && !matched) {
156     const char *value;
157     int len;
158     int count = TSMimeHdrFieldValuesCount(buf, hdrs, field);
159 
160     for (int i = 0; i < count; ++i) {
161       value = TSMimeHdrFieldValueStringGet(buf, hdrs, field, i, &len);
162       const String val(value, len);
163       if (c.classify(val, classname)) {
164         matched = true;
165         break;
166       }
167     }
168 
169     field = ::nextDuplicate(buf, hdrs, field);
170   }
171 
172   TSHandleMLocRelease(buf, hdrs, field);
173   return matched;
174 }
175 
176 static String
getUri(TSMBuffer buf,TSMLoc url)177 getUri(TSMBuffer buf, TSMLoc url)
178 {
179   String uri;
180   int uriLen;
181   const char *uriPtr = TSUrlStringGet(buf, url, &uriLen);
182   if (nullptr != uriPtr && 0 != uriLen) {
183     uri.assign(uriPtr, uriLen);
184     TSfree((void *)uriPtr);
185   } else {
186     CacheKeyError("failed to get URI");
187   }
188   return uri;
189 }
190 
191 static String
getCanonicalUrl(TSMBuffer buf,TSMLoc url,bool canonicalPrefix,bool provideDefaultKey)192 getCanonicalUrl(TSMBuffer buf, TSMLoc url, bool canonicalPrefix, bool provideDefaultKey)
193 {
194   String canonicalUrl;
195 
196   String scheme;
197   int schemeLen;
198   const char *schemePtr = TSUrlSchemeGet(buf, url, &schemeLen);
199   if (nullptr != schemePtr && 0 != schemeLen) {
200     scheme.assign(schemePtr, schemeLen);
201   } else {
202     CacheKeyError("failed to get scheme");
203     return canonicalUrl;
204   }
205 
206   String host;
207   int hostLen;
208   const char *hostPtr = TSUrlHostGet(buf, url, &hostLen);
209   if (nullptr != hostPtr && 0 != hostLen) {
210     host.assign(hostPtr, hostLen);
211   } else {
212     CacheKeyError("failed to get host");
213     return canonicalUrl;
214   }
215 
216   String port;
217   int portInt = TSUrlPortGet(buf, url);
218   ::append(port, portInt);
219 
220   if (canonicalPrefix) {
221     /* return the same for both regex input or default key, results in 'scheme://host:port' */
222     canonicalUrl.assign(scheme).append("://").append(host).append(":").append(port);
223   } else {
224     if (provideDefaultKey) {
225       /* return the key default - results in '/host/port' */
226       canonicalUrl.assign("/").append(host).append("/").append(port);
227     } else {
228       /* return regex input string - results in 'host:port' (use-case kept for compatibility reasons) */
229       canonicalUrl.assign(host).append(":").append(port);
230     }
231   }
232 
233   return canonicalUrl;
234 }
235 
236 /**
237  * @brief Constructor setting up the cache key prefix, initializing request info.
238  * @param txn transaction handle.
239  * @param separator cache key elements separator
240  * @param uriType type of the URI used to create the cachekey ("remap" or "pristine")
241  * @param rri remap request info
242  */
CacheKey(TSHttpTxn txn,String separator,CacheKeyUriType uriType,CacheKeyKeyType keyType,TSRemapRequestInfo * rri)243 CacheKey::CacheKey(TSHttpTxn txn, String separator, CacheKeyUriType uriType, CacheKeyKeyType keyType, TSRemapRequestInfo *rri)
244   : _txn(txn), _separator(std::move(separator)), _uriType(uriType), _keyType(keyType)
245 {
246   _key.reserve(512);
247 
248   _remap = (nullptr != rri);
249 
250   /* Get the URI and header to base the cachekey on.
251    * @TODO it might make sense to add more supported URI types */
252 
253   CacheKeyDebug("setting %s from a %s plugin", getCacheKeyKeyTypeName(_keyType), _remap ? "remap" : "global");
254 
255   if (_remap) {
256     if (PRISTINE == _uriType) {
257       if (TS_SUCCESS != TSHttpTxnPristineUrlGet(_txn, &_buf, &_url)) {
258         /* Failing here is unlikely. No action seems the only reasonable thing to do from within this plug-in */
259         CacheKeyError("failed to get pristine URI handle");
260         return;
261       }
262       CacheKeyDebug("using pristine uri '%s'", getUri(_buf, _url).c_str());
263     } else {
264       _buf = rri->requestBufp;
265       _url = rri->requestUrl;
266       CacheKeyDebug("using remap uri '%s'", getUri(_buf, _url).c_str());
267     }
268     _hdrs = rri->requestHdrp;
269   } else {
270     if (TS_SUCCESS != TSHttpTxnClientReqGet(_txn, &_buf, &_hdrs)) {
271       /* Failing here is unlikely. No action seems the only reasonable thing to do from within this plug-in */
272       CacheKeyError("failed to get client request handle");
273       return;
274     }
275 
276     if (PRISTINE == _uriType) {
277       if (TS_SUCCESS != TSHttpTxnPristineUrlGet(_txn, &_buf, &_url)) {
278         TSHandleMLocRelease(_buf, TS_NULL_MLOC, _hdrs);
279         CacheKeyError("failed to get pristine URI handle");
280         return;
281       }
282       CacheKeyDebug("using pristine uri '%s'", getUri(_buf, _url).c_str());
283     } else {
284       if (TS_SUCCESS != TSHttpHdrUrlGet(_buf, _hdrs, &_url)) {
285         TSHandleMLocRelease(_buf, TS_NULL_MLOC, _hdrs);
286         CacheKeyError("failed to get URI handle");
287         return;
288       }
289       CacheKeyDebug("using post-remap uri '%s','", getUri(_buf, _url).c_str());
290     }
291   }
292   _valid = true; /* success, we got all necessary elements - URI, headers, etc. */
293 }
294 
~CacheKey()295 CacheKey::~CacheKey()
296 {
297   if (_valid) {
298     /* free resources only if valid, if not valid it is assumed nothing was allocated or was freed */
299     if (_remap) {
300       /* _buf and _hdrs are assigned from remap info - no need to release here. */
301       if (PRISTINE == _uriType) {
302         if (TS_SUCCESS != TSHandleMLocRelease(_buf, TS_NULL_MLOC, _url)) {
303           CacheKeyError("failed to release pristine URI handle");
304         }
305       }
306     } else {
307       if (TS_SUCCESS != TSHandleMLocRelease(_buf, TS_NULL_MLOC, _hdrs) &&
308           TS_SUCCESS != TSHandleMLocRelease(_buf, TS_NULL_MLOC, _url)) {
309         CacheKeyError("failed to release URI and headers handle");
310       }
311     }
312   }
313 }
314 
315 /**
316  * @brief Append unsigned integer to the key.
317  * @param number unsigned integer
318  */
319 void
append(unsigned n)320 CacheKey::append(unsigned n)
321 {
322   _key.append(_separator);
323   ::append(_key, n);
324 }
325 
326 /**
327  * @brief Append a string to the key.
328  * @param s string
329  */
330 void
append(const String & s)331 CacheKey::append(const String &s)
332 {
333   _key.append(_separator);
334   ::appendEncoded(_key, s.data(), s.size());
335 }
336 
337 void
append(const String & s,bool useSeparator)338 CacheKey::append(const String &s, bool useSeparator)
339 {
340   if (useSeparator) {
341     append(s);
342   } else {
343     _key.append(s);
344   }
345 }
346 
347 /**
348  * @brief Append null-terminated C-style string to the key.
349  * @param s null-terminated C-style string.
350  */
351 void
append(const char * s)352 CacheKey::append(const char *s)
353 {
354   _key.append(_separator);
355   ::appendEncoded(_key, s, strlen(s));
356 }
357 
358 /**
359  * @brief Append first n characters from array if characters pointed by s.
360  * @param n number of characters
361  * @param s character array pointer
362  */
363 void
append(const char * s,unsigned n)364 CacheKey::append(const char *s, unsigned n)
365 {
366   _key.append(_separator);
367   ::appendEncoded(_key, s, n);
368 }
369 
370 /**
371  * @brief Append to the cache key a custom prefix, capture from hots:port, capture from URI or default to host:port part of the
372  * URI.
373  * @note This is the only cache key component from the key which is always available.
374  * @param prefix if not empty string will append the static prefix to the cache key.
375  * @param prefixCapture if not empty will append regex capture/replacement from the host:port.
376  * @param prefixCaptureUri if not empty will append regex capture/replacement from the whole URI.
377  * @param canonicalPrefix false - use 'host:port' as starting point of all transformations, true - use 'scheme://host:port'
378  * @note if both prefix and pattern are not empty prefix will be added first, followed by the results from pattern.
379  */
380 void
appendPrefix(const String & prefix,Pattern & prefixCapture,Pattern & prefixCaptureUri,bool canonicalPrefix)381 CacheKey::appendPrefix(const String &prefix, Pattern &prefixCapture, Pattern &prefixCaptureUri, bool canonicalPrefix)
382 {
383   // "true" would mean that the plugin config meant to override the default prefix, "false" means use default.
384   bool customPrefix = false;
385 
386   /* For all the following operations if a canonical prefix is required then appned to the key with no separator
387    * to leave the door open for potential valid host name formed in the final resulting cache key. */
388 
389   if (!prefix.empty()) {
390     customPrefix = true;
391     append(prefix, /* useSeparator */ !canonicalPrefix);
392     CacheKeyDebug("added static prefix, key: '%s'", _key.c_str());
393   }
394 
395   if (!prefixCapture.empty()) {
396     customPrefix = true;
397 
398     StringVector captures;
399     if (prefixCapture.process(getCanonicalUrl(_buf, _url, canonicalPrefix, /* provideDefaultKey */ false), captures)) {
400       for (auto &capture : captures) {
401         append(capture, /* useSeparator */ !canonicalPrefix);
402       }
403       CacheKeyDebug("added host:port capture prefix, key: '%s'", _key.c_str());
404     }
405   }
406 
407   if (!prefixCaptureUri.empty()) {
408     customPrefix = true;
409 
410     String uri = getUri(_buf, _url);
411     if (!uri.empty()) {
412       StringVector captures;
413       if (prefixCaptureUri.process(uri, captures)) {
414         for (auto &capture : captures) {
415           append(capture, /* useSeparator */ !canonicalPrefix);
416         }
417         CacheKeyDebug("added URI capture prefix, key: '%s'", _key.c_str());
418       }
419     }
420   }
421 
422   if (!customPrefix) {
423     /* nothing was customized => default prefix */
424     append(getCanonicalUrl(_buf, _url, canonicalPrefix, /* provideDefaultKey */ true), /* useSeparator */ false);
425     CacheKeyDebug("added default prefix, key: '%s'", _key.c_str());
426   }
427 }
428 
429 /**
430  * @brief Appends to the cache key the path from the URI (default), regex capture/replacement from the URI path,
431  * regex capture/replacement from URI as whole.
432  * @note A path is always defined for a URI, though the defined path may be empty (zero length) (RFC 3986)
433  * @param pathCapture if not empty will append regex capture/replacement from the URI path
434  * @param pathCaptureUri if not empty will append regex capture/replacement from the URI as a whole
435  * @todo enhance, i.e. /<regex>/<replace>/
436  */
437 void
appendPath(Pattern & pathCapture,Pattern & pathCaptureUri)438 CacheKey::appendPath(Pattern &pathCapture, Pattern &pathCaptureUri)
439 {
440   // "true" would mean that the plugin config meant to override the default path.
441   bool customPath = false;
442   String path;
443 
444   int pathLen;
445   const char *pathPtr = TSUrlPathGet(_buf, _url, &pathLen);
446   if (nullptr != pathPtr && 0 != pathLen) {
447     path.assign(pathPtr, pathLen);
448   }
449 
450   if (!pathCaptureUri.empty()) {
451     customPath = true;
452 
453     String uri = getUri(_buf, _url);
454     if (!uri.empty()) {
455       StringVector captures;
456       if (pathCaptureUri.process(uri, captures)) {
457         for (auto &capture : captures) {
458           append(capture);
459         }
460         CacheKeyDebug("added URI capture (path), key: '%s'", _key.c_str());
461       }
462     }
463   }
464 
465   if (!pathCapture.empty()) {
466     customPath = true;
467 
468     // If path is empty don't even try to capture/replace.
469     if (!path.empty()) {
470       StringVector captures;
471       if (pathCapture.process(path, captures)) {
472         for (auto &capture : captures) {
473           append(capture);
474         }
475         CacheKeyDebug("added path capture, key: '%s'", _key.c_str());
476       }
477     }
478   }
479 
480   if (!customPath && !path.empty()) {
481     append(path);
482   }
483 }
484 
485 template <class T>
486 void
processHeader(const String & name,const ConfigHeaders & config,T & dst,void (* fun)(const ConfigHeaders & config,const String & name_s,const String & value_s,T & captures))487 CacheKey::processHeader(const String &name, const ConfigHeaders &config, T &dst,
488                         void (*fun)(const ConfigHeaders &config, const String &name_s, const String &value_s, T &captures))
489 {
490   TSMLoc field;
491 
492   for (field = TSMimeHdrFieldFind(_buf, _hdrs, name.c_str(), name.size()); field != TS_NULL_MLOC;
493        field = ::nextDuplicate(_buf, _hdrs, field)) {
494     const char *value;
495     int vlen;
496     int count = TSMimeHdrFieldValuesCount(_buf, _hdrs, field);
497 
498     for (int i = 0; i < count; ++i) {
499       value = TSMimeHdrFieldValueStringGet(_buf, _hdrs, field, i, &vlen);
500       if (value == nullptr || vlen == 0) {
501         CacheKeyDebug("missing value %d for header %s", i, name.c_str());
502         continue;
503       }
504 
505       String value_s(value, vlen);
506       fun(config, name, value_s, dst);
507     }
508   }
509 }
510 
511 template <class T>
512 void
captureWholeHeaders(const ConfigHeaders & config,const String & name,const String & value,T & captures)513 captureWholeHeaders(const ConfigHeaders &config, const String &name, const String &value, T &captures)
514 {
515   CacheKeyDebug("processing header %s", name.c_str());
516   if (config.toBeAdded(name)) {
517     String header;
518     header.append(name).append(":").append(value);
519     captures.insert(header);
520     CacheKeyDebug("adding header '%s: %s'", name.c_str(), value.c_str());
521   } else {
522     CacheKeyDebug("failed to find header '%s'", name.c_str());
523   }
524 }
525 
526 template <class T>
527 void
captureFromHeaders(const ConfigHeaders & config,const String & name,const String & value,T & captures)528 captureFromHeaders(const ConfigHeaders &config, const String &name, const String &value, T &captures)
529 {
530   CacheKeyDebug("processing capture from header %s", name.c_str());
531   auto itMp = config.getCaptures().find(name);
532   if (config.getCaptures().end() != itMp) {
533     itMp->second->process(value, captures);
534     CacheKeyDebug("found capture pattern for header '%s'", name.c_str());
535   } else {
536     CacheKeyDebug("failed to find header '%s'", name.c_str());
537   }
538 }
539 
540 /**
541  * @brief Append headers by following the rules specified in the header configuration object.
542  * @param config header-related configuration containing information about which headers need to be appended to the key.
543  * @note Add the headers to hier-part (RFC 3986), always sort them in the cache key.
544  */
545 void
appendHeaders(const ConfigHeaders & config)546 CacheKey::appendHeaders(const ConfigHeaders &config)
547 {
548   if (!config.toBeRemoved() && !config.toBeSkipped()) {
549     /* Iterating header by header is not efficient according to comments inside traffic server API,
550      * Iterate over an 'include'-kind of list or the capture definitions to avoid header by header iteration.
551      * @todo: revisit this when (if?) adding regex matching for headers. */
552 
553     /* Adding whole headers, iterate over "--include-header" list */
554     StringSet hdrSet; /* Sort and uniquify the header list in the cache key. */
555     for (auto it = config.getInclude().begin(); it != config.getInclude().end(); ++it) {
556       processHeader(*it, config, hdrSet, captureWholeHeaders);
557     }
558 
559     /* Append to the cache key. It doesn't make sense to have the headers unordered in the cache key. */
560     String headers_key = containerToString<StringSet, StringSet::const_iterator>(hdrSet, "", _separator);
561     if (!headers_key.empty()) {
562       append(headers_key);
563     }
564   }
565 
566   if (!config.getCaptures().empty()) {
567     /* Adding captures from headers, iterate over "--capture-header" definitions */
568     StringVector hdrCaptures;
569     for (auto it = config.getCaptures().begin(); it != config.getCaptures().end(); ++it) {
570       processHeader(it->first, config, hdrCaptures, captureFromHeaders);
571     }
572 
573     /* Append to the cache key. Add the captures in the order capture definitions are captured / specified */
574     for (auto &capture : hdrCaptures) {
575       append(capture);
576     }
577   }
578 }
579 
580 /**
581  * @brief Append cookies by following the rules specified in the cookies config object.
582  * @param config cookies-related configuration containing information about which cookies need to be appended to the key.
583  * @note Add the cookies to "hier-part" (RFC 3986), always sort them in the cache key.
584  */
585 void
appendCookies(const ConfigCookies & config)586 CacheKey::appendCookies(const ConfigCookies &config)
587 {
588   if (config.toBeRemoved() || config.toBeSkipped()) {
589     /* Don't append any cookies to the cache key. */
590     return;
591   }
592 
593   TSMLoc field;
594   StringSet cset; /* sort and uniquify the cookies list in the cache key */
595 
596   for (field = TSMimeHdrFieldFind(_buf, _hdrs, TS_MIME_FIELD_COOKIE, TS_MIME_LEN_COOKIE); field != TS_NULL_MLOC;
597        field = ::nextDuplicate(_buf, _hdrs, field)) {
598     int count = TSMimeHdrFieldValuesCount(_buf, _hdrs, field);
599 
600     for (int i = 0; i < count; ++i) {
601       const char *value;
602       int len;
603 
604       value = TSMimeHdrFieldValueStringGet(_buf, _hdrs, field, i, &len);
605       if (value == nullptr || len == 0) {
606         continue;
607       }
608 
609       std::istringstream istr(String(value, len));
610       String cookie;
611 
612       while (std::getline(istr, cookie, ';')) {
613         ::ltrim(cookie); // Trim leading spaces.
614 
615         String::size_type pos(cookie.find_first_of('='));
616         String name(cookie.substr(0, pos == String::npos ? cookie.size() : pos));
617 
618         /* We only add it to the cache key it is in the cookie set. */
619         if (config.toBeAdded(name)) {
620           cset.insert(cookie);
621         }
622       }
623     }
624   }
625 
626   /* We are iterating over the cookies in client order,
627    * but the cache key needs a stable ordering, so we sort via std::set. */
628   String cookies_keys = containerToString<StringSet, StringSet::const_iterator>(cset, "", ";");
629   if (!cookies_keys.empty()) {
630     append(cookies_keys);
631   }
632 }
633 
634 /**
635  * @brief Append query parameters by following the rules specified in the query configuration object.
636  * @param config query configuration containing information about which query parameters need to be appended to the key.
637  * @note Keep the query parameters in the "query part" (RFC 3986).
638  */
639 void
appendQuery(const ConfigQuery & config)640 CacheKey::appendQuery(const ConfigQuery &config)
641 {
642   /* No query parameters in the cache key? */
643   if (config.toBeRemoved()) {
644     return;
645   }
646 
647   const char *query;
648   int length;
649 
650   query = TSUrlHttpQueryGet(_buf, _url, &length);
651   if (query == nullptr || length == 0) {
652     return;
653   }
654 
655   /* If need to skip all other rules just append the whole query to the key. */
656   if (config.toBeSkipped()) {
657     _key.append("?");
658     _key.append(query, length);
659     return;
660   }
661 
662   /* Use the corresponding container based on whether we need
663    * to sort the parameters (set) or keep the order (list) */
664   String keyQuery;
665   if (config.toBeSorted()) {
666     keyQuery = getKeyQuery<StringSet>(query, length, config);
667   } else {
668     keyQuery = getKeyQuery<StringList>(query, length, config);
669   }
670 
671   if (!keyQuery.empty()) {
672     _key.append(keyQuery);
673   }
674 }
675 
676 /**
677  * @brief Append User-Agent header captures specified in the Pattern configuration object.
678  *
679  * Apply given PCRE pattern/replacement to the first User-Agent value, and append any captured portions to cache key.
680  * @param config PCRE pattern which contains capture groups.
681  * @todo: TBD if ignoring the comma in the header as a field separator is generic enough.
682  * @note Add the UA captures to hier-part (RFC 3986) in the original order.
683  */
684 void
appendUaCaptures(Pattern & config)685 CacheKey::appendUaCaptures(Pattern &config)
686 {
687   if (config.empty()) {
688     return;
689   }
690 
691   TSMLoc field;
692   const char *value;
693   int len;
694 
695   field = TSMimeHdrFieldFind(_buf, _hdrs, TS_MIME_FIELD_USER_AGENT, TS_MIME_LEN_USER_AGENT);
696   if (field == TS_NULL_MLOC) {
697     CacheKeyDebug("missing %.*s header", TS_MIME_LEN_USER_AGENT, TS_MIME_FIELD_USER_AGENT);
698     return;
699   }
700 
701   /* Now, strictly speaking, the User-Agent header should not contain a comma,
702    * because that's really a field separator (RFC 2616). Unfortunately, the
703    * iOS apps will send an embedded comma and we have to deal with it as if
704    * it was a single header. */
705   value = TSMimeHdrFieldValueStringGet(_buf, _hdrs, field, -1, &len);
706   if (value && len) {
707     String val(value, len);
708     StringVector captures;
709 
710     if (config.process(val, captures)) {
711       for (auto &capture : captures) {
712         append(capture);
713       }
714     }
715   }
716 
717   TSHandleMLocRelease(_buf, _hdrs, field);
718 }
719 
720 /**
721  * @brief Append the class name based on the User-Agent classification using the provided classifier.
722  * @param classifier User-Agent header classifier which will return a single class name to be added to the key.
723  * @return true if classification successful, false if no match was found.
724  * @note Add the class to hier-part (RFC 3986).
725  */
726 bool
appendUaClass(Classifier & classifier)727 CacheKey::appendUaClass(Classifier &classifier)
728 {
729   String classname;
730   bool matched = ::classifyUserAgent(classifier, _buf, _hdrs, classname);
731 
732   if (matched) {
733     append(classname);
734   } else {
735     /* @todo: TBD do we need a default class name to be added to the key? */
736   }
737 
738   return matched;
739 }
740 
741 /**
742  * @brief Update cache key.
743  * @return true if success, false if failed to set the cache key.
744  */
745 bool
finalize() const746 CacheKey::finalize() const
747 {
748   bool res = false;
749   String msg;
750 
751   CacheKeyDebug("finalizing %s '%s' from a %s plugin", getCacheKeyKeyTypeName(_keyType), _key.c_str(),
752                 (_remap ? "remap" : "global"));
753   switch (_keyType) {
754   case CACHE_KEY: {
755     if (TS_SUCCESS == TSCacheUrlSet(_txn, &(_key[0]), _key.size())) {
756       /* Set cache key succesfully */
757       msg.assign("set cache key to ").append(_key);
758       res = true;
759     } else {
760       if (_remap) {
761         /* Remap instance. Always runs first by design (before TS_HTTP_POST_REMAP_HOOK) */
762         msg.assign("failed to set cache key");
763       } else {
764         /* Global instance. We would fail and get here if a per-remap instance has already set the cache key
765          * (currently TSCacheUrlSet() can be called only once successfully). Don't error, just debug.
766          * @todo avoid the consecutive attempts and error only on unexpected failures. */
767         msg.assign("failed to set cache key");
768       }
769     }
770   } break;
771   case PARENT_SELECTION_URL: {
772     /* parent selection */
773     const char *start = _key.c_str();
774     const char *end   = _key.c_str() + _key.length();
775     TSMLoc new_url_loc;
776     if (TS_SUCCESS == TSUrlCreate(_buf, &new_url_loc)) {
777       if (TS_PARSE_DONE == TSUrlParse(_buf, new_url_loc, &start, end)) {
778         if (TS_SUCCESS == TSHttpTxnParentSelectionUrlSet(_txn, _buf, new_url_loc)) {
779           msg.assign("set parent selection URL to ").append(_key);
780           res = true;
781         } else {
782           msg.assign("failed to set parent selection URL");
783         }
784       } else {
785         msg.assign("failed to parse parent selection URL");
786       }
787       TSHandleMLocRelease(_buf, TS_NULL_MLOC, new_url_loc);
788     } else {
789       msg.assign("failed to create parent selection URL");
790     }
791   } break;
792   default: {
793     msg.assign("unknown target URI type");
794   } break;
795   }
796 
797   /* Report status - debug level in case of success, error in case of failure.
798    * Since getting effective URI is expensive add it only in case of failure */
799   if (res) {
800     CacheKeyDebug("%.*s", static_cast<int>(msg.length()), msg.c_str());
801   } else {
802     int len;
803     char *url = TSHttpTxnEffectiveUrlStringGet(_txn, &len);
804     if (nullptr != url) {
805       msg.append(" for url ").append(url, len);
806       TSfree(url);
807     }
808     CacheKeyError("%.*s", static_cast<int>(msg.length()), msg.c_str());
809   }
810   return res;
811 }
812