This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch 10.1.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit 29f6bec8c6d016e82b66aa70e0c85f24e36203c1 Author: Mark Thomas <ma...@apache.org> AuthorDate: Wed Jun 26 11:58:39 2024 +0100 Prepare for the fix for BZ 69131 Refactor matching of request paths to filter mappings to a utility class so it can be reused if required. --- .../catalina/core/ApplicationFilterFactory.java | 92 +-------------- java/org/apache/catalina/util/FilterUtil.java | 125 +++++++++++++++++++++ 2 files changed, 128 insertions(+), 89 deletions(-) diff --git a/java/org/apache/catalina/core/ApplicationFilterFactory.java b/java/org/apache/catalina/core/ApplicationFilterFactory.java index d71220de8e..be03358a09 100644 --- a/java/org/apache/catalina/core/ApplicationFilterFactory.java +++ b/java/org/apache/catalina/core/ApplicationFilterFactory.java @@ -23,6 +23,7 @@ import jakarta.servlet.ServletRequest; import org.apache.catalina.Globals; import org.apache.catalina.Wrapper; import org.apache.catalina.connector.Request; +import org.apache.catalina.util.FilterUtil; import org.apache.juli.logging.Log; import org.apache.juli.logging.LogFactory; import org.apache.tomcat.util.descriptor.web.FilterMap; @@ -95,11 +96,7 @@ public final class ApplicationFilterFactory { // Acquire the information we will need to match filter mappings DispatcherType dispatcher = (DispatcherType) request.getAttribute(Globals.DISPATCHER_TYPE_ATTR); - String requestPath = null; - Object attribute = request.getAttribute(Globals.DISPATCHER_REQUEST_PATH_ATTR); - if (attribute != null) { - requestPath = attribute.toString(); - } + String requestPath = FilterUtil.getRequestPath(request) ; String servletName = wrapper.getName(); @@ -108,7 +105,7 @@ public final class ApplicationFilterFactory { if (!matchDispatcher(filterMap, dispatcher)) { continue; } - if (!matchFiltersURL(filterMap, requestPath)) { + if (!FilterUtil.matchFiltersURL(filterMap, requestPath)) { continue; } ApplicationFilterConfig filterConfig = @@ -145,89 +142,6 @@ public final class ApplicationFilterFactory { // -------------------------------------------------------- Private Methods - /** - * Return <code>true</code> if the context-relative request path matches the requirements of the specified filter - * mapping; otherwise, return <code>false</code>. - * - * @param filterMap Filter mapping being checked - * @param requestPath Context-relative request path of this request - */ - private static boolean matchFiltersURL(FilterMap filterMap, String requestPath) { - - // Check the specific "*" special URL pattern, which also matches - // named dispatches - if (filterMap.getMatchAllUrlPatterns()) { - return true; - } - - if (requestPath == null) { - return false; - } - - // Match on context relative request path - String[] testPaths = filterMap.getURLPatterns(); - - for (String testPath : testPaths) { - if (matchFiltersURL(testPath, requestPath)) { - return true; - } - } - - // No match - return false; - - } - - - /** - * Return <code>true</code> if the context-relative request path matches the requirements of the specified filter - * mapping; otherwise, return <code>false</code>. - * - * @param testPath URL mapping being checked - * @param requestPath Context-relative request path of this request - */ - private static boolean matchFiltersURL(String testPath, String requestPath) { - - if (testPath == null) { - return false; - } - - // Case 1 - Exact Match - if (testPath.equals(requestPath)) { - return true; - } - - // Case 2 - Path Match ("/.../*") - if (testPath.equals("/*")) { - return true; - } - if (testPath.endsWith("/*")) { - if (testPath.regionMatches(0, requestPath, 0, testPath.length() - 2)) { - if (requestPath.length() == (testPath.length() - 2)) { - return true; - } else if ('/' == requestPath.charAt(testPath.length() - 2)) { - return true; - } - } - return false; - } - - // Case 3 - Extension Match - if (testPath.startsWith("*.")) { - int slash = requestPath.lastIndexOf('/'); - int period = requestPath.lastIndexOf('.'); - if ((slash >= 0) && (period > slash) && (period != requestPath.length() - 1) && - ((requestPath.length() - period) == (testPath.length() - 1))) { - return testPath.regionMatches(2, requestPath, period + 1, testPath.length() - 2); - } - } - - // Case 4 - "Default" Match - return false; // NOTE - Not relevant for selecting filters - - } - - /** * Return <code>true</code> if the specified servlet name matches the requirements of the specified filter mapping; * otherwise return <code>false</code>. diff --git a/java/org/apache/catalina/util/FilterUtil.java b/java/org/apache/catalina/util/FilterUtil.java new file mode 100644 index 0000000000..1d32292d18 --- /dev/null +++ b/java/org/apache/catalina/util/FilterUtil.java @@ -0,0 +1,125 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.catalina.util; + +import jakarta.servlet.ServletRequest; + +import org.apache.catalina.Globals; +import org.apache.tomcat.util.descriptor.web.FilterMap; + +/** + * General purpose utility methods related to filters and filter processing. + */ +public class FilterUtil { + + private FilterUtil() { + // Utility class. Hide default constructor. + } + + + /** + * Tests if the provided, context-relative, request path matches the provided filter mapping. + * + * @param filterMap Filter mapping being checked + * @param requestPath Context-relative request path of this request + * + * @return <code>true</code> if the context-relative request path matches the requirements of the specified filter + * mapping; otherwise, return <code>false</code>. + */ + public static boolean matchFiltersURL(FilterMap filterMap, String requestPath) { + + // Check the specific "*" special URL pattern, which also matches + // named dispatches + if (filterMap.getMatchAllUrlPatterns()) { + return true; + } + + if (requestPath == null) { + return false; + } + + // Match on context relative request path + String[] testPaths = filterMap.getURLPatterns(); + + for (String testPath : testPaths) { + if (matchFiltersURL(testPath, requestPath)) { + return true; + } + } + + // No match + return false; + } + + + /** + * Return <code>true</code> if the context-relative request path matches the requirements of the specified filter + * mapping; otherwise, return <code>false</code>. + * + * @param testPath URL mapping being checked + * @param requestPath Context-relative request path of this request + */ + private static boolean matchFiltersURL(String testPath, String requestPath) { + + if (testPath == null) { + return false; + } + + // Case 1 - Exact Match + if (testPath.equals(requestPath)) { + return true; + } + + // Case 2 - Path Match ("/.../*") + if (testPath.equals("/*")) { + return true; + } + if (testPath.endsWith("/*")) { + if (testPath.regionMatches(0, requestPath, 0, testPath.length() - 2)) { + if (requestPath.length() == (testPath.length() - 2)) { + return true; + } else if ('/' == requestPath.charAt(testPath.length() - 2)) { + return true; + } + } + return false; + } + + // Case 3 - Extension Match + if (testPath.startsWith("*.")) { + int slash = requestPath.lastIndexOf('/'); + int period = requestPath.lastIndexOf('.'); + if ((slash >= 0) && (period > slash) && (period != requestPath.length() - 1) && + ((requestPath.length() - period) == (testPath.length() - 1))) { + return testPath.regionMatches(2, requestPath, period + 1, testPath.length() - 2); + } + } + + // Case 4 - "Default" Match + return false; // NOTE - Not relevant for selecting filters + } + + + public static String getRequestPath(ServletRequest request) { + String result = null; + Object attribute = request.getAttribute(Globals.DISPATCHER_REQUEST_PATH_ATTR); + if (attribute != null) { + result = attribute.toString(); + } + return result; + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org