Skip to content

Commit 0a668e0

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 7d50e72 commit 0a668e0

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
@@ -602,7 +602,7 @@ protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws Se
602602
}
603603

604604
InputStream resourceInputStream = null;
605-
605+
File tempContentFile = null;
606606
try {
607607
// Append data specified in ranges to existing content for this
608608
// resource - create a temp. file on the local filesystem to
@@ -611,8 +611,8 @@ protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws Se
611611
if (range == IGNORE) {
612612
resourceInputStream = req.getInputStream();
613613
} else {
614-
File contentFile = executePartialPut(req, range, path);
615-
resourceInputStream = new FileInputStream(contentFile);
614+
tempContentFile = executePartialPut(req, range, path);
615+
resourceInputStream = new FileInputStream(tempContentFile);
616616
}
617617

618618
if (resources.write(path, resourceInputStream, true)) {
@@ -636,6 +636,9 @@ protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws Se
636636
// Ignore
637637
}
638638
}
639+
if (tempContentFile != null) {
640+
tempContentFile.delete();
641+
}
639642
}
640643
}
641644

@@ -658,13 +661,7 @@ protected File executePartialPut(HttpServletRequest req, ContentRange range, Str
658661
// resource - create a temp. file on the local filesystem to
659662
// perform this operation
660663
File tempDir = (File) getServletContext().getAttribute(ServletContext.TEMPDIR);
661-
// Convert all '/' characters to '.' in resourcePath
662-
String convertedResourcePath = path.replace('/', '.');
663-
File contentFile = new File(tempDir, convertedResourcePath);
664-
if (contentFile.createNewFile()) {
665-
// Clean up contentFile when Tomcat is terminated
666-
contentFile.deleteOnExit();
667-
}
664+
File contentFile = File.createTempFile("put-part-", null, tempDir);
668665

669666
try (RandomAccessFile randAccessContentFile = new RandomAccessFile(contentFile, "rw")) {
670667

webapps/docs/changelog.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@
204204
<code>IOException</code> occurs on a non-container thread during
205205
asynchronous processing. (markt)
206206
</fix>
207+
<fix>
208+
Enhance lifecycle of temporary files used by partial PUT. (remm)
209+
</fix>
207210
</changelog>
208211
</subsection>
209212
<subsection name="Coyote">

0 commit comments

Comments
 (0)