From 5fc63bf8e899aeb7c9239127e46b7bb0013bb970 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Wed, 6 Apr 2022 11:14:52 +0200 Subject: [PATCH] Issue #7748 - Merge PathSpec improvements made in 9.4.x (#7845) * Fixes to backport of #7748 + Backport of #7748 + Fix RegexPathSpec pathInfo + Fix UriTemplatePathSpec pathInfo + Test regression option to 93 behaviour * small optimization Signed-off-by: Greg Wilkins --- .../jetty/http/pathmap/RegexPathSpec.java | 2 +- .../jetty/http/pathmap/ServletPathSpec.java | 2 +- .../jetty/http/pathmap/UriTemplatePathSpec.java | 2 +- .../jetty/http/pathmap/RegexPathSpecTest.java | 16 ++++++++++++++++ .../http/pathmap/UriTemplatePathSpecTest.java | 16 ++++++++++++++++ 5 files changed, 35 insertions(+), 3 deletions(-) diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/pathmap/RegexPathSpec.java b/jetty-http/src/main/java/org/eclipse/jetty/http/pathmap/RegexPathSpec.java index 53c7a92a9256..98b86262a62d 100644 --- a/jetty-http/src/main/java/org/eclipse/jetty/http/pathmap/RegexPathSpec.java +++ b/jetty-http/src/main/java/org/eclipse/jetty/http/pathmap/RegexPathSpec.java @@ -135,7 +135,7 @@ public String getPathMatch(String path) Matcher matcher = getMatcher(path); if (matcher.matches()) { - if (matcher.groupCount() >= 1) + if (_group == PathSpecGroup.PREFIX_GLOB && matcher.groupCount() >= 1) { int idx = matcher.start(1); if (idx > 0) diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/pathmap/ServletPathSpec.java b/jetty-http/src/main/java/org/eclipse/jetty/http/pathmap/ServletPathSpec.java index dc8c0a11a487..2ff378ee35e8 100644 --- a/jetty-http/src/main/java/org/eclipse/jetty/http/pathmap/ServletPathSpec.java +++ b/jetty-http/src/main/java/org/eclipse/jetty/http/pathmap/ServletPathSpec.java @@ -345,7 +345,7 @@ public String getPathInfo(String path) case PREFIX_GLOB: if (path.length() == (_specLength - 2)) return null; - return path.substring(_specLength - 2); + return _specLength == 2 ? path : path.substring(_specLength - 2); default: return null; diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/pathmap/UriTemplatePathSpec.java b/jetty-http/src/main/java/org/eclipse/jetty/http/pathmap/UriTemplatePathSpec.java index 3fd6300ec0af..6a8453972554 100644 --- a/jetty-http/src/main/java/org/eclipse/jetty/http/pathmap/UriTemplatePathSpec.java +++ b/jetty-http/src/main/java/org/eclipse/jetty/http/pathmap/UriTemplatePathSpec.java @@ -353,7 +353,7 @@ public String getPathMatch(String path) Matcher matcher = getMatcher(path); if (matcher.matches()) { - if (matcher.groupCount() >= 1) + if (_group == PathSpecGroup.PREFIX_GLOB && matcher.groupCount() >= 1) { int idx = matcher.start(1); if (idx > 0) diff --git a/jetty-http/src/test/java/org/eclipse/jetty/http/pathmap/RegexPathSpecTest.java b/jetty-http/src/test/java/org/eclipse/jetty/http/pathmap/RegexPathSpecTest.java index 3110636288a6..f80374e355a1 100644 --- a/jetty-http/src/test/java/org/eclipse/jetty/http/pathmap/RegexPathSpecTest.java +++ b/jetty-http/src/test/java/org/eclipse/jetty/http/pathmap/RegexPathSpecTest.java @@ -19,7 +19,9 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.nullValue; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; public class RegexPathSpecTest { @@ -71,6 +73,20 @@ public void testMiddleSpec() assertNotMatches(spec, "/rest/list"); } + @Test + public void testPathInfo() + { + RegexPathSpec spec = new RegexPathSpec("^/test(/.*)?$"); + assertTrue(spec.matches("/test/info")); + assertThat(spec.getPathMatch("/test/info"), equalTo("/test")); + assertThat(spec.getPathInfo("/test/info"), equalTo("/info")); + + spec = new RegexPathSpec("^/[Tt]est(/.*)?$"); + assertTrue(spec.matches("/test/info")); + assertThat(spec.getPathMatch("/test/info"), equalTo("/test/info")); + assertThat(spec.getPathInfo("/test/info"), nullValue()); + } + @Test public void testMiddleSpecNoGrouping() { diff --git a/jetty-http/src/test/java/org/eclipse/jetty/http/pathmap/UriTemplatePathSpecTest.java b/jetty-http/src/test/java/org/eclipse/jetty/http/pathmap/UriTemplatePathSpecTest.java index 3e07b8a859a6..700f45d0b2dd 100644 --- a/jetty-http/src/test/java/org/eclipse/jetty/http/pathmap/UriTemplatePathSpecTest.java +++ b/jetty-http/src/test/java/org/eclipse/jetty/http/pathmap/UriTemplatePathSpecTest.java @@ -22,7 +22,9 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * Tests for URI Template Path Specs @@ -142,6 +144,20 @@ public void testMiddleVarPathSpec() assertEquals("b", mapped.get("var"), "Spec.pathParams[var]"); } + @Test + public void testPathInfo() + { + UriTemplatePathSpec spec = new UriTemplatePathSpec("/test/{var}"); + assertTrue(spec.matches("/test/info")); + assertThat(spec.getPathMatch("/test/info"), equalTo("/test")); + assertThat(spec.getPathInfo("/test/info"), equalTo("info")); + + spec = new UriTemplatePathSpec("/{x}/test/{y}"); + assertTrue(spec.matches("/try/test/info")); + assertThat(spec.getPathMatch("/try/test/info"), equalTo("/try/test/info")); + assertThat(spec.getPathInfo("/try/test/info"), nullValue()); + } + @Test public void testOneVarPathSpec() {