Skip to content

Commit 4b5b38f

Browse files
committed
Support matching root path "/"
1 parent 4d095f0 commit 4b5b38f

File tree

5 files changed

+306
-282
lines changed

5 files changed

+306
-282
lines changed

.bazelversion

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.19.2

src/http_template.cc

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ namespace {
3333
// - what are the constraints on LITERAL and IDENT?
3434
// - what is the character set for the grammar?
3535
//
36-
// Template = "/" Segments [ Verb ] ;
36+
// Template = "/" | "/" Segments [ Verb ] ;
3737
// Segments = Segment { "/" Segment } ;
3838
// Segment = "*" | "**" | LITERAL | Variable ;
3939
// Variable = "{" FieldPath [ "=" Segments ] "}" ;
4040
// FieldPath = IDENT { "." IDENT } ;
4141
// Verb = ":" LITERAL ;
4242
class Parser {
43-
public:
43+
public:
4444
Parser(const std::string &input)
4545
: input_(input), tb_(0), te_(0), in_variable_(false) {}
4646

@@ -73,7 +73,7 @@ class Parser {
7373
return true;
7474
}
7575

76-
private:
76+
private:
7777
// Template = "/" Segments [ Verb ] ;
7878
bool ParseTemplate() {
7979
if (!Consume('/')) {
@@ -99,7 +99,8 @@ class Parser {
9999
}
100100

101101
for (;;) {
102-
if (!Consume('/')) break;
102+
if (!Consume('/'))
103+
break;
103104
if (!ParseSegment()) {
104105
return false;
105106
}
@@ -114,25 +115,25 @@ class Parser {
114115
return false;
115116
}
116117
switch (current_char()) {
117-
case '*': {
118-
Consume('*');
119-
if (Consume('*')) {
120-
// **
121-
segments_.push_back("**");
122-
if (in_variable_) {
123-
return MarkVariableHasWildCardPath();
124-
}
125-
return true;
126-
} else {
127-
segments_.push_back("*");
128-
return true;
118+
case '*': {
119+
Consume('*');
120+
if (Consume('*')) {
121+
// **
122+
segments_.push_back("**");
123+
if (in_variable_) {
124+
return MarkVariableHasWildCardPath();
129125
}
126+
return true;
127+
} else {
128+
segments_.push_back("*");
129+
return true;
130130
}
131+
}
131132

132-
case '{':
133-
return ParseVariable();
134-
default:
135-
return ParseLiteralSegment();
133+
case '{':
134+
return ParseVariable();
135+
default:
136+
return ParseLiteralSegment();
136137
}
137138
}
138139

@@ -188,8 +189,10 @@ class Parser {
188189

189190
// Verb = ":" LITERAL ;
190191
bool ParseVerb() {
191-
if (!Consume(':')) return false;
192-
if (!ParseLiteral(&verb_)) return false;
192+
if (!Consume(':'))
193+
return false;
194+
if (!ParseLiteral(&verb_))
195+
return false;
193196
return true;
194197
}
195198

@@ -202,14 +205,14 @@ class Parser {
202205
while (NextChar()) {
203206
char c;
204207
switch (c = current_char()) {
205-
case '.':
206-
case '}':
207-
case '=':
208-
return result && AddFieldIdentifier(std::move(idf));
209-
default:
210-
Consume(c);
211-
idf.push_back(c);
212-
break;
208+
case '.':
209+
case '}':
210+
case '=':
211+
return result && AddFieldIdentifier(std::move(idf));
212+
default:
213+
Consume(c);
214+
idf.push_back(c);
215+
break;
213216
}
214217
result = true;
215218
}
@@ -227,14 +230,14 @@ class Parser {
227230
for (;;) {
228231
char c;
229232
switch (c = current_char()) {
230-
case '/':
231-
case ':':
232-
case '}':
233-
return result;
234-
default:
235-
Consume(c);
236-
lit->push_back(c);
237-
break;
233+
case '/':
234+
case ':':
235+
case '}':
236+
return result;
237+
default:
238+
Consume(c);
239+
lit->push_back(c);
240+
break;
238241
}
239242

240243
result = true;
@@ -359,7 +362,7 @@ class Parser {
359362
std::vector<HttpTemplate::Variable> variables_;
360363
};
361364

362-
} // namespace
365+
} // namespace
363366

364367
const char HttpTemplate::kSingleParameterKey[] = "/.";
365368

@@ -368,6 +371,10 @@ const char HttpTemplate::kWildCardPathPartKey[] = "*";
368371
const char HttpTemplate::kWildCardPathKey[] = "**";
369372

370373
std::unique_ptr<HttpTemplate> HttpTemplate::Parse(const std::string &ht) {
374+
if (ht == "/") {
375+
return std::unique_ptr<HttpTemplate>(new HttpTemplate({}, {}, {}));
376+
}
377+
371378
Parser p(ht);
372379
if (!p.Parse() || !p.ValidateParts()) {
373380
return nullptr;
@@ -377,6 +384,6 @@ std::unique_ptr<HttpTemplate> HttpTemplate::Parse(const std::string &ht) {
377384
std::move(p.segments()), std::move(p.verb()), std::move(p.variables())));
378385
}
379386

380-
} // namespace transcoding
381-
} // namespace grpc
382-
} // namespace google
387+
} // namespace transcoding
388+
} // namespace grpc
389+
} // namespace google

0 commit comments

Comments
 (0)