Skip to content

Commit d1ecdf6

Browse files
committed
Issue #8184 - Correcting match logic for multiple servlet suffix url-pattern
Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
1 parent ad757df commit d1ecdf6

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

jetty-http/src/main/java/org/eclipse/jetty/http/pathmap/PathMappings.java

+10-7
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,15 @@ public MatchedResource<E> getMatched(String path)
176176
int i = path.length();
177177
while (i >= 0)
178178
{
179-
MappedResource<E> candidate = _exactMap.getBest(path, 0, i);
179+
MappedResource<E> candidate = _exactMap.getBest(path, 0, i--);
180180
if (candidate == null)
181-
break;
181+
continue;
182182

183183
matchedPath = candidate.getPathSpec().matched(path);
184184
if (matchedPath != null)
185185
{
186186
return new MatchedResource<>(candidate.getResource(), candidate.getPathSpec(), matchedPath);
187187
}
188-
i--;
189188
}
190189
// If we reached here, there's NO optimized EXACT Match possible, skip simple match below
191190
skipRestOfGroup = true;
@@ -200,14 +199,13 @@ public MatchedResource<E> getMatched(String path)
200199
int i = path.length();
201200
while (i >= 0)
202201
{
203-
MappedResource<E> candidate = _prefixMap.getBest(path, 0, i);
202+
MappedResource<E> candidate = _prefixMap.getBest(path, 0, i--);
204203
if (candidate == null)
205-
break;
204+
continue;
206205

207206
matchedPath = candidate.getPathSpec().matched(path);
208207
if (matchedPath != null)
209208
return new MatchedResource<>(candidate.getResource(), candidate.getPathSpec(), matchedPath);
210-
i--;
211209
}
212210
// If we reached here, there's NO optimized PREFIX Match possible, skip simple match below
213211
skipRestOfGroup = true;
@@ -220,11 +218,16 @@ public MatchedResource<E> getMatched(String path)
220218
if (_optimizedSuffix)
221219
{
222220
int i = 0;
221+
// Loop through each suffix mark
222+
// Input is "/a.b.c.foo"
223+
// Loop 1: "b.c.foo"
224+
// Loop 2: "c.foo"
225+
// Loop 3: "foo"
223226
while ((i = path.indexOf('.', i + 1)) > 0)
224227
{
225228
MappedResource<E> candidate = _suffixMap.get(path, i + 1, path.length() - i - 1);
226229
if (candidate == null)
227-
break;
230+
continue;
228231

229232
matchedPath = candidate.getPathSpec().matched(path);
230233
if (matchedPath != null)

jetty-http/src/test/java/org/eclipse/jetty/http/pathmap/PathMappingsTest.java

+26
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,32 @@ public void testGetServletPathSpec()
319319
assertThat(p.get(new RegexPathSpec("/a/b/c")), nullValue());
320320
}
321321

322+
@Test
323+
public void testServletMultipleSuffixMappings()
324+
{
325+
PathMappings<String> p = new PathMappings<>();
326+
p.put(new ServletPathSpec("*.foo"), "resourceFoo");
327+
p.put(new ServletPathSpec("*.bar"), "resourceBar");
328+
p.put(new ServletPathSpec("*.zed"), "resourceZed");
329+
330+
MatchedResource<String> matched;
331+
332+
matched = p.getMatched("/a.b.c.foo");
333+
assertThat(matched.getResource(), is("resourceFoo"));
334+
335+
matched = p.getMatched("/a.b.c.bar");
336+
assertThat(matched.getResource(), is("resourceBar"));
337+
338+
matched = p.getMatched("/a.b.c.pop");
339+
assertNull(matched);
340+
341+
matched = p.getMatched("/a.foo.c.pop");
342+
assertNull(matched);
343+
344+
matched = p.getMatched("/a%2Efoo");
345+
assertNull(matched);
346+
}
347+
322348
@Test
323349
public void testRemoveUriTemplatePathSpec()
324350
{

0 commit comments

Comments
 (0)