Skip to content

Commit

Permalink
quarkusio#42687 ignore end-slash when matching path subdir
Browse files Browse the repository at this point in the history
  • Loading branch information
jonomorris authored and bschuhmann committed Nov 16, 2024
1 parent 94780c3 commit 75f375f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,24 +76,20 @@ private RequestMatch<T> mapFromPathMatcher(String path, PathMatcher.PathMatch<Ar
for (int i = 1; i < potentialMatch.template.components.length; ++i) {
URITemplate.TemplateComponent segment = potentialMatch.template.components[i];
if (segment.type == URITemplate.Type.CUSTOM_REGEX) {
if (initialMatch.getRemaining().isEmpty() || initialMatch.getRemaining().equals("/")) {
matched = true;
} else {
boolean endSlash = path.charAt(path.length() - 1) == '/';
// exclude any path end slash when matching, but include it in the matched length
Matcher matcher = segment.pattern.matcher(
endSlash ? path.substring(0, path.length() - 1) : path);
matched = matcher.find(matchPos);
if (!matched || matcher.start() != matchPos) {
break;
}
matchPos = matcher.end();
if (endSlash) {
matchPos++;
}
for (String group : segment.groups) {
params[paramCount++] = matcher.group(group);
}
// exclude any path end slash when matching a subdir, but include it in the matched length
boolean endSlash = matchPos < path.length() && path.charAt(path.length() - 1) == '/';
Matcher matcher = segment.pattern.matcher(
endSlash ? path.substring(0, path.length() - 1) : path);
matched = matcher.find(matchPos);
if (!matched || matcher.start() != matchPos) {
break;
}
matchPos = matcher.end();
if (endSlash) {
matchPos++;
}
for (String group : segment.groups) {
params[paramCount++] = matcher.group(group);
}
} else if (segment.type == URITemplate.Type.LITERAL) {
//make sure the literal text is the same
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ public void test() {
.statusCode(200)
.body(equalTo("Hello World! 1"));

get("/hello/")
.then()
.statusCode(404);

get("/hello/1")
.then()
.statusCode(200)
.body(equalTo("Hello! 1"));

get("/hello/1/")
.then()
.statusCode(200)
.body(equalTo("Hello! 1"));

get("/hello/again/")
.then()
.statusCode(404);

get("/hello/again/1")
.then()
.statusCode(200)
Expand All @@ -54,12 +72,12 @@ public void test() {
get("/hello/again/2/surprise")
.then()
.statusCode(200)
.body(equalTo("Hello Surprise! 2"));
.body(equalTo("Hello Again Surprise! 2"));

get("/hello/again/2/surprise/")
.then()
.statusCode(200)
.body(equalTo("Hello Surprise! 2"));
.body(equalTo("Hello Again Surprise! 2"));
}

@Path("/hello")
Expand All @@ -68,22 +86,29 @@ public static class TestResource {
@GET
@Path("/world/{sample}")
@Produces(MediaType.TEXT_PLAIN)
public String hello(int sample) {
public String helloWorld(int sample) {
return "Hello World! " + sample;
}

@GET
@Path("/{sample:\\d+}")
@Produces(MediaType.TEXT_PLAIN)
public String hello(int sample) {
return "Hello! " + sample;
}

@GET
@Path("/again/{sample:\\d+}")
@Produces(MediaType.TEXT_PLAIN)
public String helloWithRegex(int sample) {
public String helloAgain(int sample) {
return "Hello Again! " + sample;
}

@GET
@Path("/again/{sample:\\d+}/surprise")
@Produces(MediaType.TEXT_PLAIN)
public String helloWithRegexSecond(int sample) {
return "Hello Surprise! " + sample;
public String helloAgainSurprise(int sample) {
return "Hello Again Surprise! " + sample;
}
}
}

0 comments on commit 75f375f

Please sign in to comment.