Polishing (collapsed if checks, consistent downcasts, refined javadoc)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -59,6 +59,7 @@ class PathResourceLookupFunction implements Function<ServerRequest, Mono<Resourc
|
||||
if (!this.pattern.matches(pathContainer)) {
|
||||
return Mono.empty();
|
||||
}
|
||||
|
||||
pathContainer = this.pattern.extractPathWithinPattern(pathContainer);
|
||||
String path = processPath(pathContainer.value());
|
||||
if (path.contains("%")) {
|
||||
@@ -67,6 +68,7 @@ class PathResourceLookupFunction implements Function<ServerRequest, Mono<Resourc
|
||||
if (!StringUtils.hasLength(path) || isInvalidPath(path)) {
|
||||
return Mono.empty();
|
||||
}
|
||||
|
||||
try {
|
||||
Resource resource = this.location.createRelative(path);
|
||||
if (resource.exists() && resource.isReadable() && isResourceUnderLocation(resource)) {
|
||||
@@ -81,7 +83,7 @@ class PathResourceLookupFunction implements Function<ServerRequest, Mono<Resourc
|
||||
}
|
||||
}
|
||||
|
||||
private static String processPath(String path) {
|
||||
private String processPath(String path) {
|
||||
boolean slash = false;
|
||||
for (int i = 0; i < path.length(); i++) {
|
||||
if (path.charAt(i) == '/') {
|
||||
@@ -98,7 +100,7 @@ class PathResourceLookupFunction implements Function<ServerRequest, Mono<Resourc
|
||||
return (slash ? "/" : "");
|
||||
}
|
||||
|
||||
private static boolean isInvalidPath(String path) {
|
||||
private boolean isInvalidPath(String path) {
|
||||
if (path.contains("WEB-INF") || path.contains("META-INF")) {
|
||||
return true;
|
||||
}
|
||||
@@ -141,22 +143,20 @@ class PathResourceLookupFunction implements Function<ServerRequest, Mono<Resourc
|
||||
if (locationPath.equals(resourcePath)) {
|
||||
return true;
|
||||
}
|
||||
locationPath = (locationPath.endsWith("/") || locationPath.isEmpty() ? locationPath :
|
||||
locationPath + "/");
|
||||
locationPath = (locationPath.endsWith("/") || locationPath.isEmpty() ? locationPath : locationPath + "/");
|
||||
if (!resourcePath.startsWith(locationPath)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (resourcePath.contains("%")) {
|
||||
if (StringUtils.uriDecode(resourcePath, StandardCharsets.UTF_8).contains("../")) {
|
||||
return false;
|
||||
}
|
||||
if (resourcePath.contains("%") && StringUtils.uriDecode(resourcePath, StandardCharsets.UTF_8).contains("../")) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s -> %s", this.pattern, this.location);
|
||||
return this.pattern + " -> " + this.location;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -112,9 +112,7 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
|
||||
* @see org.springframework.web.util.pattern.PathPattern
|
||||
*/
|
||||
@Nullable
|
||||
protected Object lookupHandler(PathContainer lookupPath, ServerWebExchange exchange)
|
||||
throws Exception {
|
||||
|
||||
protected Object lookupHandler(PathContainer lookupPath, ServerWebExchange exchange) throws Exception {
|
||||
return this.handlerMap.entrySet().stream()
|
||||
.filter(entry -> entry.getKey().matches(lookupPath))
|
||||
.sorted((entry1, entry2) ->
|
||||
@@ -167,9 +165,7 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
|
||||
* @throws BeansException if the handler couldn't be registered
|
||||
* @throws IllegalStateException if there is a conflicting handler registered
|
||||
*/
|
||||
protected void registerHandler(String[] urlPaths, String beanName)
|
||||
throws BeansException, IllegalStateException {
|
||||
|
||||
protected void registerHandler(String[] urlPaths, String beanName) throws BeansException, IllegalStateException {
|
||||
Assert.notNull(urlPaths, "URL path array must not be null");
|
||||
for (String urlPath : urlPaths) {
|
||||
registerHandler(urlPath, beanName);
|
||||
@@ -184,9 +180,7 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
|
||||
* @throws BeansException if the handler couldn't be registered
|
||||
* @throws IllegalStateException if there is a conflicting handler registered
|
||||
*/
|
||||
protected void registerHandler(String urlPath, Object handler)
|
||||
throws BeansException, IllegalStateException {
|
||||
|
||||
protected void registerHandler(String urlPath, Object handler) throws BeansException, IllegalStateException {
|
||||
Assert.notNull(urlPath, "URL path must not be null");
|
||||
Assert.notNull(handler, "Handler object must not be null");
|
||||
Object resolvedHandler = handler;
|
||||
@@ -196,12 +190,10 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
|
||||
PathPattern pattern = getPathPatternParser().parse(urlPath);
|
||||
if (this.handlerMap.containsKey(pattern)) {
|
||||
Object existingHandler = this.handlerMap.get(pattern);
|
||||
if (existingHandler != null) {
|
||||
if (existingHandler != resolvedHandler) {
|
||||
throw new IllegalStateException(
|
||||
"Cannot map " + getHandlerDescription(handler) + " to [" + urlPath + "]: " +
|
||||
"there is already " + getHandlerDescription(existingHandler) + " mapped.");
|
||||
}
|
||||
if (existingHandler != null && existingHandler != resolvedHandler) {
|
||||
throw new IllegalStateException(
|
||||
"Cannot map " + getHandlerDescription(handler) + " to [" + urlPath + "]: " +
|
||||
"there is already " + getHandlerDescription(existingHandler) + " mapped.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -220,6 +212,12 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
|
||||
}
|
||||
}
|
||||
|
||||
private String getHandlerDescription(Object handler) {
|
||||
return "handler " + (handler instanceof String ?
|
||||
"'" + handler + "'" : "of type [" + handler.getClass() + "]");
|
||||
}
|
||||
|
||||
|
||||
private static String prependLeadingSlash(String pattern) {
|
||||
if (StringUtils.hasLength(pattern) && !pattern.startsWith("/")) {
|
||||
return "/" + pattern;
|
||||
@@ -229,9 +227,4 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
|
||||
}
|
||||
}
|
||||
|
||||
private String getHandlerDescription(Object handler) {
|
||||
return "handler " + (handler instanceof String ?
|
||||
"'" + handler + "'" : "of type [" + handler.getClass() + "]");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -83,15 +83,6 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
|
||||
});
|
||||
}
|
||||
|
||||
private static String prependLeadingSlash(String pattern) {
|
||||
if (StringUtils.hasLength(pattern) && !pattern.startsWith("/")) {
|
||||
return "/" + pattern;
|
||||
}
|
||||
else {
|
||||
return pattern;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ContextRefreshedEvent event) {
|
||||
if (this.handlerMap.isEmpty()) {
|
||||
@@ -191,4 +182,14 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
|
||||
.orElse(Mono.empty());
|
||||
}
|
||||
|
||||
|
||||
private static String prependLeadingSlash(String pattern) {
|
||||
if (StringUtils.hasLength(pattern) && !pattern.startsWith("/")) {
|
||||
return "/" + pattern;
|
||||
}
|
||||
else {
|
||||
return pattern;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user