From 8df487b563cdcc92ecd5334fe528c5634484e83c Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Tue, 19 Apr 2022 09:26:30 +0200 Subject: [PATCH 1/2] Fix 7891 regex pathInfo Use the pathSpec methods to set servletPath and pathInfo when possible Signed-off-by: Greg Wilkins --- .../jetty/server/ServletPathMapping.java | 8 ++++- .../org/eclipse/jetty/server/RequestTest.java | 34 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/ServletPathMapping.java b/jetty-server/src/main/java/org/eclipse/jetty/server/ServletPathMapping.java index c03607f542e7..3e4643069eb2 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/ServletPathMapping.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/ServletPathMapping.java @@ -90,9 +90,15 @@ public ServletPathMapping(PathSpec pathSpec, String servletName, String pathInCo throw new IllegalStateException(); } } + else if (pathSpec != null) + { + _mappingMatch = null; + _servletPath = pathSpec.getPathMatch(pathInContext); + _matchValue = _servletPath.startsWith("/") ? _servletPath.substring(1) : _servletPath; + _pathInfo = pathSpec.getPathInfo(pathInContext); + } else { - // TODO can we do better for RegexPathSpec _mappingMatch = null; _matchValue = ""; _servletPath = pathInContext; diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/RequestTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/RequestTest.java index 73269c2d7384..99e3dca600fb 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/RequestTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/RequestTest.java @@ -62,6 +62,7 @@ import org.eclipse.jetty.http.MetaData; import org.eclipse.jetty.http.MimeTypes; import org.eclipse.jetty.http.UriCompliance; +import org.eclipse.jetty.http.pathmap.RegexPathSpec; import org.eclipse.jetty.http.pathmap.ServletPathSpec; import org.eclipse.jetty.io.Connection; import org.eclipse.jetty.io.EndPoint; @@ -2043,6 +2044,39 @@ public void testServletPathMapping() assertThat(m.getPathInfo(), is(spec.getPathInfo(uri))); } + @Test + public void testRegexPathMapping() + { + RegexPathSpec spec; + ServletPathMapping m; + + spec = new RegexPathSpec("^/.*$"); + m = new ServletPathMapping(spec, "Something", "/some/path"); + assertThat(m.getMappingMatch(), nullValue()); + assertThat(m.getPattern(), is(spec.getDeclaration())); + assertThat(m.getServletName(), is("Something")); + assertThat(m.getServletPath(), is("/some/path")); + assertThat(m.getPathInfo(), nullValue()); + assertThat(m.getMatchValue(), is("some/path")); + + spec = new RegexPathSpec("^/some(/.*)?$"); + m = new ServletPathMapping(spec, "Something", "/some/path"); + assertThat(m.getMappingMatch(), nullValue()); + assertThat(m.getPattern(), is(spec.getDeclaration())); + assertThat(m.getServletName(), is("Something")); + assertThat(m.getServletPath(), is("/some")); + assertThat(m.getPathInfo(), is("/path")); + assertThat(m.getMatchValue(), is("some")); + + m = new ServletPathMapping(spec, "Something", "/some"); + assertThat(m.getMappingMatch(), nullValue()); + assertThat(m.getPattern(), is(spec.getDeclaration())); + assertThat(m.getServletName(), is("Something")); + assertThat(m.getServletPath(), is("/some")); + assertThat(m.getPathInfo(), nullValue()); + assertThat(m.getMatchValue(), is("some")); + } + private static long getFileCount(Path path) { try (Stream s = Files.list(path)) From 51a49b9716fec6dda3573a1c46c867d33547cb25 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Tue, 19 Apr 2022 18:34:20 +0200 Subject: [PATCH 2/2] Fix #7891 regex pathInfo Fixed test Signed-off-by: Greg Wilkins --- .../java/org/eclipse/jetty/servlet/RegexServletTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/RegexServletTest.java b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/RegexServletTest.java index 1940308cbc36..d4e67f13c35d 100644 --- a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/RegexServletTest.java +++ b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/RegexServletTest.java @@ -79,7 +79,7 @@ public void testMapping() throws Exception assertThat(response, containsString("servletPath='/test/info'")); assertThat(response, containsString("pathInfo='null'")); assertThat(response, containsString("mapping.mappingMatch='null'")); - assertThat(response, containsString("mapping.matchValue=''")); + assertThat(response, containsString("mapping.matchValue='test/info'")); assertThat(response, containsString("mapping.pattern='^/test/.*$'")); } @@ -96,7 +96,7 @@ public void testForward() throws Exception assertThat(response, containsString("servletPath='/Test/info'")); assertThat(response, containsString("pathInfo='null'")); assertThat(response, containsString("mapping.mappingMatch='null'")); - assertThat(response, containsString("mapping.matchValue=''")); + assertThat(response, containsString("mapping.matchValue='Test/info'")); assertThat(response, containsString("mapping.pattern='^/[Tt]est(/.*)?'")); } @@ -113,7 +113,7 @@ public void testInclude() throws Exception assertThat(response, containsString("servletPath='/include'")); assertThat(response, containsString("pathInfo='null'")); assertThat(response, containsString("mapping.mappingMatch='null'")); - assertThat(response, containsString("mapping.matchValue=''")); + assertThat(response, containsString("mapping.matchValue='include'")); assertThat(response, containsString("mapping.pattern='^/include$'")); }