File: parser.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 (305 lines) | stat: -rw-r--r-- 9,511 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
/*
  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.
*/
//////////////////////////////////////////////////////////////////////////////////////////////
// parser.cc: implementation of the config parser
//
//
#include <utility>
#include <iostream>
#include <string>
#include <sstream>

#include "ts/ts.h"

#include "parser.h"

enum ParserState { PARSER_DEFAULT, PARSER_IN_QUOTE, PARSER_IN_REGEX, PARSER_IN_EXPANSION };

bool
Parser::parse_line(const std::string &original_line)
{
  std::string line        = original_line;
  ParserState state       = PARSER_DEFAULT;
  bool extracting_token   = false;
  off_t cur_token_start   = 0;
  size_t cur_token_length = 0;

  for (size_t i = 0; i < line.size(); ++i) {
    if ((state == PARSER_DEFAULT) && (std::isspace(line[i]) || ((line[i] == '=')))) {
      if (extracting_token) {
        cur_token_length = i - cur_token_start;
        if (cur_token_length > 0) {
          _tokens.push_back(line.substr(cur_token_start, cur_token_length));
        }
        extracting_token = false;
        state            = PARSER_DEFAULT;
      } else if (!std::isspace(line[i])) {
        // we got a standalone =, > or <
        _tokens.push_back(std::string(1, line[i]));
      }
    } else if ((state != PARSER_IN_QUOTE) && (line[i] == '/')) {
      // Deal with regexes, nothing gets escaped / quoted in here
      if ((state != PARSER_IN_REGEX) && !extracting_token) {
        state            = PARSER_IN_REGEX;
        extracting_token = true;
        cur_token_start  = i;
      } else if ((state == PARSER_IN_REGEX) && extracting_token && (line[i - 1] != '\\')) {
        cur_token_length = i - cur_token_start + 1;
        _tokens.push_back(line.substr(cur_token_start, cur_token_length));
        state            = PARSER_DEFAULT;
        extracting_token = false;
      }
    } else if ((state != PARSER_IN_REGEX) && (line[i] == '\\')) {
      // Escaping
      if (!extracting_token) {
        extracting_token = true;
        cur_token_start  = i;
      }
      line.erase(i, 1);
    } else if ((state != PARSER_IN_REGEX) && (line[i] == '"')) {
      if ((state != PARSER_IN_QUOTE) && !extracting_token) {
        state            = PARSER_IN_QUOTE;
        extracting_token = true;
        cur_token_start  = i + 1; // Eat the leading quote
      } else if ((state == PARSER_IN_QUOTE) && extracting_token) {
        cur_token_length = i - cur_token_start;
        _tokens.push_back(line.substr(cur_token_start, cur_token_length));
        state            = PARSER_DEFAULT;
        extracting_token = false;
      } else {
        // Malformed expression / operation, ignore ...
        TSError("[%s] malformed line \"%s\", ignoring", PLUGIN_NAME, line.c_str());
        _tokens.clear();
        _empty = true;
        return false;
      }
    } else if (!extracting_token) {
      if (_tokens.empty() && line[i] == '#') {
        // this is a comment line (it may have had leading whitespace before the #)
        _empty = true;
        break;
      }

      if ((line[i] == '=') || (line[i] == '+')) {
        // These are always a separate token
        _tokens.push_back(std::string(1, line[i]));
        continue;
      }

      extracting_token = true;
      cur_token_start  = i;
    }
  }

  if (extracting_token) {
    if (state != PARSER_IN_QUOTE) {
      /* we hit the end of the line while parsing a token, let's add it */
      _tokens.push_back(line.substr(cur_token_start));
    } else {
      // unterminated quote, error case.
      TSError("[%s] malformed line, unterminated quotation: \"%s\", ignoring", PLUGIN_NAME, line.c_str());
      _tokens.clear();
      _empty = true;
      return false;
    }
  }

  if (_tokens.empty()) {
    _empty = true;
  } else {
    return preprocess(_tokens);
  }

  return true;
}

