Skip to content

Commit fa40ad7

Browse files
authored
Issue #8184 - Correcting match logic for multiple servlet suffix url-pattern (#8186)
Cherry-pick of commit d1ecdf6 Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
1 parent 3886ac5 commit fa40ad7

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

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

+11-12
Original file line numberDiff line numberDiff line change
@@ -171,20 +171,17 @@ public MatchedResource<E> getMatched(String path)
171171
if (_optimizedExact)
172172
{
173173
int i = path.length();
174-
175-
final Trie<MappedResource<E>> exact_map = _exactMap;
176174
while (i >= 0)
177175
{
178-
MappedResource<E> candidate = exact_map.getBest(path, 0, i);
176+
MappedResource<E> candidate = _exactMap.getBest(path, 0, i--);
179177
if (candidate == null)
180-
break;
178+
continue;
181179

182180
matchedPath = candidate.getPathSpec().matched(path);
183181
if (matchedPath != null)
184182
{
185183
return new MatchedResource<>(candidate.getResource(), candidate.getPathSpec(), matchedPath);
186184
}
187-
i--;
188185
}
189186
// If we reached here, there's NO optimized EXACT Match possible, skip simple match below
190187
skipRestOfGroup = true;
@@ -197,17 +194,15 @@ public MatchedResource<E> getMatched(String path)
197194
if (_optimizedPrefix)
198195
{
199196
int i = path.length();
200-
final Trie<MappedResource<E>> prefix_map = _prefixMap;
201197
while (i >= 0)
202198
{
203-
MappedResource<E> candidate = prefix_map.getBest(path, 0, i);
199+
MappedResource<E> candidate = _prefixMap.getBest(path, 0, i--);
204200
if (candidate == null)
205-
break;
201+
continue;
206202

207203
matchedPath = candidate.getPathSpec().matched(path);
208204
if (matchedPath != null)
209205
return new MatchedResource<>(candidate.getResource(), candidate.getPathSpec(), matchedPath);
210-
i--;
211206
}
212207
// If we reached here, there's NO optimized PREFIX Match possible, skip simple match below
213208
skipRestOfGroup = true;
@@ -220,12 +215,16 @@ public MatchedResource<E> getMatched(String path)
220215
if (_optimizedSuffix)
221216
{
222217
int i = 0;
223-
final Trie<MappedResource<E>> suffix_map = _suffixMap;
218+
// Loop through each suffix mark
219+
// Input is "/a.b.c.foo"
220+
// Loop 1: "b.c.foo"
221+
// Loop 2: "c.foo"
222+
// Loop 3: "foo"
224223
while ((i = path.indexOf('.', i + 1)) > 0)
225224
{
226-
MappedResource<E> candidate = suffix_map.get(path, i + 1, path.length() - i - 1);
225+
MappedResource<E> candidate = _suffixMap.get(path, i + 1, path.length() - i - 1);
227226
if (candidate == null)
228-
break;
227+
continue;
229228

230229
matchedPath = candidate.getPathSpec().matched(path);
231230
if (matchedPath != null)

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

+26
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,32 @@ public void testGetServletPathSpec()
324324
assertThat(p.get(new RegexPathSpec("/a/b/c")), nullValue());
325325
}
326326

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

0 commit comments

Comments
 (0)