Coverage Report

Created: 2021-09-27 09:58

/proc/self/cwd/src/http_template.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2016 Google Inc. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//    http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
//
15
////////////////////////////////////////////////////////////////////////////////
16
//
17
#include <cassert>
18
#include <string>
19
#include <vector>
20
21
#include "grpc_transcoding/http_template.h"
22
23
namespace google {
24
namespace grpc {
25
namespace transcoding {
26
27
namespace {
28
29
// TODO: implement an error sink.
30
31
// HTTP Template Grammar:
32
// Questions:
33
//   - what are the constraints on LITERAL and IDENT?
34
//   - what is the character set for the grammar?
35
//
36
// Template = "/" | "/" Segments [ Verb ] ;
37
// Segments = Segment { "/" Segment } ;
38
// Segment  = "*" | "**" | LITERAL | Variable ;
39
// Variable = "{" FieldPath [ "=" Segments ] "}" ;
40
// FieldPath = IDENT { "." IDENT } ;
41
// Verb     = ":" LITERAL ;
42
class Parser {
43
 public:
44
  Parser(const std::string &input)
45
1.00k
      : input_(input), tb_(0), te_(0), in_variable_(false) {}
46
47
1.00k
  bool Parse() {
48
1.00k
    if (!ParseTemplate() || !ConsumedAllInput()) {
49
560
      return false;
50
560
    }
51
448
    PostProcessVariables();
52
448
    return true;
53
1.00k
  }
54
55
378
  std::vector<std::string> &segments() { return segments_; }
56
378
  std::string &verb() { return verb_; }
57
378
  std::vector<HttpTemplate::Variable> &variables() { return variables_; }
58
59
  // only constant path segments are allowed after '**'.
60
448
  bool ValidateParts() {
61
448
    bool found_wild_card = false;
62
2.26M
    for (size_t i = 0; i < segments_.size(); i++) {
63
2.26M
      if (!found_wild_card) {
64
2.26M
        if (segments_[i] == HttpTemplate::kWildCardPathKey) {
65
215
          found_wild_card = true;
66
215
        }
67
2.26M
      } else if (segments_[i] == HttpTemplate::kSingleParameterKey ||
68
2.15k
                 segments_[i] == HttpTemplate::kWildCardPathPartKey ||
69
2.15k
                 segments_[i] == HttpTemplate::kWildCardPathKey) {
70
70
        return false;
71
70
      }
72
2.26M
    }
73
378
    return true;
74
448
  }
75
76
 private:
77
  // Template = "/" Segments [ Verb ] ;
78
1.00k
  bool ParseTemplate() {
79
1.00k
    if (!Consume('/')) {
80
      // Expected '/'
81
27
      return false;
82
27
    }
83
981
    if (!ParseSegments()) {
84
397
      return false;
85
397
    }
86
87
584
    if (EnsureCurrent() && current_char() == ':') {
88
66
      if (!ParseVerb()) {
89
34
        return false;
90
34
      }
91
66
    }
92
550
    return true;
93
584
  }
94
95
  // Segments = Segment { "/" Segment } ;
96
17.0k
  bool ParseSegments() {
97
17.0k
    if (!ParseSegment()) {
98
164
      return false;
99
164
    }
100
101
6.09M
    for (;;) {
102
6.09M
      if (!Consume('/')) break;
103
6.07M
      if (!ParseSegment()) {
104
276
        return false;
105
276
      }
106
6.07M
    }
107
108
16.5k
    return true;
109
16.8k
  }
110
111
  // Segment  = "*" | "**" | LITERAL | Variable ;
112
6.09M
  bool ParseSegment() {
113
6.09M
    if (!EnsureCurrent()) {
114
59
      return false;
115
59
    }
116
6.09M
    switch (current_char()) {
117
233k
      case '*': {
118
233k
        Consume('*');
119
233k
        if (Consume('*')) {
120
          // **
121
63.0k
          segments_.push_back("**");
122
63.0k
          if (in_variable_) {
123
8.16k
            return MarkVariableHasWildCardPath();
124
8.16k
          }
125
54.8k
          return true;
126
170k
        } else {
127
170k
          segments_.push_back("*");
128
170k
          return true;
129
170k
        }
130
233k
      }
131
132
794k
      case '{':
133
794k
        return ParseVariable();
134
5.06M
      default:
135
5.06M
        return ParseLiteralSegment();
136
6.09M
    }
137
6.09M
  }
138
139
  // Variable = "{" FieldPath [ "=" Segments ] "}" ;
140
794k
  bool ParseVariable() {
141
794k
    if (!Consume('{')) {
142
0
      return false;
143
0
    }
144
794k
    if (!StartVariable()) {
145
14
      return false;
146
14
    }
147
794k
    if (!ParseFieldPath()) {
148
97
      return false;
149
97
    }
150
794k
    if (Consume('=')) {
151
16.0k
      if (!ParseSegments()) {
152
43
        return false;
153
43
      }
154
778k
    } else {
155
      // {field_path} is equivalent to {field_path=*}
156
778k
      segments_.push_back("*");
157
778k
    }
158
794k
    if (!EndVariable()) {
159
0
      return false;
160
0
    }
161
794k
    if (!Consume('}')) {
162
201
      return false;
163
201
    }
164
793k
    return true;
165
794k
  }
166
167
5.06M
  bool ParseLiteralSegment() {
168
5.06M
    std::string ls;
169
5.06M
    if (!ParseLiteral(&ls)) {
170
26
      return false;
171
26
    }
172
5.06M
    segments_.push_back(ls);
173
5.06M
    return true;
174
5.06M
  }
175
176
  // FieldPath = IDENT { "." IDENT } ;
177
794k
  bool ParseFieldPath() {
178
794k
    if (!ParseIdentifier()) {
179
61
      return false;
180
61
    }
181
1.68M
    while (Consume('.')) {
182
891k
      if (!ParseIdentifier()) {
183
36
        return false;
184
36
      }
185
891k
    }
186
794k
    return true;
187
794k
  }
188
189
  // Verb     = ":" LITERAL ;
190
66
  bool ParseVerb() {
191
66
    if (!Consume(':')) return false;
192
66
    if (!ParseLiteral(&verb_)) return false;
193
32
    return true;
194
66
  }
195
196
1.68M
  bool ParseIdentifier() {
197
1.68M
    std::string idf;
198
199
    // Initialize to false to handle empty literal.
200
1.68M
    bool result = false;
201
202
15.7M
    while (NextChar()) {
203
15.7M
      char c;
204
15.7M
      switch (c = current_char()) {
205
891k
        case '.':
206
1.66M
        case '}':
207
1.68M
        case '=':
208
1.68M
          return result && AddFieldIdentifier(std::move(idf));
209
14.0M
        default:
210
14.0M
          Consume(c);
211
14.0M
          idf.push_back(c);
212
14.0M
          break;
213
15.7M
      }
214
14.0M
      result = true;
215
14.0M
    }
216
187
    return result && AddFieldIdentifier(std::move(idf));
217
1.68M
  }
218
219
5.06M
  bool ParseLiteral(std::string *lit) {
220
5.06M
    if (!EnsureCurrent()) {
221
30
      return false;
222
30
    }
223
224
    // Initialize to false in case we encounter an empty literal.
225
5.06M
    bool result = false;
226
227
21.7M
    for (;;) {
228
21.7M
      char c;
229
21.7M
      switch (c = current_char()) {
230
5.05M
        case '/':
231
5.05M
        case ':':
232
5.06M
        case '}':
233
5.06M
          return result;
234
16.6M
        default:
235
16.6M
          Consume(c);
236
16.6M
          lit->push_back(c);
237
16.6M
          break;
238
21.7M
      }
239
240
16.6M
      result = true;
241
242
16.6M
      if (!NextChar()) {
243
337
        break;
244
337
      }
245
16.6M
    }
246
337
    return result;
247
5.06M
  }
248
249
41.4M
  bool Consume(char c) {
250
41.4M
    if (tb_ >= te_ && !NextChar()) {
251
933
      return false;
252
933
    }
253
41.4M
    if (current_char() != c) {
254
1.75M
      return false;
255
1.75M
    }
256
39.6M
    tb_++;
257
39.6M
    return true;
258
41.4M
  }
259
260
550
  bool ConsumedAllInput() { return tb_ >= input_.size(); }
261
262
11.1M
  bool EnsureCurrent() { return tb_ < te_ || NextChar(); }
263
264
39.6M
  bool NextChar() {
265
39.6M
    if (te_ < input_.size()) {
266
39.6M
      te_++;
267
39.6M
      return true;
268
39.6M
    } else {
269
1.96k
      return false;
270
1.96k
    }
271
39.6M
  }
272
273
  // Returns the character looked at.
274
85.0M
  char current_char() const {
275
85.0M
    return tb_ < te_ && te_ <= input_.size() ? input_[te_ - 1] : -1;
276
85.0M
  }
277
278
4.87M
  HttpTemplate::Variable &CurrentVariable() { return variables_.back(); }
279
280
794k
  bool StartVariable() {
281
794k
    if (!in_variable_) {
282
794k
      variables_.push_back(HttpTemplate::Variable{});
283
794k
      CurrentVariable().start_segment = segments_.size();
284
794k
      CurrentVariable().has_wildcard_path = false;
285
794k
      in_variable_ = true;
286
794k
      return true;
287
794k
    } else {
288
      // nested variables are not allowed
289
14
      return false;
290
14
    }
291
794k
  }
292
293
794k
  bool EndVariable() {
294
794k
    if (in_variable_ && !variables_.empty()) {
295
794k
      CurrentVariable().end_segment = segments_.size();
296
794k
      in_variable_ = false;
297
794k
      return ValidateVariable(CurrentVariable());
298
794k
    } else {
299
      // something's wrong we're not in a variable
300
0
      return false;
301
0
    }
302
794k
  }
303
304
1.68M
  bool AddFieldIdentifier(std::string id) {
305
1.68M
    if (in_variable_ && !variables_.empty()) {
306
1.68M
      CurrentVariable().field_path.emplace_back(std::move(id));
307
1.68M
      return true;
308
1.68M
    } else {
309
      // something's wrong we're not in a variable
310
0
      return false;
311
0
    }
312
1.68M
  }
313
314
8.16k
  bool MarkVariableHasWildCardPath() {
315
8.16k
    if (in_variable_ && !variables_.empty()) {
316
8.16k
      CurrentVariable().has_wildcard_path = true;
317
8.16k
      return true;
318
8.16k
    } else {
319
      // something's wrong we're not in a variable
320
0
      return false;
321
0
    }
322
8.16k
  }
323
324
794k
  bool ValidateVariable(const HttpTemplate::Variable &var) {
325
794k
    return !var.field_path.empty() && (var.start_segment < var.end_segment) &&
326
794k
           (var.end_segment <= static_cast<int>(segments_.size()));
327
794k
  }
328
329
448
  void PostProcessVariables() {
330
466k
    for (auto &var : variables_) {
331
466k
      if (var.has_wildcard_path) {
332
        // if the variable contains a '**', store the end_positon
333
        // relative to the end, such that -1 corresponds to the end
334
        // of the path. As we only support fixed path after '**',
335
        // this will allow the matcher code to reconstruct the variable
336
        // value based on the url segments.
337
4.54k
        var.end_segment = (var.end_segment - segments_.size() - 1);
338
4.54k
      }
339
466k
    }
340
448
  }
341
342
  const std::string &input_;
343
344
  // Token delimiter indexes
345
  size_t tb_;
346
  size_t te_;
347
348
  // are we in nested Segments of a variable?
349
  bool in_variable_;
350
351
  std::vector<std::string> segments_;
352
  std::string verb_;
353
  std::vector<HttpTemplate::Variable> variables_;
354
};
355
356
}  // namespace
357
358
const char HttpTemplate::kSingleParameterKey[] = "/.";
359
360
const char HttpTemplate::kWildCardPathPartKey[] = "*";
361
362
const char HttpTemplate::kWildCardPathKey[] = "**";
363
364
1.00k
std::unique_ptr<HttpTemplate> HttpTemplate::Parse(const std::string &ht) {
365
1.00k
  if (ht == "/") {
366
1
    return std::unique_ptr<HttpTemplate>(new HttpTemplate({}, {}, {}));
367
1
  }
368
369
1.00k
  Parser p(ht);
370
1.00k
  if (!p.Parse() || !p.ValidateParts()) {
371
630
    return nullptr;
372
630
  }
373
374
378
  return std::unique_ptr<HttpTemplate>(new HttpTemplate(
375
378
      std::move(p.segments()), std::move(p.verb()), std::move(p.variables())));
376
1.00k
}
377
378
}  // namespace transcoding
379
}  // namespace grpc
380
}  // namespace google