UriUtils.extractFileExtension properly handles all fragments

Issue: SPR-15786
(cherry picked from commit 13080f0)
This commit is contained in:
Juergen Hoeller
2017-07-19 22:40:24 +02:00
parent 4160ced64c
commit 40b4276475
2 changed files with 9 additions and 5 deletions

View File

@@ -222,11 +222,12 @@ public abstract class UriUtils {
*/
public static String extractFileExtension(String path) {
int end = path.indexOf('?');
int fragmentIndex = path.indexOf('#');
if (fragmentIndex != -1 && (end == -1 || fragmentIndex < end)) {
end = fragmentIndex;
}
if (end == -1) {
end = path.indexOf('#');
if (end == -1) {
end = path.length();
}
end = path.length();
}
int begin = path.lastIndexOf('/', end) + 1;
int paramIndex = path.indexOf(';', begin);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@ import static org.junit.Assert.*;
/**
* @author Arjen Poutsma
* @author Juergen Hoeller
* @author Med Belamachi
*/
public class UriUtilsTests {
@@ -113,6 +114,8 @@ public class UriUtilsTests {
assertEquals("html", UriUtils.extractFileExtension("/products/view.html#/a"));
assertEquals("html", UriUtils.extractFileExtension("/products/view.html#/path/a"));
assertEquals("html", UriUtils.extractFileExtension("/products/view.html#/path/a.do"));
assertEquals("html", UriUtils.extractFileExtension("/products/view.html#aaa?bbb"));
assertEquals("html", UriUtils.extractFileExtension("/products/view.html#aaa.xml?bbb"));
assertEquals("html", UriUtils.extractFileExtension("/products/view.html?param=a"));
assertEquals("html", UriUtils.extractFileExtension("/products/view.html?param=/path/a"));
assertEquals("html", UriUtils.extractFileExtension("/products/view.html?param=/path/a.do"));