File: configs.cc

package info (click to toggle)
trafficserver 9.2.5%2Bds-0%2Bdeb12u3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 64,964 kB
  • sloc: cpp: 345,958; ansic: 31,184; python: 25,297; sh: 7,023; makefile: 3,045; perl: 2,255; java: 277; pascal: 119; sql: 94; xml: 2
file content (662 lines) | stat: -rw-r--r-- 17,812 bytes parent folder | download
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
/*
  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 configs.cc
 * @brief Plugin configuration.
 */

#include <fstream>   /* std::ifstream */
#include <sstream>   /* std::istringstream */
#include <getopt.h>  /* getopt_long() */
#include <strings.h> /* strncasecmp() */
#include <cstring>   /* strlen() */

#include "configs.h"

template <typename ContainerType>
static void
commaSeparateString(ContainerType &c, const String &input)
{
  std::istringstream istr(input);
  String token;

  while (std::getline(istr, token, ',')) {
    c.insert(c.end(), token);
  }
}

static bool
isTrue(const char *arg)
{
  return (nullptr == arg || 0 == strncasecmp("true", arg, 4) || 0 == strncasecmp("1", arg, 1) || 0 == strncasecmp("yes", arg, 3));
}

void
ConfigElements::setExclude(const char *arg)
{
  ::commaSeparateString<StringSet>(_exclude, arg);
}

void
ConfigElements::setInclude(const char *arg)
{
  ::commaSeparateString<StringSet>(_include, arg);
}

static void
setPattern(MultiPattern &multiPattern, const char *arg)
{
  Pattern *p = new Pattern();
  if (nullptr != p && p->init(arg)) {
    multiPattern.add(p);
  } else {
    delete p;
  }
}

bool
ConfigElements::setCapture(const String &name, const String &pattern)
{
  auto it = _captures.find(name);
  if (_captures.end() == it) {
    auto mp = new MultiPattern(name);
    if (nullptr != mp) {
      _captures[name] = mp;
    } else {
      return false;
    }
  }
  setPattern(*_captures[name], pattern.c_str());
  CacheKeyDebug("added capture pattern '%s' for element '%s'", pattern.c_str(), name.c_str());
  return true;
}

void
ConfigElements::addCapture(const char *arg)
{
  StringView args(arg);
  StringView::size_type pos = args.find_first_of(':');
  if (StringView::npos != pos) {
    String name(args.substr(0, pos));
    if (!name.empty()) {
      String pattern(args.substr(pos + 1));
      if (!pattern.empty()) {
        if (!setCapture(name, pattern)) {
          CacheKeyError("failed to add capture: '%s'", arg);
        }
      } else {
        CacheKeyError("missing pattern in capture: '%s'", arg);
      }
    } else {
      CacheKeyError("missing element name in capture: %s", arg);
    }
  } else {
    CacheKeyError("invalid capture: %s, should be 'name:<capture_definition>", arg);
  }
}

void
ConfigElements::setExcludePatterns(const char *arg)
{
  setPattern(_excludePatterns, arg);
}

void
ConfigElements::setIncludePatterns(const char *arg)
{
  setPattern(_includePatterns, arg);
}

void
ConfigElements::setSort(const char *arg)
{
  _sort = ::isTrue(arg);
}

void
ConfigElements::setRemove(const char *arg)
{
  _remove = ::isTrue(arg);
}

bool
ConfigElements::toBeRemoved() const
{
  return _remove;
}

bool
ConfigElements::toBeSkipped() const
{
  return _skip;
}

bool
ConfigElements::toBeSorted() const
{
  return _sort;
}

bool
ConfigElements::toBeAdded(const String &element) const
{
  /* Exclude the element if it is in the exclusion list. If the list is empty don't exclude anything. */
  bool exclude = (!_exclude.empty() && _exclude.find(element) != _exclude.end()) ||
                 (!_excludePatterns.empty() && _excludePatterns.match(element));
  CacheKeyDebug("%s '%s' %s the 'exclude' rule", name().c_str(), element.c_str(), exclude ? "matches" : "does not match");

  /* Include the element only if it is in the inclusion list. If the list is empty include everything. */
  bool include =
    ((_include.empty() && _includePatterns.empty()) || _include.find(element) != _include.end()) || _includePatterns.match(element);
  CacheKeyDebug("%s '%s' %s the 'include' rule", name().c_str(), element.c_str(), include ? "matches" : "do not match");

  if (include && !exclude) {
    CacheKeyDebug("%s '%s' should be added to cache key", name().c_str(), element.c_str());
    return true;
  }

  CacheKeyDebug("%s '%s' should not be added to cache key", name().c_str(), element.c_str());
  return false;
}

