This is an automated email from the ASF dual-hosted git repository. remm 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 0a668e0c27 Enhance lifecycle of temporary files used by partial PUT 0a668e0c27 is described below commit 0a668e0c27f2b7ca0cc7c6eea32253b9b5ecb29c Author: remm <r...@apache.org> AuthorDate: Fri Jan 24 15:06:02 2025 +0100 Enhance lifecycle of temporary files used by partial PUT Delete temporary file right after finishing request processing. Simplify using createTempFile. --- java/org/apache/catalina/servlets/DefaultServlet.java | 17 +++++++---------- webapps/docs/changelog.xml | 3 +++ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/java/org/apache/catalina/servlets/DefaultServlet.java b/java/org/apache/catalina/servlets/DefaultServlet.java index 3e9f78c877..20ad454b94 100644 --- a/java/org/apache/catalina/servlets/DefaultServlet.java +++ b/java/org/apache/catalina/servlets/DefaultServlet.java @@ -602,7 +602,7 @@ public class DefaultServlet extends HttpServlet { } InputStream resourceInputStream = null; - + File tempContentFile = null; try { // Append data specified in ranges to existing content for this // resource - create a temp. file on the local filesystem to @@ -611,8 +611,8 @@ public class DefaultServlet extends HttpServlet { if (range == IGNORE) { resourceInputStream = req.getInputStream(); } else { - File contentFile = executePartialPut(req, range, path); - resourceInputStream = new FileInputStream(contentFile); + tempContentFile = executePartialPut(req, range, path); + resourceInputStream = new FileInputStream(tempContentFile); } if (resources.write(path, resourceInputStream, true)) { @@ -636,6 +636,9 @@ public class DefaultServlet extends HttpServlet { // Ignore } } + if (tempContentFile != null) { + tempContentFile.delete(); + } } } @@ -658,13 +661,7 @@ public class DefaultServlet extends HttpServlet { // resource - create a temp. file on the local filesystem to // perform this operation File tempDir = (File) getServletContext().getAttribute(ServletContext.TEMPDIR); - // Convert all '/' characters to '.' in resourcePath - String convertedResourcePath = path.replace('/', '.'); - File contentFile = new File(tempDir, convertedResourcePath); - if (contentFile.createNewFile()) { - // Clean up contentFile when Tomcat is terminated - contentFile.deleteOnExit(); - } + File contentFile = File.createTempFile("put-part-", null, tempDir); try (RandomAccessFile randAccessContentFile = new RandomAccessFile(contentFile, "rw")) { diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index e57e9ccc1d..41ffa3b2c0 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -204,6 +204,9 @@ <code>IOException</code> occurs on a non-container thread during asynchronous processing. (markt) </fix> + <fix> + Enhance lifecycle of temporary files used by partial PUT. (remm) + </fix> </changelog> </subsection> <subsection name="Coyote"> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org