From 4e419d80cc53b23b1e82a4ae0c96614d409288cd Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Wed, 8 Jun 2022 08:02:07 -0500 Subject: [PATCH] Skip optional group name/info lookup if regex fails. Signed-off-by: Joakim Erdfelt --- .../jetty/http/pathmap/RegexPathSpec.java | 26 ++++++++--- .../jetty/http/pathmap/RegexPathSpecTest.java | 43 ++++++++++++++++++- 2 files changed, 62 insertions(+), 7 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 71f751000c36..5fe2db237c9e 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 @@ -289,10 +289,17 @@ public RegexMatchedPath(RegexPathSpec regexPathSpec, String path, Matcher matche @Override public String getPathMatch() { - String p = matcher.group("name"); - if (p != null) + try { - return p; + String p = matcher.group("name"); + if (p != null) + { + return p; + } + } + catch (IllegalArgumentException ignore) + { + // ignore if group name not found. } if (pathSpec.getGroup() == PathSpecGroup.PREFIX_GLOB && matcher.groupCount() >= 1) @@ -313,10 +320,17 @@ public String getPathMatch() @Override public String getPathInfo() { - String p = matcher.group("info"); - if (p != null) + try + { + String p = matcher.group("info"); + if (p != null) + { + return p; + } + } + catch (IllegalArgumentException ignore) { - return p; + // ignore if group info not found. } // Path Info only valid for PREFIX_GLOB 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 b011dafb7ee5..79ff15c97b37 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 @@ -129,7 +129,7 @@ public void testPrefixSpec() } @Test - public void testSuffixSpec() + public void testSuffixSpecTraditional() { RegexPathSpec spec = new RegexPathSpec("^(.*).do$"); assertEquals("^(.*).do$", spec.getDeclaration(), "Spec.pathSpec"); @@ -146,6 +146,47 @@ public void testSuffixSpec() assertNotMatches(spec, "/aa"); assertNotMatches(spec, "/aa/bb"); assertNotMatches(spec, "/aa/bb.do/more"); + + assertThat(spec.getPathMatch("/a/b/c.do"), equalTo("/a/b/c.do")); + assertThat(spec.getPathInfo("/a/b/c.do"), nullValue()); + } + + /** + * A suffix type path spec, where the beginning of the path is evaluated + * but the rest of the path is ignored. + * The beginning is starts with a glob, contains a literal, and no terminal "$". + */ + @Test + public void testSuffixSpecGlobish() + { + RegexPathSpec spec = new RegexPathSpec("^/[Hh]ello"); + assertEquals("^/[Hh]ello", spec.getDeclaration(), "Spec.pathSpec"); + assertEquals("^/[Hh]ello", spec.getPattern().pattern(), "Spec.pattern"); + assertEquals(1, spec.getPathDepth(), "Spec.pathDepth"); + assertEquals(PathSpecGroup.SUFFIX_GLOB, spec.getGroup(), "Spec.group"); + + assertMatches(spec, "/hello"); + assertMatches(spec, "/Hello"); + + assertNotMatches(spec, "/Hello/World"); + assertNotMatches(spec, "/a"); + assertNotMatches(spec, "/aa"); + assertNotMatches(spec, "/aa/bb"); + assertNotMatches(spec, "/aa/bb.do/more"); + + assertThat(spec.getPathMatch("/hello"), equalTo("/hello")); + assertThat(spec.getPathInfo("/hello"), nullValue()); + + assertThat(spec.getPathMatch("/Hello"), equalTo("/Hello")); + assertThat(spec.getPathInfo("/Hello"), nullValue()); + + MatchedPath matchedPath = spec.matched("/hello"); + assertThat(matchedPath.getPathMatch(), equalTo("/hello")); + assertThat(matchedPath.getPathInfo(), nullValue()); + + matchedPath = spec.matched("/Hello"); + assertThat(matchedPath.getPathMatch(), equalTo("/Hello")); + assertThat(matchedPath.getPathInfo(), nullValue()); } @Test