inline bool
ConfigElements::noIncludeExcludeRules() const
{
  return _exclude.empty() && _excludePatterns.empty() && _include.empty() && _includePatterns.empty();
}

ConfigElements::~ConfigElements()
{
  for (auto &_capture : _captures) {
    delete _capture.second;
  }
}

/**
 * @brief finalizes the query parameters related configuration.
 *
 * If we don't have any inclusions or exclusions and don't have to sort, we don't need to do anything
 * with the query string. Include the whole original query in the cache key.
 */
bool
ConfigQuery::finalize()
{
  _skip = noIncludeExcludeRules() && !_sort;
  return true;
}

const String ConfigQuery::_NAME = "query parameter";
inline const String &
ConfigQuery::name() const
{
  return _NAME;
}

bool
ConfigMatrix::finalize()
{
  _remove = noIncludeExcludeRules();
  return true;
}

const String ConfigMatrix::_NAME = "matrix parameter";
inline const String &
ConfigMatrix::name() const
{
  return _NAME;
}

/**
 * @briefs finalizes the headers related configuration.
 *
 * If the all include and exclude lists are empty, including patterns, then there is no headers to be included.
 */
bool
ConfigHeaders::finalize()
{
  _remove = noIncludeExcludeRules();
  return true;
}

const String ConfigHeaders::_NAME = "header";
inline const String &
ConfigHeaders::name() const
{
  return _NAME;
}

/**
 * @brief finalizes the cookies related configuration.
 *
 * If the all include and exclude lists are empty, including pattern, then there is no cookies to be included.
 */
bool
ConfigCookies::finalize()
{
  _remove = noIncludeExcludeRules();
  return true;
}

const String ConfigCookies::_NAME = "cookie";
inline const String &
ConfigCookies::name() const
{
  return _NAME;
}

/**
 * @brief Accessor method for getting include list only for headers config.
 *
 * We would not need to drill this hole in the design if there was an efficient way to iterate through the headers in the traffic
 * server API (inefficiency mentioned in ts/ts.h), iterating through the "include" list should be good enough work-around.
 */
const StringSet &
ConfigHeaders::getInclude() const
{
  return _include;
}

/**
 * @brief Rebase a relative path onto the configuration directory.
 */
static String
makeConfigPath(const String &path)
{
  if (path.empty() || path[0] == '/') {
    return path;
  }

  return String(TSConfigDirGet()) + "/" + path;
}

/**
 * @brief a helper function which loads the classifier from files.
 * @param args classname + filename in '<classname>:<filename>' format.
 * @param denylist true - load as a denylist classifier, false - allowlist.
 * @return true if successful, false otherwise.
 */
bool
Configs::loadClassifiers(const String &args, bool denylist)
{
  static const char *EXPECTED_FORMAT = "<classname>:<filename>";

  std::size_t d = args.find(':');
  if (String::npos == d) {
    CacheKeyError("failed to parse classifier string '%s', expected format: '%s'", optarg ? optarg : "null", EXPECTED_FORMAT);
    return false;
  }

  String classname(optarg, 0, d);
  String filename(optarg, d + 1, String::npos);

  if (classname.empty() || filename.empty()) {
    CacheKeyError("'<classname>' and '<filename>' in '%s' cannot be empty, expected format: '%s'", optarg ? optarg : "null",
                  EXPECTED_FORMAT);
    return false;
  }

  String path(makeConfigPath(filename));

  std::ifstream ifstr;
  String regex;
  unsigned lineno = 0;

  ifstr.open(path.c_str());
  if (!ifstr) {
    CacheKeyError("failed to load classifier '%s' from '%s'", classname.c_str(), path.c_str());
    return false;
  }

  MultiPattern *multiPattern;
  if (denylist) {
    multiPattern = new NonMatchingMultiPattern(classname);
  } else {
    multiPattern = new MultiPattern(classname);
  }
  if (nullptr == multiPattern) {
    CacheKeyError("failed to allocate classifier '%s'", classname.c_str());
    return false;
  }

  CacheKeyDebug("loading classifier '%s' from '%s'", classname.c_str(), path.c_str());

  while (std::getline(ifstr, regex)) {
    Pattern *p;
    String::size_type pos;

    ++lineno;

    // Allow #-prefixed comments.
    pos = regex.find_first_of('#');
    if (pos != String::npos) {
      regex.resize(pos);
    }

    if (regex.empty()) {
      continue;
    }

    p = new Pattern();

    if (nullptr != p && p->init(regex)) {
      if (denylist) {
        CacheKeyDebug("Added pattern '%s' to deny list '%s'", regex.c_str(), classname.c_str());
        multiPattern->add(p);
      } else {
        CacheKeyDebug("Added pattern '%s' to allow list '%s'", regex.c_str(), classname.c_str());
        multiPattern->add(p);
      }
    } else {
      CacheKeyError("%s:%u: failed to parse regex '%s'", path.c_str(), lineno, regex.c_str());
      delete p;
    }
  }

  ifstr.close();

  if (!multiPattern->empty()) {
    _classifier.add(multiPattern);
  } else {
    delete multiPattern;
  }

  return true;
}

