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