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

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


The following commit(s) were added to refs/heads/main by this push:
     new 466996f8b0 Improve handling for failed protocols
466996f8b0 is described below

commit 466996f8b096b0aa752929ee8cb751ed069e5986
Author: remm <[email protected]>
AuthorDate: Wed May 20 13:26:14 2026 +0200

    Improve handling for failed protocols
---
 java/org/apache/catalina/connector/Connector.java | 57 ++++++++++++++++++++---
 webapps/docs/changelog.xml                        |  4 ++
 2 files changed, 54 insertions(+), 7 deletions(-)

diff --git a/java/org/apache/catalina/connector/Connector.java 
b/java/org/apache/catalina/connector/Connector.java
index 563b43c0cd..8def271a9e 100644
--- a/java/org/apache/catalina/connector/Connector.java
+++ b/java/org/apache/catalina/connector/Connector.java
@@ -647,6 +647,26 @@ public class Connector extends LifecycleMBeanBase {
     }
 
 
+    /**
+     * Returns the unique index for this connector.
+     * @return the index
+     */
+    public int getNameIndex() {
+        // Try shortcut that should work for nearly all uses first as it does
+        // not use reflection and is therefore faster.
+        if (protocolHandler instanceof AbstractProtocol<?>) {
+            return ((AbstractProtocol<?>) protocolHandler).getNameIndex();
+        }
+        // Fall back for custom protocol handlers not based on AbstractProtocol
+        Object index = getProperty("NameIndex");
+        if (index instanceof Integer) {
+            return ((Integer) index).intValue();
+        }
+        // Usually means an invalid protocol has been configured.
+        return 0;
+    }
+
+
     /**
      * Returns the port number on which this connector is configured to listen 
for requests.
      * The special value of 0 means select a random free port when the socket 
is bound.
@@ -728,7 +748,18 @@ public class Connector extends LifecycleMBeanBase {
      * @return the actual local port number
      */
     public int getLocalPort() {
-        return ((Integer) getProperty("localPort")).intValue();
+        // Try shortcut that should work for nearly all uses first as it does
+        // not use reflection and is therefore faster.
+        if (protocolHandler instanceof AbstractProtocol<?>) {
+            return ((AbstractProtocol<?>) protocolHandler).getLocalPort();
+        }
+        // Fall back for custom protocol handlers not based on AbstractProtocol
+        Object port = getProperty("localPort");
+        if (port instanceof Integer) {
+            return ((Integer) port).intValue();
+        }
+        // Usually means an invalid protocol has been configured.
+        return 0;
     }
 
 
@@ -985,7 +1016,9 @@ public class Connector extends LifecycleMBeanBase {
      * @param sslHostConfig the SSL host configuration to add
      */
     public void addSslHostConfig(SSLHostConfig sslHostConfig) {
-        protocolHandler.addSslHostConfig(sslHostConfig);
+        if (protocolHandler != null) {
+            protocolHandler.addSslHostConfig(sslHostConfig);
+        }
     }
 
 
@@ -994,7 +1027,11 @@ public class Connector extends LifecycleMBeanBase {
      * @return array of SSL host configurations
      */
     public SSLHostConfig[] findSslHostConfigs() {
-        return protocolHandler.findSslHostConfigs();
+        if (protocolHandler != null) {
+            return protocolHandler.findSslHostConfigs();
+        } else {
+            return new SSLHostConfig[0];
+        }
     }
 
 
@@ -1003,7 +1040,9 @@ public class Connector extends LifecycleMBeanBase {
      * @param upgradeProtocol the upgrade protocol to add
      */
     public void addUpgradeProtocol(UpgradeProtocol upgradeProtocol) {
-        protocolHandler.addUpgradeProtocol(upgradeProtocol);
+        if (protocolHandler != null) {
+            protocolHandler.addUpgradeProtocol(upgradeProtocol);
+        }
     }
 
 
@@ -1012,7 +1051,11 @@ public class Connector extends LifecycleMBeanBase {
      * @return array of upgrade protocols
      */
     public UpgradeProtocol[] findUpgradeProtocols() {
-        return protocolHandler.findUpgradeProtocols();
+        if (protocolHandler != null) {
+            return protocolHandler.findUpgradeProtocols();
+        } else {
+            return new UpgradeProtocol[0];
+        }
     }
 
 
@@ -1150,7 +1193,7 @@ public class Connector extends LifecycleMBeanBase {
                 sb.append(port);
             } else {
                 sb.append("auto-");
-                sb.append(getProperty("nameIndex"));
+                sb.append(Integer.valueOf(getNameIndex()));
             }
             String address = "";
             if (addressObj instanceof InetAddress) {
@@ -1326,7 +1369,7 @@ public class Connector extends LifecycleMBeanBase {
                     sb.append(port);
                 } else {
                     sb.append("auto-");
-                    sb.append(getProperty("nameIndex"));
+                    sb.append(Integer.valueOf(getNameIndex()));
                 }
             }
         } else {
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 16fc43cc9e..5635490446 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -233,6 +233,10 @@
       <fix>
         Cleaner handling of invalid SPNEGO tokens. (remm)
       </fix>
+      <fix>
+        Avoid some NPEs in the Connector class on an uninitialize protocol.
+        (remm)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Coyote">


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

Reply via email to