/**
 * @brief initializes plugin configuration.
 * @param argc number of plugin parameters
 * @param argv plugin parameters
 * @param perRemapConfig boolean showing if this is per-remap config (vs global config).
 *
 */
bool
Configs::init(int argc, const char *argv[], bool perRemapConfig)
{
  static const struct option longopt[] = {
    {const_cast<char *>("exclude-params"), optional_argument, nullptr, 'a'},
    {const_cast<char *>("include-params"), optional_argument, nullptr, 'b'},
    {const_cast<char *>("include-match-params"), optional_argument, nullptr, 'c'},
    {const_cast<char *>("exclude-match-params"), optional_argument, nullptr, 'd'},
    {const_cast<char *>("sort-params"), optional_argument, nullptr, 'e'},
    {const_cast<char *>("remove-all-params"), optional_argument, nullptr, 'f'},
    {const_cast<char *>("include-headers"), optional_argument, nullptr, 'g'},
    {const_cast<char *>("include-cookies"), optional_argument, nullptr, 'h'},
    {const_cast<char *>("ua-capture"), optional_argument, nullptr, 'i'},
    {const_cast<char *>("ua-allowlist"), optional_argument, nullptr, 'j'},
    {const_cast<char *>("ua-denylist"), optional_argument, nullptr, 'k'},
    {const_cast<char *>("static-prefix"), optional_argument, nullptr, 'l'},
    {const_cast<char *>("capture-prefix"), optional_argument, nullptr, 'm'},
    {const_cast<char *>("capture-prefix-uri"), optional_argument, nullptr, 'n'},
    {const_cast<char *>("capture-path"), optional_argument, nullptr, 'o'},
    {const_cast<char *>("capture-path-uri"), optional_argument, nullptr, 'p'},
    {const_cast<char *>("remove-prefix"), optional_argument, nullptr, 'q'},
    {const_cast<char *>("remove-path"), optional_argument, nullptr, 'r'},
    {const_cast<char *>("separator"), optional_argument, nullptr, 's'},
    {const_cast<char *>("uri-type"), optional_argument, nullptr, 't'},
    {const_cast<char *>("key-type"), optional_argument, nullptr, 'u'},
    {const_cast<char *>("capture-header"), optional_argument, nullptr, 'v'},
    {const_cast<char *>("canonical-prefix"), optional_argument, nullptr, 'w'},
    /* reserve 'z' for 'config' files */
    {nullptr, 0, nullptr, 0},
  };

  bool status = true;

  /* For remap.config: argv contains the "to" and "from" URLs. Skip the first so that the second one poses as the program name.
   * For plugin.config: argv contains the plugin shared object name. Don't skip any */
  if (perRemapConfig) {
    argc--;
    argv++;
  }

  for (;;) {
    int opt;
    opt = getopt_long(argc, const_cast<char *const *>(argv), "", longopt, nullptr);

    if (opt == -1) {
      break;
    }
    CacheKeyDebug("processing %s", argv[optind - 1]);

    switch (opt) {
    case 'a': /* exclude-params */
      _query.setExclude(optarg);
      break;
    case 'b': /* include-params */
      _query.setInclude(optarg);
      break;
    case 'c': /* include-match-params */
      _query.setIncludePatterns(optarg);
      break;
    case 'd': /* exclude-match-params */
      _query.setExcludePatterns(optarg);
      break;
    case 'e': /* sort-params */
      _query.setSort(optarg);
      break;
    case 'f': /* remove-all-params */
      _query.setRemove(optarg);
      break;
    case 'g': /* include-headers */
      _headers.setInclude(optarg);
      break;
    case 'h': /* include-cookies */
      _cookies.setInclude(optarg);
      break;
    case 'i': /* ua-capture */
      if (!_uaCapture.init(optarg)) {
        CacheKeyError("failed to initialize User-Agent capture pattern '%s'", optarg);
        status = false;
      }
      break;
    case 'j': /* ua-allowlist */
      if (!loadClassifiers(optarg, /* denylist = */ false)) {
        CacheKeyError("failed to load User-Agent pattern allow-list '%s'", optarg);
        status = false;
      }
      break;
    case 'k': /* ua-denylist */
      if (!loadClassifiers(optarg, /* denylist = */ true)) {
        CacheKeyError("failed to load User-Agent pattern deny-list '%s'", optarg);
        status = false;
      }
      break;
    case 'l': /* static-prefix */
      _prefix.assign(optarg);
      CacheKeyDebug("prefix='%s'", _prefix.c_str());
      break;
    case 'm': /* capture-prefix */
      if (!_prefixCapture.init(optarg)) {
        CacheKeyError("failed to initialize prefix URI host:port capture pattern '%s'", optarg);
        status = false;
      }
      break;
    case 'n': /* capture-prefix-uri */
      if (!_prefixCaptureUri.init(optarg)) {
        CacheKeyError("failed to initialize prefix URI capture pattern '%s'", optarg);
        status = false;
      }
      break;
    case 'o': /* capture-path */
      if (!_pathCapture.init(optarg)) {
        CacheKeyError("failed to initialize path capture pattern '%s'", optarg);
        status = false;
      }
      break;
    case 'p': /* capture-path-uri */
      if (!_pathCaptureUri.init(optarg)) {
        CacheKeyError("failed to initialize path URI capture pattern '%s'", optarg);
        status = false;
      }
      break;
    case 'q': /* remove-prefix */
      _prefixToBeRemoved = isTrue(optarg);
      break;
    case 'r': /* remove-path */
      _pathToBeRemoved = isTrue(optarg);
      break;
    case 's': /* separator */
      setSeparator(optarg);
      break;
    case 't': /* uri-type */
      setUriType(optarg);
      break;
    case 'u': /* key-type */
      setKeyType(optarg);
      break;
    case 'v': /* capture-header */
      _headers.addCapture(optarg);
      break;
    case 'w': /* canonical-prefix */
      _canonicalPrefix = isTrue(optarg);
      break;
    }
  }

  status &= finalize();

  return status;
}

