File: Variables.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 (460 lines) | stat: -rw-r--r-- 16,505 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
/** @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 "Variables.h"
#include "Attribute.h"
#include "Utils.h"

#include <cerrno>

using std::list;
using std::pair;
using std::string;
using namespace EsiLib;

const string Variables::EMPTY_STRING("");
const string Variables::TRUE_STRING("true");
const string Variables::VENDOR_STRING("vendor");
const string Variables::VERSION_STRING("version");
const string Variables::PLATFORM_STRING("platform");
const string Variables::SIMPLE_HEADERS[] = {string("HOST"), string("REFERER"), string("")};

const string Variables::SPECIAL_HEADERS[] = {string("ACCEPT-LANGUAGE"), string("COOKIE"), string("USER-AGENT"),
                                             string("QUERY_STRING"), string("")};

const string Variables::NORM_SIMPLE_HEADERS[] = {string("HTTP_HOST"), string("HTTP_REFERER"), string("")};

const string Variables::NORM_SPECIAL_HEADERS[] = {string("HTTP_ACCEPT_LANGUAGE"), string("HTTP_COOKIE"), string("HTTP_USER_AGENT"),
                                                  string("QUERY_STRING"),         string("HTTP_HEADER"), string("")};

inline string &
Variables::_toUpperCase(string &str) const
{
  for (char &i : str) {
    if ((i >= 'a') && (i <= 'z')) {
      i = 'A' + (i - 'a');
    }
  }
  return str;
}

inline int
Variables::_searchHeaders(const string headers[], const char *name, int name_len) const
{
  int curr_header_size;
  for (int i = 0; (curr_header_size = static_cast<int>(headers[i].size())); ++i) {
    if ((name_len == curr_header_size) && (strncasecmp(headers[i].data(), name, curr_header_size) == 0)) {
      return i;
    }
  }
  return -1;
}

inline void
Variables::_insert(StringHash &hash, const std::string &key, const std::string &value)
{
  std::pair<StringHash::iterator, bool> result = hash.insert(StringHash::value_type(key, value));
  if (!result.second) {
    result.first->second = value;
  }
}

void
Variables::populate(const HttpHeader &header)
{
  if (header.name && header.name_len && header.value && header.value_len) {
    // doing this because we can't change our const input arg
    int name_len  = (header.name_len == -1) ? strlen(header.name) : header.name_len;
    int value_len = (header.value_len == -1) ? strlen(header.value) : header.value_len;

    // we need to save the cookie string to build the jar from
    if ((name_len == 6) && (strncasecmp(header.name, "Cookie", 6) == 0)) {
      _releaseCookieJar();
      if (_cookie_str.size()) {
        _cookie_str.append(", ");
      }
      _cookie_str.append(header.value, value_len);
    }

    if (_headers_parsed) {
      _parseHeader(header.name, name_len, header.value, value_len);
    } else {
      int match_index = _searchHeaders(SIMPLE_HEADERS, header.name, name_len);
      if (match_index != -1) {
        _cached_simple_headers[match_index].push_back(string(header.value, value_len));
      } else {
        match_index = _searchHeaders(SPECIAL_HEADERS, header.name, name_len);
        if (match_index != -1) {
          _cached_special_headers[match_index].push_back(string(header.value, value_len));
        }
      }
    }
    _insert(_dict_data[HTTP_HEADER], string(header.name, name_len), string(header.value, value_len));
  }
}

inline void
Variables::_parseSimpleHeader(SimpleHeader hdr, const string &value)
{
  _debugLog(_debug_tag, "[%s] Inserting value for simple header [%s]", __FUNCTION__, SIMPLE_HEADERS[hdr].c_str());
  _simple_data[NORM_SIMPLE_HEADERS[hdr]] = value;
}

inline void
Variables::_parseSimpleHeader(SimpleHeader hdr, const char *value, int value_len)
{
  _parseSimpleHeader(hdr, string(value, value_len));
}

void
Variables::_parseSpecialHeader(SpecialHeader hdr, const char *value, int value_len)
{
  switch (hdr) {
  case HTTP_ACCEPT_LANGUAGE:
    _parseAcceptLangString(value, value_len);
    break;
  case HTTP_COOKIE:
    _parseCookieString(value, value_len);
    break;
  case HTTP_USER_AGENT:
    _parseUserAgentString(value, value_len);
    break;
  default:
    _debugLog(_debug_tag, "[%s] Skipping unrecognized header", __FUNCTION__);
    break;
  }
}

void
Variables::_parseHeader(const char *name, int name_len, const char *value, int value_len)
{
  int match_index = _searchHeaders(SIMPLE_HEADERS, name, name_len);
  if (match_index != -1) {
    _parseSimpleHeader(static_cast<SimpleHeader>(match_index), value, value_len);
  } else {
    match_index = _searchHeaders(SPECIAL_HEADERS, name, name_len);
    if (match_index != -1) {
      _parseSpecialHeader(static_cast<SpecialHeader>(match_index), value, value_len);
    } else {
      _debugLog(_debug_tag, "[%s] Unrecognized header [%.*s]", __FUNCTION__, value_len, value);
    }
  }
}

void
Variables::_parseQueryString(const char *query_string, int query_string_len)
{
  _insert(_simple_data, string("QUERY_STRING"), string(query_string, query_string_len));
  AttributeList attr_list;
  Utils::parseAttributes(query_string, query_string_len, attr_list, "&");
  for (auto &iter : attr_list) {
    _debugLog(_debug_tag, "[%s] Inserting query string variable [%.*s] with value [%.*s]", __FUNCTION__, iter.name_len, iter.name,
              iter.value_len, iter.value);
    _insert(_dict_data[QUERY_STRING], string(iter.name, iter.name_len), string(iter.value, iter.value_len));
  }
}

void
Variables::_parseCachedHeaders()
{
  _debugLog(_debug_tag, "[%s] Parsing headers", __FUNCTION__);
  for (int i = 0; i < N_SIMPLE_HEADERS; ++i) {
    for (Utils::HeaderValueList::iterator value_iter = _cached_simple_headers[i].begin();
         value_iter != _cached_simple_headers[i].end(); ++value_iter) {
      _parseSimpleHeader(static_cast<SimpleHeader>(i), *value_iter);
    }
  }
  for (int i = 0; i < N_SPECIAL_HEADERS; ++i) {
    for (Utils::HeaderValueList::iterator value_iter = _cached_special_headers[i].begin();
         value_iter != _cached_special_headers[i].end(); ++value_iter) {
      _parseSpecialHeader(static_cast<SpecialHeader>(i), value_iter->data(), value_iter->size());
    }
  }
}

const std::string &
Variables::getValue(const string &name) const
{
  if (!_headers_parsed || !_query_string_parsed) {
    // we need to do this because we want to
    // 1) present const getValue() to clients
    // 2) parse lazily (only on demand)
    Variables &non_const_self = const_cast<Variables &>(*this);
    if (!_headers_parsed) {
      non_const_self._parseCachedHeaders();
      non_const_self._headers_parsed = true;
    }
    if (!_query_string_parsed) {
      int query_string_size = static_cast<int>(_query_string.size());
      if (query_string_size) {
        non_const_self._parseQueryString(_query_string.data(), query_string_size);
        non_const_self._query_string_parsed = true;
      }
    }
  }
  string search_key(name);
  _toUpperCase(search_key);
  StringHash::const_iterator iter = _simple_data.find(search_key);
  if (iter != _simple_data.end()) {
    _debugLog(_debug_tag, "[%s] Found value [%.*s] for variable [%.*s] in simple data", __FUNCTION__, iter->second.size(),
              iter->second.data(), name.size(), name.data());
    return iter->second;
  }
  const char *header;
  int header_len;
  const char *attr;
  int attr_len;
  if (!_parseDictVariable(name, header, header_len, attr, attr_len)) {
    _debugLog(_debug_tag, "[%s] Unmatched simple variable [%.*s] not in dict variable form", __FUNCTION__, name.size(),
              name.data());
    return EMPTY_STRING;
  }
  int dict_index = _searchHeaders(NORM_SPECIAL_HEADERS, header, header_len); // ignore the HTTP_ prefix
  if (dict_index == -1) {
    _debugLog(_debug_tag, "[%s] Dict variable [%.*s] refers to unknown dictionary", __FUNCTION__, name.size(), name.data());
    return EMPTY_STRING;
  }

  // Disallow Cookie retrieval though HTTP_HEADER
  if (dict_index == HTTP_HEADER && ((attr_len == 6) && (strncasecmp(attr, "Cookie", 6) == 0))) {
    _errorLog("[%s] Cannot use HTTP_HEADER to retrieve Cookie", __FUNCTION__);
    return EMPTY_STRING;
  }

  // change variable name to use only the attribute field
  search_key.assign(attr, attr_len);

  iter = _dict_data[dict_index].find(search_key);

  if (dict_index == HTTP_ACCEPT_LANGUAGE) {
    _debugLog(_debug_tag, "[%s] Returning boolean literal for lang variable [%.*s]", __FUNCTION__, search_key.size(),
              search_key.data());
    return (iter == _dict_data[dict_index].end()) ? EMPTY_STRING : TRUE_STRING;
  }

  if (iter != _dict_data[dict_index].end()) {
    _debugLog(_debug_tag, "[%s] Found variable [%.*s] in %s dictionary with value [%.*s]", __FUNCTION__, search_key.size(),
              search_key.data(), NORM_SPECIAL_HEADERS[dict_index].c_str(), iter->second.size(), iter->second.data());
    return iter->second;
  }

  size_t cookie_part_divider = (dict_index == HTTP_COOKIE) ? search_key.find(';') : search_key.size();
  if (cookie_part_divider && (cookie_part_divider < (search_key.size() - 1))) {
    _debugLog(_debug_tag, "[%s] Cookie variable [%s] refers to sub cookie", __FUNCTION__, search_key.c_str());
    return _getSubCookieValue(search_key, cookie_part_divider);
  }

  _debugLog(_debug_tag, "[%s] Found no value for dict variable [%s]", __FUNCTION__, name.c_str());
  return EMPTY_STRING;
}

void
Variables::_parseSubCookies()
{
  StringHash &cookies = _dict_data[HTTP_COOKIE];
  for (StringHash::const_iterator it_cookie = cookies.begin(); it_cookie != cookies.end(); ++it_cookie) {
    const std::string &name  = it_cookie->first;
    const std::string &value = it_cookie->second;
    if (strchr(value.c_str(), '=') == nullptr) {
      continue;
    }

    StringHash &subcookies = _sub_cookies[name];
    AttributeList attr_list;
    Utils::parseAttributes(value.c_str(), value.length(), attr_list, "&");
    for (auto &iter : attr_list) {
      _debugLog(_debug_tag, "[%s] Inserting query string variable [%.*s] with value [%.*s]", __FUNCTION__, iter.name_len, iter.name,
                iter.value_len, iter.value);
      _insert(subcookies, string(iter.name, iter.name_len), string(iter.value, iter.value_len));
    }
  }
}

const string &
Variables::_getSubCookieValue(const string &cookie_str, size_t cookie_part_divider) const
{
  if (!_cookie_jar_created) {
    if (_cookie_str.size() == 0) {
      _debugLog(_debug_tag, "[%s] Cookie string empty; nothing to construct jar from", __FUNCTION__);
      return EMPTY_STRING;
    }

    Variables &non_const_self = const_cast<Variables &>(*this); // same reasoning as in getValue()
    non_const_self._parseSubCookies();
    non_const_self._cookie_jar_created = true;
  }

  // we need to do this as we are going to manipulate the 'divider'
  // character, and we don't need to create a copy of the string for
  // that; hence this shortcut
  string &non_const_cookie_str = const_cast<string &>(cookie_str);
  StringHash::const_iterator it_part;

  non_const_cookie_str[cookie_part_divider] = '\0';                        // make sure cookie name is NULL terminated
  const char *cookie_name                   = non_const_cookie_str.data(); /* above NULL will take effect */
  const char *part_name                     = non_const_cookie_str.c_str() + cookie_part_divider + 1;

  StringKeyHash<StringHash>::const_iterator it_cookie = _sub_cookies.find(cookie_name);
  if (it_cookie == _sub_cookies.end()) {
    _debugLog(_debug_tag, "[%s] Could not find value for cookie [%s]", __FUNCTION__, cookie_name);
    goto fail;
  }

  it_part = it_cookie->second.find(part_name);
  if (it_part == it_cookie->second.end()) {
    _debugLog(_debug_tag, "[%s] Could not find value for part [%s] of cookie [%.*s]", __FUNCTION__, part_name, cookie_part_divider,
              cookie_name);
    goto fail;
  }

  _debugLog(_debug_tag, "[%s] Got value [%s] for cookie name [%.*s] and part [%s]", __FUNCTION__, it_part->second.c_str(),
            cookie_part_divider, cookie_name, part_name);

  non_const_cookie_str[cookie_part_divider] = ';'; // restore before returning

  this->_cached_sub_cookie_value.assign(it_part->second);
  return this->_cached_sub_cookie_value;

