This is an automated email from the ASF dual-hosted git repository.
markt-asf 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 c6aaf543e3 Follow-up to 0dd86e53
c6aaf543e3 is described below
commit c6aaf543e317d18d5c097338a0ef23b3bdd9ffc0
Author: Mark Thomas <[email protected]>
AuthorDate: Wed Sep 30 10:34:21 2026 +0100
Follow-up to 0dd86e53
Add validation.
Avoid overflow.
Reduce code duplication.
---
.../apache/catalina/ant/AbstractCatalinaTask.java | 34 +++++++++++++++++-----
java/org/apache/catalina/ant/DeployTask.java | 5 +---
2 files changed, 27 insertions(+), 12 deletions(-)
diff --git a/java/org/apache/catalina/ant/AbstractCatalinaTask.java
b/java/org/apache/catalina/ant/AbstractCatalinaTask.java
index 594b9215f6..d13d44373a 100644
--- a/java/org/apache/catalina/ant/AbstractCatalinaTask.java
+++ b/java/org/apache/catalina/ant/AbstractCatalinaTask.java
@@ -28,6 +28,7 @@ import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
+import java.util.concurrent.TimeUnit;
import org.apache.catalina.util.IOTools;
import org.apache.tomcat.util.http.Method;
@@ -119,6 +120,9 @@ public abstract class AbstractCatalinaTask extends
BaseRedirectorHelperTask {
* @param timeout the timeout in seconds; zero (the default) means no
timeout
*/
public void setTimeout(int timeout) {
+ if (timeout < 0) {
+ throw new IllegalArgumentException("timeout may not be negative");
+ }
this.timeout = timeout;
}
@@ -254,10 +258,7 @@ public abstract class AbstractCatalinaTask extends
BaseRedirectorHelperTask {
hconn.setAllowUserInteraction(false);
hconn.setDoInput(true);
hconn.setUseCaches(false);
- if (timeout > 0) {
- hconn.setConnectTimeout(timeout * 1000);
- hconn.setReadTimeout(timeout * 1000);
- }
+ configureTimeout(hconn, timeout);
// Set up authorization with our credentials
Authenticator authenticator = new TaskAuthenticator(username,
password);
@@ -387,6 +388,26 @@ public abstract class AbstractCatalinaTask extends
BaseRedirectorHelperTask {
}
+ /**
+ * Configures the given connection with the given timeout.
+ *
+ * @param urlConn The connection for which the timeout should be configured
+ * @param seconds Timeout to configure in seconds
+ */
+ protected void configureTimeout(URLConnection urlConn, int seconds) {
+ if (seconds > 0) {
+ // timeout is in seconds.
+ long millis = TimeUnit.SECONDS.toMillis(seconds);
+ // This is about 24 days so is unlikely to happen but limit it to
avoid an IAE in setConnectTimeout()
+ if (millis > Integer.MAX_VALUE) {
+ millis = Integer.MAX_VALUE;
+ }
+ urlConn.setConnectTimeout((int) millis);
+ urlConn.setReadTimeout((int) millis);
+ }
+ }
+
+
/*
* This is a hack. We need to use streaming to avoid OOME on large
uploads. We'd like to use
* Authenticator.setDefault() for authentication as the JRE then provides
the DIGEST client implementation. However,
@@ -408,10 +429,7 @@ public abstract class AbstractCatalinaTask extends
BaseRedirectorHelperTask {
hconn.setAllowUserInteraction(false);
hconn.setDoInput(true);
hconn.setUseCaches(false);
- if (timeout > 0) {
- hconn.setConnectTimeout(timeout * 1000);
- hconn.setReadTimeout(timeout * 1000);
- }
+ configureTimeout(hconn, timeout);
hconn.setDoOutput(false);
hconn.setAuthenticator(authenticator);
hconn.setRequestMethod(Method.OPTIONS);
diff --git a/java/org/apache/catalina/ant/DeployTask.java
b/java/org/apache/catalina/ant/DeployTask.java
index 6595b20d9e..baea4fdc30 100644
--- a/java/org/apache/catalina/ant/DeployTask.java
+++ b/java/org/apache/catalina/ant/DeployTask.java
@@ -188,10 +188,7 @@ public class DeployTask extends
AbstractCatalinaCommandTask {
try {
URI uri = new URI(war);
URLConnection conn = uri.toURL().openConnection();
- if (timeout > 0) {
- conn.setConnectTimeout(timeout * 1000);
- conn.setReadTimeout(timeout * 1000);
- }
+ configureTimeout(conn, timeout);
contentLength = conn.getContentLengthLong();
stream = new BufferedInputStream(conn.getInputStream(),
1024);
} catch (IOException | URISyntaxException e) {
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]