Skip to content

Commit

Permalink
Enhance lifecycle of temporary files used by partial PUT
Browse files Browse the repository at this point in the history
Delete temporary file right after finishing request processing.
Simplify using createTempFile.
  • Loading branch information
rmaucher committed Jan 24, 2025
1 parent 662f814 commit eb61aad
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
17 changes: 7 additions & 10 deletions java/org/apache/catalina/servlets/DefaultServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws Se
}

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
Expand All @@ -634,8 +634,8 @@ protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws Se
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)) {
Expand All @@ -659,6 +659,9 @@ protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws Se
// Ignore
}
}
if (tempContentFile != null) {
tempContentFile.delete();
}
}
}

Expand All @@ -681,13 +684,7 @@ protected File executePartialPut(HttpServletRequest req, Range range, String pat
// 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")) {

Expand Down
3 changes: 3 additions & 0 deletions webapps/docs/changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down

0 comments on commit eb61aad

Please sign in to comment.