fail:
  non_const_cookie_str[cookie_part_divider] = ';'; // restore before returning
  return EMPTY_STRING;
}

void
Variables::clear()
{
  _simple_data.clear();
  for (int i = 0; i < N_SPECIAL_HEADERS; ++i) {
    _dict_data[i].clear();
    _cached_special_headers[i].clear();
  }
  for (auto &_cached_simple_header : _cached_simple_headers) {
    _cached_simple_header.clear();
  }
  _query_string.clear();
  _headers_parsed = _query_string_parsed = false;
  _cookie_str.clear();
  _releaseCookieJar();
}

void
Variables::_parseCookieString(const char *str, int str_len)
{
  AttributeList cookies;
  Utils::parseAttributes(str, str_len, cookies, ";,");
  for (auto &iter : cookies) {
    std::string cookie = iter.name;
    size_t pos         = cookie.find('=');

    if (pos != std::string::npos) {
      cookie = cookie.substr(0, pos);
    }

    bool found = false;
    for (auto &_allowlistCookie : _allowlistCookies) {
      if ((_allowlistCookie == "*") || (_allowlistCookie == cookie)) {
        found = true;
      }
    }

    if (found == true) {
      _insert(_dict_data[HTTP_COOKIE], string(iter.name, iter.name_len), string(iter.value, iter.value_len));
      _debugLog(_debug_tag, "[%s] Inserted cookie with name [%.*s] and value [%.*s]", __FUNCTION__, iter.name_len, iter.name,
                iter.value_len, iter.value);
    }
  }
}

