Do not include URL hash in resource paths
When getting the lookup path of a resource, both query params and hashes should be removed from the request path. This commit fixes the public path resolution for paths like `/resources/main.svg#icon-hamburgermenu`. Issue: SPR-14928
This commit is contained in:
parent
66e6b35e9f
commit
933f1501e8
|
@ -174,7 +174,7 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
|
|||
logger.trace("Getting resource URL for request URL \"" + requestUrl + "\"");
|
||||
}
|
||||
int prefixIndex = getLookupPathIndex(exchange);
|
||||
int suffixIndex = getQueryParamsIndex(requestUrl);
|
||||
int suffixIndex = getEndPathIndex(requestUrl);
|
||||
String prefix = requestUrl.substring(0, prefixIndex);
|
||||
String suffix = requestUrl.substring(suffixIndex);
|
||||
String lookupPath = requestUrl.substring(prefixIndex, suffixIndex);
|
||||
|
@ -188,9 +188,17 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
|
|||
return requestPath.indexOf(lookupPath);
|
||||
}
|
||||
|
||||
private int getQueryParamsIndex(String lookupPath) {
|
||||
int index = lookupPath.indexOf("?");
|
||||
return index > 0 ? index : lookupPath.length();
|
||||
private int getEndPathIndex(String lookupPath) {
|
||||
int suffixIndex = lookupPath.length();
|
||||
int queryIndex = lookupPath.indexOf("?");
|
||||
if(queryIndex > 0) {
|
||||
suffixIndex = queryIndex;
|
||||
}
|
||||
int hashIndex = lookupPath.indexOf("#");
|
||||
if(hashIndex > 0) {
|
||||
suffixIndex = Math.min(suffixIndex, hashIndex);
|
||||
}
|
||||
return suffixIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -79,7 +79,7 @@ public class ResourceUrlProviderTests {
|
|||
}
|
||||
|
||||
@Test // SPR-13374
|
||||
public void getStaticResourceUrlRequestWithRequestParams() {
|
||||
public void getStaticResourceUrlRequestWithQueryOrHash() {
|
||||
MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/");
|
||||
MockServerHttpResponse response = new MockServerHttpResponse();
|
||||
WebSessionManager manager = new DefaultWebSessionManager();
|
||||
|
@ -88,6 +88,10 @@ public class ResourceUrlProviderTests {
|
|||
String url = "/resources/foo.css?foo=bar&url=http://example.org";
|
||||
String resolvedUrl = this.urlProvider.getForRequestUrl(exchange, url).blockMillis(5000);
|
||||
assertEquals(url, resolvedUrl);
|
||||
|
||||
url = "/resources/foo.css#hash";
|
||||
resolvedUrl = this.urlProvider.getForRequestUrl(exchange, url).blockMillis(5000);
|
||||
assertEquals(url, resolvedUrl);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -173,7 +173,7 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
|
|||
logger.trace("Getting resource URL for request URL \"" + requestUrl + "\"");
|
||||
}
|
||||
int prefixIndex = getLookupPathIndex(request);
|
||||
int suffixIndex = getQueryParamsIndex(requestUrl);
|
||||
int suffixIndex = getEndPathIndex(requestUrl);
|
||||
String prefix = requestUrl.substring(0, prefixIndex);
|
||||
String suffix = requestUrl.substring(suffixIndex);
|
||||
String lookupPath = requestUrl.substring(prefixIndex, suffixIndex);
|
||||
|
@ -188,9 +188,17 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
|
|||
return requestUri.indexOf(lookupPath);
|
||||
}
|
||||
|
||||
private int getQueryParamsIndex(String lookupPath) {
|
||||
int index = lookupPath.indexOf("?");
|
||||
return index > 0 ? index : lookupPath.length();
|
||||
private int getEndPathIndex(String lookupPath) {
|
||||
int suffixIndex = lookupPath.length();
|
||||
int queryIndex = lookupPath.indexOf("?");
|
||||
if(queryIndex > 0) {
|
||||
suffixIndex = queryIndex;
|
||||
}
|
||||
int hashIndex = lookupPath.indexOf("#");
|
||||
if(hashIndex > 0) {
|
||||
suffixIndex = Math.min(suffixIndex, hashIndex);
|
||||
}
|
||||
return suffixIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -73,7 +73,7 @@ public class ResourceUrlProviderTests {
|
|||
}
|
||||
|
||||
@Test // SPR-13374
|
||||
public void getStaticResourceUrlRequestWithRequestParams() {
|
||||
public void getStaticResourceUrlRequestWithQueryOrHash() {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setContextPath("/");
|
||||
request.setRequestURI("/");
|
||||
|
@ -81,6 +81,10 @@ public class ResourceUrlProviderTests {
|
|||
String url = "/resources/foo.css?foo=bar&url=http://example.org";
|
||||
String resolvedUrl = this.urlProvider.getForRequestUrl(request, url);
|
||||
assertEquals("/resources/foo.css?foo=bar&url=http://example.org", resolvedUrl);
|
||||
|
||||
url = "/resources/foo.css#hash";
|
||||
resolvedUrl = this.urlProvider.getForRequestUrl(request, url);
|
||||
assertEquals("/resources/foo.css#hash", resolvedUrl);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue