From 41ba53102044c82b3d8906fbe9603074394a3bc0 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Wed, 6 Apr 2022 11:14:22 +0200 Subject: [PATCH] Fixes to backport of #7748 (#7834) + Backport of #7748 + Fix RegexPathSpec pathInfo + Fix UriTemplatePathSpec pathInfo + Test regression option to 93 behaviour Signed-off-by: Greg Wilkins --- .../jetty/http/pathmap/RegexPathSpec.java | 2 +- .../http/pathmap/UriTemplatePathSpec.java | 2 +- .../jetty/http/pathmap/RegexPathSpecTest.java | 16 ++++++++++++++++ .../http/pathmap/UriTemplatePathSpecTest.java | 16 ++++++++++++++++ .../eclipse/jetty/servlet/ServletHandler.java | 18 ++++++++++++------ 5 files changed, 46 insertions(+), 8 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 bccc818a7136..b893096e6bfb 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 @@ -140,7 +140,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/UriTemplatePathSpec.java b/jetty-http/src/main/java/org/eclipse/jetty/http/pathmap/UriTemplatePathSpec.java index 54454236a388..4736d9b4a191 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 @@ -358,7 +358,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 585509329bde..514f67a199d4 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 @@ -24,7 +24,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 { @@ -76,6 +78,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 8c51c21dcbf2..6da4cd972de5 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 @@ -27,7 +27,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 @@ -147,6 +149,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() { diff --git a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletHandler.java b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletHandler.java index 550176c246a2..ce9d4b0f01ad 100644 --- a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletHandler.java +++ b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletHandler.java @@ -303,7 +303,7 @@ protected synchronized void doStop() } } else - servletHolders.add(_servlets[i]); //only retain embedded + servletHolders.add(_servlets[i]); //only retain embedded } } @@ -314,7 +314,7 @@ protected synchronized void doStop() ServletMapping[] sms = servletMappings.toArray(new ServletMapping[0]); updateBeans(_servletMappings, sms); _servletMappings = sms; - + if (_contextHandler != null) _contextHandler.contextDestroyed(); @@ -1151,7 +1151,7 @@ public void addFilterMapping(FilterMapping mapping) else { //there are existing entries. If this is a programmatic filtermapping, it is added at the end of the list. - //If this is a normal filtermapping, it is inserted after all the other filtermappings (matchBefores and normals), + //If this is a normal filtermapping, it is inserted after all the other filtermappings (matchBefores and normals), //but before the first matchAfter filtermapping. if (source == Source.JAVAX_API) { @@ -1299,7 +1299,13 @@ protected synchronized void updateNameMappings() } } - protected synchronized void updateMappings() + protected PathSpec asPathSpec(String pathSpec) + { + // By default only allow servlet path specs + return new ServletPathSpec(pathSpec); + } + + protected void updateMappings() { // update filter mappings if (_filterMappings == null) @@ -1388,7 +1394,7 @@ protected synchronized void updateMappings() finalMapping = mapping; else { - //already have a candidate - only accept another one + //already have a candidate - only accept another one //if the candidate is a default, or we're allowing duplicate mappings if (finalMapping.isDefault()) finalMapping = mapping; @@ -1421,7 +1427,7 @@ else if (isAllowDuplicateMappings()) finalMapping.getServletName(), _servletNameMap.get(finalMapping.getServletName()).getSource()); - pm.put(new ServletPathSpec(pathSpec), _servletNameMap.get(finalMapping.getServletName())); + pm.put(asPathSpec(pathSpec), _servletNameMap.get(finalMapping.getServletName())); } _servletPathMap = pm;