/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 | 926 | : input_(input), tb_(0), te_(0), in_variable_(false) {} |
46 | | |
47 | 926 | bool Parse() { |
48 | 926 | if (!ParseTemplate() || !ConsumedAllInput()) { |
49 | 480 | return false; |
50 | 480 | } |
51 | 446 | PostProcessVariables(); |
52 | 446 | return true; |
53 | 926 | } |
54 | | |
55 | 375 | std::vector<std::string> &segments() { return segments_; } |
56 | 375 | std::string &verb() { return verb_; } |
57 | 375 | std::vector<HttpTemplate::Variable> &variables() { return variables_; } |
58 | | |
59 | | // only constant path segments are allowed after '**'. |
60 | 446 | bool ValidateParts() { |
61 | 446 | bool found_wild_card = false; |
62 | 4.46M | for (size_t i = 0; i < segments_.size(); i++) { |
63 | 4.46M | if (!found_wild_card) { |
64 | 4.46M | if (segments_[i] == HttpTemplate::kWildCardPathKey) { |
65 | 213 | found_wild_card = true; |
66 | 213 | } |
67 | 4.46M | } else if (segments_[i] == HttpTemplate::kSingleParameterKey || |
68 | 1.73k | segments_[i] == HttpTemplate::kWildCardPathPartKey || |
69 | 1.73k | segments_[i] == HttpTemplate::kWildCardPathKey) { |
70 | 71 | return false; |
71 | 71 | } |
72 | 4.46M | } |
73 | 375 | return true; |
74 | 446 | } |
75 | | |
76 | | private: |
77 | | // Template = "/" Segments [ Verb ] ; |
78 | 926 | bool ParseTemplate() { |
79 | 926 | if (!Consume('/')) { |
80 | | // Expected '/' |
81 | 26 | return false; |
82 | 26 | } |
83 | 900 | if (!ParseSegments()) { |
84 | 326 | return false; |
85 | 326 | } |
86 | | |
87 | 574 | if (EnsureCurrent() && current_char() == ':') { |
88 | 56 | if (!ParseVerb()) { |
89 | 33 | return false; |
90 | 33 | } |
91 | 56 | } |
92 | 541 | return true; |
93 | 574 | } |
94 | | |
95 | | // Segments = Segment { "/" Segment } ; |
96 | 6.89k | bool ParseSegments() { |
97 | 6.89k | if (!ParseSegment()) { |
98 | 124 | return false; |
99 | 124 | } |
100 | | |
101 | 5.51M | for (;;) { |
102 | 5.51M | if (!Consume('/')) break; |
103 | 5.51M | if (!ParseSegment()) { |
104 | 229 | return false; |
105 | 229 | } |
106 | 5.51M | } |
107 | | |
108 | 6.54k | return true; |
109 | 6.76k | } |
110 | | |
111 | | // Segment = "*" | "**" | LITERAL | Variable ; |
112 | 5.51M | bool ParseSegment() { |
113 | 5.51M | if (!EnsureCurrent()) { |
114 | 46 | return false; |
115 | 46 | } |
116 | 5.51M | switch (current_char()) { |
117 | 2.19M | case '*': { |
118 | 2.19M | Consume('*'); |
119 | 2.19M | if (Consume('*')) { |
120 | | // ** |
121 | 10.0k | segments_.push_back("**"); |
122 | 10.0k | if (in_variable_) { |
123 | 5.52k | return MarkVariableHasWildCardPath(); |
124 | 5.52k | } |
125 | 4.55k | return true; |
126 | 2.18M | } else { |
127 | 2.18M | segments_.push_back("*"); |
128 | 2.18M | return true; |
129 | 2.18M | } |
130 | 2.19M | } |
131 | | |
132 | 449k | case '{': |
133 | 449k | return ParseVariable(); |
134 | 2.87M | default: |
135 | 2.87M | return ParseLiteralSegment(); |
136 | 5.51M | } |
137 | 5.51M | } |
138 | | |
139 | | // Variable = "{" FieldPath [ "=" Segments ] "}" ; |
140 | 449k | bool ParseVariable() { |
141 | 449k | if (!Consume('{')) { |
142 | 0 | return false; |
143 | 0 | } |
144 | 449k | if (!StartVariable()) { |
145 | 9 | return false; |
146 | 9 | } |
147 | 449k | if (!ParseFieldPath()) { |
148 | 83 | return false; |
149 | 83 | } |
150 | 448k | if (Consume('=')) { |
151 | 5.99k | if (!ParseSegments()) { |
152 | 27 | return false; |
153 | 27 | } |
154 | 443k | } else { |
155 | | // {field_path} is equivalent to {field_path=*} |
156 | 443k | segments_.push_back("*"); |
157 | 443k | } |
158 | 448k | if (!EndVariable()) { |
159 | 0 | return false; |
160 | 0 | } |
161 | 448k | if (!Consume('}')) { |
162 | 167 | return false; |
163 | 167 | } |
164 | 448k | return true; |
165 | 448k | } |
166 | | |
167 | 2.87M | bool ParseLiteralSegment() { |
168 | 2.87M | std::string ls; |
169 | 2.87M | if (!ParseLiteral(&ls)) { |
170 | 21 | return false; |
171 | 21 | } |
172 | 2.87M | segments_.push_back(ls); |
173 | 2.87M | return true; |
174 | 2.87M | } |
175 | | |
176 | | // FieldPath = IDENT { "." IDENT } ; |
177 | 449k | bool ParseFieldPath() { |
178 | 449k | if (!ParseIdentifier()) { |
179 | 48 | return false; |
180 | 48 | } |
181 | 1.02M | while (Consume('.')) { |
182 | 580k | if (!ParseIdentifier()) { |
183 | 35 | return false; |
184 | 35 | } |
185 | 580k | } |
186 | 448k | return true; |
187 | 449k | } |
188 | | |
189 | | // Verb = ":" LITERAL ; |
190 | 56 | bool ParseVerb() { |
191 | 56 | if (!Consume(':')) return false; |
192 | 56 | if (!ParseLiteral(&verb_)) return false; |
193 | 23 | return true; |
194 | 56 | } |
195 | | |
196 | 1.02M | bool ParseIdentifier() { |
197 | 1.02M | std::string idf; |
198 | | |
199 | | // Initialize to false to handle empty literal. |
200 | 1.02M | bool result = false; |
201 | | |
202 | 9.71M | while (NextChar()) { |
203 | 9.71M | char c; |
204 | 9.71M | switch (c = current_char()) { |
205 | 580k | case '.': |
206 | 1.02M | case '}': |
207 | 1.02M | case '=': |
208 | 1.02M | return result && AddFieldIdentifier(std::move(idf)); |
209 | 8.68M | default: |
210 | 8.68M | Consume(c); |
211 | 8.68M | idf.push_back(c); |
212 | 8.68M | break; |
213 | 9.71M | } |
214 | 8.68M | result = true; |
215 | 8.68M | } |
216 | 181 | return result && AddFieldIdentifier(std::move(idf)); |
217 | 1.02M | } |
218 | | |
219 | 2.87M | bool ParseLiteral(std::string *lit) { |
220 | 2.87M | if (!EnsureCurrent()) { |
221 | 30 | return false; |
222 | 30 | } |
223 | | |
224 | | // Initialize to false in case we encounter an empty literal. |
225 | 2.87M | bool result = false; |
226 | | |
227 | 19.3M | for (;;) { |
228 | 19.3M | char c; |
229 | 19.3M | switch (c = current_char()) { |
230 | 2.87M | case '/': |
231 | 2.87M | case ':': |
232 | 2.87M | case '}': |
233 | 2.87M | return result; |
234 | 16.5M | default: |
235 | 16.5M | Consume(c); |
236 | 16.5M | lit->push_back(c); |
237 | 16.5M | break; |
238 | 19.3M | } |
239 | | |
240 | 16.5M | result = true; |
241 | | |
242 | 16.5M | if (!NextChar()) { |
243 | 316 | break; |
244 | 316 | } |
245 | 16.5M | } |
246 | 316 | return result; |
247 | 2.87M | } |
248 | | |
249 | 37.4M | bool Consume(char c) { |
250 | 37.4M | if (tb_ >= te_ && !NextChar()) { |
251 | 931 | return false; |
252 | 931 | } |
253 | 37.4M | if (current_char() != c) { |
254 | 3.08M | return false; |
255 | 3.08M | } |
256 | 34.4M | tb_++; |
257 | 34.4M | return true; |
258 | 37.4M | } |
259 | | |
260 | 541 | bool ConsumedAllInput() { return tb_ >= input_.size(); } |
261 | | |
262 | 8.39M | bool EnsureCurrent() { return tb_ < te_ || NextChar(); } |
263 | | |
264 | 34.4M | bool NextChar() { |
265 | 34.4M | if (te_ < input_.size()) { |
266 | 34.4M | te_++; |
267 | 34.4M | return true; |
268 | 34.4M | } else { |
269 | 1.93k | return false; |
270 | 1.93k | } |
271 | 34.4M | } |
272 | | |
273 | | // Returns the character looked at. |
274 | 72.1M | char current_char() const { |
275 | 72.1M | return tb_ < te_ && te_ <= input_.size() ? input_[te_ - 1] : -1; |
276 | 72.1M | } |
277 | | |
278 | 2.83M | HttpTemplate::Variable &CurrentVariable() { return variables_.back(); } |
279 | | |
280 | 449k | bool StartVariable() { |
281 | 449k | if (!in_variable_) { |
282 | 449k | variables_.push_back(HttpTemplate::Variable{}); |
283 | 449k | CurrentVariable().start_segment = segments_.size(); |
284 | 449k | CurrentVariable().has_wildcard_path = false; |
285 | 449k | in_variable_ = true; |
286 | 449k | return true; |
287 | 449k | } else { |
288 | | // nested variables are not allowed |
289 | 9 | return false; |
290 | 9 | } |
291 | 449k | } |
292 | | |
293 | 448k | bool EndVariable() { |
294 | 448k | if (in_variable_ && !variables_.empty()) { |
295 | 448k | CurrentVariable().end_segment = segments_.size(); |
296 | 448k | in_variable_ = false; |
297 | 448k | return ValidateVariable(CurrentVariable()); |
298 | 448k | } else { |
299 | | // something's wrong we're not in a variable |
300 | 0 | return false; |
301 | 0 | } |
302 | 448k | } |
303 | | |
304 | 1.02M | bool AddFieldIdentifier(std::string id) { |
305 | 1.02M | if (in_variable_ && !variables_.empty()) { |
306 | 1.02M | CurrentVariable().field_path.emplace_back(std::move(id)); |
307 | 1.02M | return true; |
308 | 1.02M | } else { |
309 | | // something's wrong we're not in a variable |
310 | 0 | return false; |
311 | 0 | } |
312 | 1.02M | } |
313 | | |
314 | 5.52k | bool MarkVariableHasWildCardPath() { |
315 | 5.52k | if (in_variable_ && !variables_.empty()) { |
316 | 5.52k | CurrentVariable().has_wildcard_path = true; |
317 | 5.52k | return true; |
318 | 5.52k | } else { |
319 | | // something's wrong we're not in a variable |
320 | 0 | return false; |
321 | 0 | } |
322 | 5.52k | } |
323 | | |
324 | 448k | bool ValidateVariable(const HttpTemplate::Variable &var) { |
325 | 448k | return !var.field_path.empty() && (var.start_segment < var.end_segment) && |
326 | 448k | (var.end_segment <= static_cast<int>(segments_.size())); |
327 | 448k | } |
328 | | |
329 | 446 | void PostProcessVariables() { |
330 | 229k | for (auto &var : variables_) { |
331 | 229k | 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 | 2.03k | var.end_segment = (var.end_segment - segments_.size() - 1); |
338 | 2.03k | } |
339 | 229k | } |
340 | 446 | } |
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 | 927 | std::unique_ptr<HttpTemplate> HttpTemplate::Parse(const std::string &ht) { |
365 | 927 | if (ht == "/") { |
366 | 1 | return std::unique_ptr<HttpTemplate>(new HttpTemplate({}, {}, {})); |
367 | 1 | } |
368 | | |
369 | 926 | Parser p(ht); |
370 | 926 | if (!p.Parse() || !p.ValidateParts()) { |
371 | 551 | return nullptr; |
372 | 551 | } |
373 | | |
374 | 375 | return std::unique_ptr<HttpTemplate>(new HttpTemplate( |
375 | 375 | std::move(p.segments()), std::move(p.verb()), std::move(p.variables()))); |
376 | 926 | } |
377 | | |
378 | | } // namespace transcoding |
379 | | } // namespace grpc |
380 | | } // namespace google |