Skip to content

Commit eb61aad

Browse files
committed
Enhance lifecycle of temporary files used by partial PUT
Delete temporary file right after finishing request processing. Simplify using createTempFile.
1 parent 662f814 commit eb61aad

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

java/org/apache/catalina/servlets/DefaultServlet.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws Se
625625
}
626626

627627
InputStream resourceInputStream = null;
628-
628+
File tempContentFile = null;
629629
try {
630630
// Append data specified in ranges to existing content for this
631631
// resource - create a temp. file on the local filesystem to
@@ -634,8 +634,8 @@ protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws Se
634634
if (range == IGNORE) {
635635
resourceInputStream = req.getInputStream();
636636
} else {
637-
File contentFile = executePartialPut(req, range, path);
638-
resourceInputStream = new FileInputStream(contentFile);
637+
tempContentFile = executePartialPut(req, range, path);
638+
resourceInputStream = new FileInputStream(tempContentFile);
639639
}
640640

641641
if (resources.write(path, resourceInputStream, true)) {
@@ -659,6 +659,9 @@ protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws Se
659659
// Ignore
660660
}
661661
}
662+
if (tempContentFile != null) {
663+
tempContentFile.delete();
664+
}
662665
}
663666
}
664667

@@ -681,13 +684,7 @@ protected File executePartialPut(HttpServletRequest req, Range range, String pat
681684
// resource - create a temp. file on the local filesystem to
682685
// perform this operation
683686
File tempDir = (File) getServletContext().getAttribute(ServletContext.TEMPDIR);
684-
// Convert all '/' characters to '.' in resourcePath
685-
String convertedResourcePath = path.replace('/', '.');
686-
File contentFile = new File(tempDir, convertedResourcePath);
687-
if (contentFile.createNewFile()) {
688-
// Clean up contentFile when Tomcat is terminated
689-
contentFile.deleteOnExit();
690-
}
687+
File contentFile = File.createTempFile("put-part-", null, tempDir);
691688

692689
try (RandomAccessFile randAccessContentFile = new RandomAccessFile(contentFile, "rw")) {
693690

webapps/docs/changelog.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@
181181
<code>IOException</code> occurs on a non-container thread during
182182
asynchronous processing. (markt)
183183
</fix>
184+
<fix>
185+
Enhance lifecycle of temporary files used by partial PUT. (remm)
186+
</fix>
184187
</changelog>
185188
</subsection>
186189
<subsection name="Coyote">

0 commit comments

Comments
 (0)