Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid performance issues that may arise when the array is large. #702

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 12 additions & 11 deletions java/org/apache/catalina/core/ApplicationFilterChain.java
Expand Up @@ -17,6 +17,8 @@
package org.apache.catalina.core;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import jakarta.servlet.Filter;
Expand Down Expand Up @@ -57,6 +59,8 @@ public final class ApplicationFilterChain implements FilterChain {
*/
private ApplicationFilterConfig[] filters = new ApplicationFilterConfig[0];

private final Set<ApplicationFilterConfig> addedFilters = new HashSet<>();


/**
* The int which is used to maintain the current position in the filter chain.
Expand Down Expand Up @@ -113,7 +117,7 @@ public void doFilter(ServletRequest request, ServletResponse response) throws IO
Filter filter = filterConfig.getFilter();

if (request.isAsyncSupported() &&
"false".equalsIgnoreCase(filterConfig.getFilterDef().getAsyncSupported())) {
Boolean.FALSE.toString().equalsIgnoreCase(filterConfig.getFilterDef().getAsyncSupported())) {
request.setAttribute(Globals.ASYNC_SUPPORTED_ATTR, Boolean.FALSE);
}
filter.doFilter(request, response, this);
Expand Down Expand Up @@ -180,20 +184,17 @@ public static ServletResponse getLastServicedResponse() {
* @param filterConfig The FilterConfig for the servlet to be executed
*/
void addFilter(ApplicationFilterConfig filterConfig) {

// Prevent the same filter being added multiple times
for (ApplicationFilterConfig filter : filters) {
if (filter == filterConfig) {
return;
}
if (addedFilters.contains(filterConfig)) {
return;
}

if (n == filters.length) {
ApplicationFilterConfig[] newFilters = new ApplicationFilterConfig[n + INCREMENT];
System.arraycopy(filters, 0, newFilters, 0, n);
filters = newFilters;
int newSize = n + INCREMENT;
filters = Arrays.copyOf(filters, newSize);
}
filters[n++] = filterConfig;
addedFilters.add(filterConfig);

}

Expand Down Expand Up @@ -250,9 +251,9 @@ void setDispatcherWrapsSameObject(boolean dispatcherWrapsSameObject) {
public void findNonAsyncFilters(Set<String> result) {
for (int i = 0; i < n; i++) {
ApplicationFilterConfig filter = filters[i];
if ("false".equalsIgnoreCase(filter.getFilterDef().getAsyncSupported())) {
if (Boolean.FALSE.toString().equalsIgnoreCase(filter.getFilterDef().getAsyncSupported())) {
result.add(filter.getFilterClass());
}
}
}
}
}