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

remm pushed a commit to branch 10.1.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/10.1.x by this push:
     new f6c01d6577 Enhance lifecycle of temporary files used by partial PUT
f6c01d6577 is described below

commit f6c01d6577cf9a1e06792be47e623d36acc3b5dc
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 27ad57a172..80290468da 100644
--- a/java/org/apache/catalina/servlets/DefaultServlet.java
+++ b/java/org/apache/catalina/servlets/DefaultServlet.java
@@ -625,7 +625,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
@@ -634,8 +634,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)) {
@@ -659,6 +659,9 @@ public class DefaultServlet extends HttpServlet {
                     // Ignore
                 }
             }
+            if (tempContentFile != null) {
+                tempContentFile.delete();
+            }
         }
     }
 
@@ -681,13 +684,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 3d66c9a2c2..036519edb7 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -181,6 +181,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

Reply via email to