/**
 * @brief provides means for post-processing of the plugin parameters to finalize the configuration or to "cache" some of the
 * decisions for later use.
 * @return true if successful, false if failure.
 */
bool
Configs::finalize()
{
  if (_keyTypes.empty()) {
    CacheKeyDebug("setting cache key");
    _keyTypes = {CACHE_KEY};
  }
  return _query.finalize() && _headers.finalize() && _cookies.finalize();
}

bool
Configs::prefixToBeRemoved()
{
  return _prefixToBeRemoved;
}

bool
Configs::pathToBeRemoved()
{
  return _pathToBeRemoved;
}

bool
Configs::canonicalPrefix()
{
  return _canonicalPrefix;
}

void
Configs::setSeparator(const char *arg)
{
  if (nullptr != arg) {
    _separator.assign(arg);
  }
}

const String &
Configs::getSeparator()
{
  return _separator;
}

void
Configs::setUriType(const char *arg)
{
  if (nullptr != arg) {
    if (5 == strlen(arg) && 0 == strncasecmp(arg, "remap", 5)) {
      _uriType = CacheKeyUriType::REMAP;
      CacheKeyDebug("using remap URI type");
    } else if (8 == strlen(arg) && 0 == strncasecmp(arg, "pristine", 8)) {
      _uriType = CacheKeyUriType::PRISTINE;
      CacheKeyDebug("using pristine URI type");
    } else {
      CacheKeyError("unrecognized URI type '%s', using default 'remap'", arg);
    }
  } else {
    CacheKeyError("found an empty URI type, using default 'remap'");
  }
}

void
Configs::setKeyType(const char *arg)
{
  if (nullptr != arg) {
    StringVector types;
    ::commaSeparateString<StringVector>(types, arg);

    for (auto type : types) {
      if (9 == type.length() && 0 == strncasecmp(type.c_str(), "cache_key", 9)) {
        _keyTypes.insert(CacheKeyKeyType::CACHE_KEY);
        CacheKeyDebug("setting cache key");
      } else if (20 == type.length() && 0 == strncasecmp(type.c_str(), "parent_selection_url", 20)) {
        _keyTypes.insert(CacheKeyKeyType::PARENT_SELECTION_URL);
        CacheKeyDebug("setting parent selection URL");
      } else {
        CacheKeyError("unrecognized key type '%s', using default 'cache_key'", arg);
      }
    }
  } else {
    CacheKeyError("found an empty key type, using default 'cache_key'");
  }
}

CacheKeyUriType
Configs::getUriType()
{
  return _uriType;
}

CacheKeyKeyTypeSet &
Configs::getKeyType()
{
  return _keyTypes;
}

const char *
getCacheKeyUriTypeName(CacheKeyUriType type)
{
  switch (type) {
  case REMAP:
    return "remap";
  case PRISTINE:
    return "pristine";
  default:
    return "unknown";
  }
}

const char *
getCacheKeyKeyTypeName(CacheKeyKeyType type)
{
  switch (type) {
  case CACHE_KEY:
    return "cache key";
  case PARENT_SELECTION_URL:
    return "parent selection url";
  default:
    return "unknown";
  }
}