void
Variables::_parseUserAgentString(const char * /* str ATS_UNUSED */, int /* str_len ATS_UNUSED */)
{
}

void
Variables::_parseAcceptLangString(const char *str, int str_len)
{
  int i;
  for (i = 0; (i < str_len) && ((isspace(str[i]) || str[i] == ',')); ++i) {
    ;
  }
  const char *lang = str + i;
  int lang_len;
  for (; i <= str_len; ++i) {
    if ((i == str_len) || (str[i] == ',')) {
      lang_len = str + i - lang;
      for (; lang_len && isspace(lang[lang_len - 1]); --lang_len) {
        ;
      }
      if (lang_len) {
        _insert(_dict_data[HTTP_ACCEPT_LANGUAGE], string(lang, lang_len), EMPTY_STRING);
        _debugLog(_debug_tag, "[%s] Added language [%.*s]", __FUNCTION__, lang_len, lang);
      }
      for (; (i < str_len) && ((isspace(str[i]) || str[i] == ',')); ++i) {
        ;
      }
      lang = str + i;
    }
  }
}

bool
Variables::_parseDictVariable(const std::string &variable, const char *&header, int &header_len, const char *&attr,
                              int &attr_len) const
{
  const char *var_ptr = variable.data();
  int var_size        = variable.size();
  if ((var_size <= 4) || (variable[var_size - 1] != '}')) {
    return false;
  }
  int paranth_index = -1;
  for (int i = 0; i < (var_size - 1); ++i) {
    if (variable[i] == '{') {
      if (paranth_index != -1) {
        _debugLog(_debug_tag, "[%s] Cannot have multiple parenthesis in dict variable [%.*s]", __FUNCTION__, var_size, var_ptr);
        return false;
      }
      paranth_index = i;
    }
    if (variable[i] == '}') {
      _debugLog(_debug_tag, "[%s] Cannot have multiple parenthesis in dict variable [%.*s]", __FUNCTION__, var_size, var_ptr);
      return false;
    }
  }
  if (paranth_index == -1) {
    _debugLog(_debug_tag, "[%s] Could not find opening parenthesis in variable [%.*s]", __FUNCTION__, var_size, var_ptr);
    return false;
  }
  if (paranth_index == 0) {
    _debugLog(_debug_tag, "[%s] Dict variable has no dict name [%.*s]", __FUNCTION__, var_size, var_ptr);
    return false;
  }
  if (paranth_index == (var_size - 2)) {
    _debugLog(_debug_tag, "[%s] Dict variable has no attribute name [%.*s]", __FUNCTION__, var_size, var_ptr);
    return false;
  }
  header     = var_ptr;
  header_len = paranth_index;
  attr       = var_ptr + paranth_index + 1;
  attr_len   = var_size - header_len - 2;
  return true;
}