This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit 9e0baf73bb88283af9f2c08da100a5df2ae34e0d
Author: Brian Matzon <[email protected]>
AuthorDate: Thu Sep 25 10:37:58 2025 +0200

    Expose clientIdentifier generation as a function and expose a setter to 
modify it as needed
---
 .../catalina/valves/CrawlerSessionManagerValve.java    | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/java/org/apache/catalina/valves/CrawlerSessionManagerValve.java 
b/java/org/apache/catalina/valves/CrawlerSessionManagerValve.java
index 0c3eaca627..5c4e3a6a37 100644
--- a/java/org/apache/catalina/valves/CrawlerSessionManagerValve.java
+++ b/java/org/apache/catalina/valves/CrawlerSessionManagerValve.java
@@ -22,6 +22,7 @@ import java.io.Serializable;
 import java.util.Enumeration;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.Function;
 import java.util.regex.Pattern;
 
 import jakarta.servlet.ServletException;
@@ -60,6 +61,8 @@ public class CrawlerSessionManagerValve extends ValveBase {
 
     private boolean isContextAware = true;
 
+    private Function<Request, String> clientIdentifierFunction = 
this::getClientIdentifier;
+
 
     /**
      * Specifies a default constructor so async support can be configured.
@@ -163,6 +166,14 @@ public class CrawlerSessionManagerValve extends ValveBase {
         this.isContextAware = isContextAware;
     }
 
+    /**
+     * Specify the clientIdentifier function that will be used to identify 
unique clients. The default is to use
+     * the client IP address, optionally combined with the host name and 
context name
+     * @param clientIdentifierFunction
+     */
+    public void setClientIdentifierFunction(Function<Request, String> 
clientIdentifierFunction) {
+        this.clientIdentifierFunction = clientIdentifierFunction;
+    }
 
     @Override
     protected void initInternal() throws LifecycleException {
@@ -185,7 +196,7 @@ public class CrawlerSessionManagerValve extends ValveBase {
         boolean isBot = false;
         String sessionId = null;
         String clientIp = request.getRemoteAddr();
-        String clientIdentifier = getClientIdentifier(host, 
request.getContext(), clientIp);
+        String clientIdentifier = clientIdentifierFunction.apply(request);
 
         if (log.isTraceEnabled()) {
             log.trace(request.hashCode() + ": ClientIdentifier=" + 
clientIdentifier + ", RequestedSessionId=" +
@@ -263,8 +274,11 @@ public class CrawlerSessionManagerValve extends ValveBase {
         }
     }
 
+    private String getClientIdentifier(Request request) {
+        return getClientIdentifierDefault(request.getHost(), 
request.getContext(), request.getRemoteAddr());
+    }
 
-    private String getClientIdentifier(Host host, Context context, String 
clientIp) {
+    private String getClientIdentifierDefault(Host host, Context context, 
String clientIp) {
         StringBuilder result = new StringBuilder(clientIp);
         if (isHostAware) {
             result.append('-').append(host.getName());


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to