WARN against invalid patterns with PathPatternParser

As of gh-24952, `PathPatternParser` will strictly reject patterns with
`"**"` in the middle of them. `"**"` is only allowed at the end of the
pattern for matching multiple path segments until the end of the path.

Currently, if `"**"` is used in the middle of a pattern it will be
considered as a single `"*"` instead. Rejecting such cases should
clarify the situation.

This commit prepares for that upcoming change and:

* logs a warning message if such a case is used by an application
* expands the MVC and WebFlux documentation about URI matching in
general

Closes gh-24958
This commit is contained in:
Brian Clozel
2020-04-23 12:48:07 +02:00
parent 5eba1ae73c
commit dc4cda1b13
5 changed files with 94 additions and 14 deletions

View File

@@ -47,6 +47,10 @@ import org.springframework.util.StringUtils;
* and captures it as a variable named "spring"</li>
* </ul>
*
* Notable behavior difference with {@code AntPathMatcher}:<br>
* {@code **} and its capturing variant <code>{*spring}</code> cannot be used in the middle of a pattern
* string, only at the end: {@code /pages/{**}} is valid, but {@code /pages/{**}/details} is not.
*
* <h3>Examples</h3>
* <ul>
* <li>{@code /pages/t?st.html} &mdash; matches {@code /pages/test.html} as well as

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -16,6 +16,9 @@
package org.springframework.web.util.pattern;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.server.PathContainer;
/**
@@ -34,6 +37,8 @@ import org.springframework.http.server.PathContainer;
*/
public class PathPatternParser {
private static final Log logger = LogFactory.getLog(PathPatternParser.class);
private boolean matchOptionalTrailingSeparator = true;
private boolean caseSensitive = true;
@@ -104,11 +109,16 @@ public class PathPatternParser {
* stage. Produces a PathPattern object that can be used for fast matching
* against paths. Each invocation of this method delegates to a new instance of
* the {@link InternalPathPatternParser} because that class is not thread-safe.
* @param pathPattern the input path pattern, e.g. /foo/{bar}
* @param pathPattern the input path pattern, e.g. /project/{name}
* @return a PathPattern for quickly matching paths against request paths
* @throws PatternParseException in case of parse errors
*/
public PathPattern parse(String pathPattern) throws PatternParseException {
int wildcardIndex = pathPattern.indexOf("**" + this.pathOptions.separator());
if (wildcardIndex != -1 && wildcardIndex != pathPattern.length() - 3) {
logger.warn("'**' patterns are not supported in the middle of patterns and will be rejected in the future. " +
"Consider using '*' instead for matching a single path segment.");
}
return new InternalPathPatternParser(this).parse(pathPattern);
}