// This is the main "parser", a helper function to the above tokenizer. NOTE: this modifies (possibly) the tokens list,
// therefore, we pass in a copy of the parsers tokens here, such that the original token list is retained (useful for tests etc.).
bool
Parser::preprocess(std::vector<std::string> tokens)
{
  // The last token might be the "flags" section, lets consume it if it is
  if (tokens.size() > 0) {
    std::string m = tokens[tokens.size() - 1];

    if (!m.empty() && (m[0] == '[')) {
      if (m[m.size() - 1] == ']') {
        m = m.substr(1, m.size() - 2);
        if (m.find_first_of(',') != std::string::npos) {
          std::istringstream iss(m);
          std::string t;
          while (getline(iss, t, ',')) {
            _mods.push_back(t);
          }
        } else {
          _mods.push_back(m);
        }
        tokens.pop_back(); // consume it, so we don't concatenate it into the value
      } else {
        // Syntax error
        TSError("[%s] mods have to be enclosed in []", PLUGIN_NAME);
        return false;
      }
    }
  }

  // Special case for "conditional" values
  if (tokens[0].substr(0, 2) == "%{") {
    _cond = true;
  } else if (tokens[0] == "cond") {
    _cond = true;
    tokens.erase(tokens.begin());
  }

  // Is it a condition or operator?
  if (_cond) {
    if ((tokens[0].substr(0, 2) == "%{") && (tokens[0][tokens[0].size() - 1] == '}')) {
      std::string s = tokens[0].substr(2, tokens[0].size() - 3);

      _op = s;
      if (tokens.size() > 2 && (tokens[1][0] == '=' || tokens[1][0] == '>' || tokens[1][0] == '<')) {
        // cond + [=<>] + argument
        _arg = tokens[1] + tokens[2];
      } else if (tokens.size() > 1) {
        // This is for the regular expression, which for some reason has its own handling?? ToDo: Why ?
        _arg = tokens[1];
      } else {
        // This would be for hook conditions, which has no argument.
        _arg = "";
      }
    } else {
      TSError("[%s] conditions must be embraced in %%{}", PLUGIN_NAME);
      return false;
    }
  } else {
    // Operator has no qualifiers, but could take an optional second argument
    _op = tokens[0];
    if (tokens.size() > 1) {
      _arg = tokens[1];

      if (tokens.size() > 2) {
        for (auto it = tokens.begin() + 2; it != tokens.end(); it++) {
          _val = _val + *it;
          if (std::next(it) != tokens.end()) {
            _val = _val + " ";
          }
        }
      } else {
        _val = "";
      }
    } else {
      _arg = "";
      _val = "";
    }
  }

  return true;
}

// Check if the operator is a condition, a hook, and if so, which hook. If the cond is not a hook
// we do not modify the hook itself, and return false.
bool
Parser::cond_is_hook(TSHttpHookID &hook) const
{
  if (!_cond) {
    return false;
  }

  if ("READ_RESPONSE_HDR_HOOK" == _op) {
    hook = TS_HTTP_READ_RESPONSE_HDR_HOOK;
    return true;
  }
  if ("READ_REQUEST_HDR_HOOK" == _op) {
    hook = TS_HTTP_READ_REQUEST_HDR_HOOK;
    return true;
  }
  if ("READ_REQUEST_PRE_REMAP_HOOK" == _op) {
    hook = TS_HTTP_PRE_REMAP_HOOK;
    return true;
  }
  if ("SEND_REQUEST_HDR_HOOK" == _op) {
    hook = TS_HTTP_SEND_REQUEST_HDR_HOOK;
    return true;
  }
  if ("SEND_RESPONSE_HDR_HOOK" == _op) {
    hook = TS_HTTP_SEND_RESPONSE_HDR_HOOK;
    return true;
  }
  if ("REMAP_PSEUDO_HOOK" == _op) {
    hook = TS_REMAP_PSEUDO_HOOK;
    return true;
  }
  if ("TXN_START_HOOK" == _op) {
    hook = TS_HTTP_TXN_START_HOOK;
    return true;
  }
  if ("TXN_CLOSE_HOOK" == _op) {
    hook = TS_HTTP_TXN_CLOSE_HOOK;
    return true;
  }

  return false;
}

HRWSimpleTokenizer::HRWSimpleTokenizer(const std::string &original_line)
{
  std::string line        = original_line;
  ParserState state       = PARSER_DEFAULT;
  bool extracting_token   = false;
  off_t cur_token_start   = 0;
  size_t cur_token_length = 0;

  for (size_t i = 0; i < line.size(); ++i) {
    extracting_token = true;
    switch (state) {
    case PARSER_DEFAULT:
      if ((line[i] == '{') || (line[i] == '<')) {
        if (line[i - 1] == '%') {
          // pickup what we currently have
          cur_token_length = i - cur_token_start - 1;
          if (cur_token_length > 0) {
            _tokens.push_back(line.substr(cur_token_start, cur_token_length));
          }

          cur_token_start  = i - 1;
          state            = PARSER_IN_EXPANSION;
          extracting_token = false;
        }
      }
      break;
    case PARSER_IN_EXPANSION:
      if ((line[i] == '}') || (line[i] == '>')) {
        cur_token_length = i - cur_token_start + 1;
        if (cur_token_length > 0) {
          _tokens.push_back(line.substr(cur_token_start, cur_token_length));
        }
        cur_token_start  = i + 1;
        state            = PARSER_DEFAULT;
        extracting_token = false;
      }
      break;
    default:
      break;
    }
  }

  // take what was left behind
  if (extracting_token) {
    _tokens.push_back(line.substr(cur_token_start));
  }
}