Skip to content

Fix greedy label matching #474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ describe("simple matching", () => {
}),
new UriSpec(
"GET",
[{ type: "path_literal", value: "mg" }, { type: "greedy" }, { type: "path_literal", value: "z" }],
[
{ type: "path_literal", value: "mg" },
{ type: "greedy" },
{ type: "path_literal", value: "y" },
{ type: "path_literal", value: "z" },
],
[],
{ service: "Test", operation: "MiddleGreedy" }
),
Expand Down Expand Up @@ -71,8 +76,10 @@ describe("simple matching", () => {
new HttpRequest({ method: "GET", path: "/greedy/a/b/c/d", query: { abc: "def" } }),
],
"Test#MiddleGreedy": [
new HttpRequest({ method: "GET", path: "/mg/a/z" }),
new HttpRequest({ method: "GET", path: "/mg/a/b/c/d/z", query: { abc: "def" } }),
new HttpRequest({ method: "GET", path: "/mg/a/y/z" }),
new HttpRequest({ method: "GET", path: "/mg/a/b/c/d/y/z", query: { abc: "def" } }),
new HttpRequest({ method: "GET", path: "/mg/a/b/y/c/d/y/z", query: { abc: "def" } }),
new HttpRequest({ method: "GET", path: "/mg/a/b/y/z/d/y/z", query: { abc: "def" } }),
],
"Test#Delete": [
new HttpRequest({ method: "DELETE", path: "/", query: { foo: "bar", baz: "quux" } }),
Expand Down Expand Up @@ -103,6 +110,10 @@ describe("simple matching", () => {
new HttpRequest({ method: "GET", path: "/mg" }),
new HttpRequest({ method: "GET", path: "/mg/q" }),
new HttpRequest({ method: "GET", path: "/mg/z" }),
new HttpRequest({ method: "GET", path: "/mg/y/z" }),
new HttpRequest({ method: "GET", path: "/mg/a/z" }),
new HttpRequest({ method: "GET", path: "/mg/a/y/z/a" }),
new HttpRequest({ method: "GET", path: "/mg/a/y/a" }),
new HttpRequest({ method: "GET", path: "/mg/a/b/z/c" }),
new HttpRequest({ method: "DELETE", path: "/", query: { foo: "bar" } }),
new HttpRequest({ method: "DELETE", path: "/", query: { baz: "quux" } }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class UriSpec<S extends string, O extends string> {
const requestPathSegments = req.path.split("/").filter((s) => s.length > 0);

let requestPathIdx = 0;
path_loop: for (let i = 0; i < this.pathSegments.length; i++) {
for (let i = 0; i < this.pathSegments.length; i++) {
if (requestPathIdx === requestPathSegments.length) {
// there are more pathSegments but we have reached the end of the requestPath
return false;
Expand All @@ -88,24 +88,16 @@ export class UriSpec<S extends string, O extends string> {
return false;
}
if (pathSegment.type === "greedy") {
if (i === this.pathSegments.length - 1) {
// greedy label at the end of pathSegments swallows the remaining path segments
requestPathIdx = requestPathSegments.length;
break path_loop;
}
let matched = false;
if (i < this.pathSegments.length - 1) {
const nextSegment = this.pathSegments[i + 1];
while (!matched && ++requestPathIdx < requestPathSegments.length) {
if (this.matchesSegment(requestPathSegments[requestPathIdx], nextSegment)) {
matched = true;
}
}
}
if (!matched) {
// the next segment after our greedy label did not match any of the remaining request segments
// greedy labels match greedily, and a greedy label cannot be followed by another greedy label
// so just consume as many segments as needed to leave an equal number of segments
// at the end of the request path as we have segments in the pattern
const remainingSegments = this.pathSegments.length - i - 1;
const newRequestPathIdx = requestPathSegments.length - remainingSegments;
if (requestPathIdx === newRequestPathIdx) {
// greedy labels must consume at least one segment
return false;
}
requestPathIdx = newRequestPathIdx;
} else {
requestPathIdx++;
}
Expand Down