Skip to content

Commit

Permalink
quarkusio#42687 handle trailing slash
Browse files Browse the repository at this point in the history
  • Loading branch information
jonomorris authored and bschuhmann committed Nov 16, 2024
1 parent 89baf33 commit 0acbda3
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,24 @@ 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) {
Matcher matcher = segment.pattern.matcher(path);
matched = matcher.find(matchPos);
if (!matched || matcher.start() != matchPos) {
break;
}
matchPos = matcher.end();
for (String group : segment.groups) {
params[paramCount++] = matcher.group(group);
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);
}
}
} else if (segment.type == URITemplate.Type.LITERAL) {
//make sure the literal text is the same
Expand Down Expand Up @@ -125,6 +135,7 @@ private RequestMatch<T> mapFromPathMatcher(String path, PathMatcher.PathMatch<Ar
if (matchPos == 1) { //matchPos == 1 corresponds to '/' as a root level match
doPrefixMatch = prefixAllowed || pathLength == 1; //if prefix is allowed, or we've matched the whole thing
} else if (path.charAt(matchPos) == '/') {
// match /world/ with matchPos = 6 and mathPos == pathLengh - 1
doPrefixMatch = prefixAllowed || matchPos == pathLength - 1; //if prefix is allowed, or the remainder is only a trailing /
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package org.jboss.resteasy.reactive.server.vertx.test.matching;

import static io.restassured.RestAssured.get;
import static org.hamcrest.Matchers.equalTo;

import java.util.function.Supplier;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

import org.jboss.resteasy.reactive.server.vertx.test.framework.ResteasyReactiveUnitTest;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

public class RegexPathTest {

@RegisterExtension
static ResteasyReactiveUnitTest test = new ResteasyReactiveUnitTest()
.setArchiveProducer(new Supplier<>() {
@Override
public JavaArchive get() {
return ShrinkWrap.create(JavaArchive.class)
.addClasses(EndingSlashTest.TestResource.class);
}
});

@Test
public void test() {

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

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

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

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

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

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

@Path("/hello")
public static class TestResource {

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

@GET
@Path("/again/{sample:\\d+}")
@Produces(MediaType.TEXT_PLAIN)
public String helloWithRegex(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;
}
}
}

0 comments on commit 0acbda3

Please sign in to comment.