Skip to content

Commit 40b4276

Browse files
committed
UriUtils.extractFileExtension properly handles all fragments
Issue: SPR-15786 (cherry picked from commit 13080f0)
1 parent 4160ced commit 40b4276

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

spring-web/src/main/java/org/springframework/web/util/UriUtils.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,12 @@ public static String decode(String source, String encoding) throws UnsupportedEn
222222
*/
223223
public static String extractFileExtension(String path) {
224224
int end = path.indexOf('?');
225+
int fragmentIndex = path.indexOf('#');
226+
if (fragmentIndex != -1 && (end == -1 || fragmentIndex < end)) {
227+
end = fragmentIndex;
228+
}
225229
if (end == -1) {
226-
end = path.indexOf('#');
227-
if (end == -1) {
228-
end = path.length();
229-
}
230+
end = path.length();
230231
}
231232
int begin = path.lastIndexOf('/', end) + 1;
232233
int paramIndex = path.indexOf(';', begin);

spring-web/src/test/java/org/springframework/web/util/UriUtilsTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525
/**
2626
* @author Arjen Poutsma
2727
* @author Juergen Hoeller
28+
* @author Med Belamachi
2829
*/
2930
public class UriUtilsTests {
3031

@@ -113,6 +114,8 @@ public void extractFileExtension() {
113114
assertEquals("html", UriUtils.extractFileExtension("/products/view.html#/a"));
114115
assertEquals("html", UriUtils.extractFileExtension("/products/view.html#/path/a"));
115116
assertEquals("html", UriUtils.extractFileExtension("/products/view.html#/path/a.do"));
117+
assertEquals("html", UriUtils.extractFileExtension("/products/view.html#aaa?bbb"));
118+
assertEquals("html", UriUtils.extractFileExtension("/products/view.html#aaa.xml?bbb"));
116119
assertEquals("html", UriUtils.extractFileExtension("/products/view.html?param=a"));
117120
assertEquals("html", UriUtils.extractFileExtension("/products/view.html?param=/path/a"));
118121
assertEquals("html", UriUtils.extractFileExtension("/products/view.html?param=/path/a.do"));

0 commit comments

Comments
 (0)