Skip to content

Commit

Permalink
Ensure partial put data range not exceed ContentRange declared
Browse files Browse the repository at this point in the history
Ensure write to bytes max to [end - start + 1], discard remaining part.
  • Loading branch information
Chenjp committed Jan 22, 2025
1 parent d21f7e6 commit 5593c81
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
7 changes: 6 additions & 1 deletion java/org/apache/catalina/servlets/DefaultServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -689,10 +689,15 @@ protected File executePartialPut(HttpServletRequest req, ContentRange range, Str
// Append data in request input stream to contentFile
randAccessContentFile.seek(range.getStart());
int numBytesRead;
long remainingBytes = range.getEnd() - range.getStart() + 1L;
byte[] transferBuffer = new byte[BUFFER_SIZE];
try (BufferedInputStream requestBufInStream = new BufferedInputStream(req.getInputStream(), BUFFER_SIZE)) {
while ((numBytesRead = requestBufInStream.read(transferBuffer)) != -1) {
while (remainingBytes > 0 && (numBytesRead = requestBufInStream.read(transferBuffer)) != -1) {
if (numBytesRead > remainingBytes) {
numBytesRead = (int) remainingBytes;
}
randAccessContentFile.write(transferBuffer, 0, numBytesRead);
remainingBytes -= numBytesRead;
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions test/org/apache/catalina/servlets/TestDefaultServletPut.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public static Collection<Object[]> parameters() {
"Content-Range: bytes 0-" + PATCH_LEN + "/" + START_LEN + CRLF, Boolean.TRUE, END_TEXT, Boolean.TRUE });
parameterSets.add(new Object[] {
"Content-Range: ByTeS 0-" + PATCH_LEN + "/" + START_LEN + CRLF, Boolean.TRUE, END_TEXT, Boolean.TRUE });
// Valid partial PUT, only the first char is replaced.
parameterSets.add(new Object[] {
"Content-Range: ByTeS 0-" + 0 + "/" + START_LEN + CRLF, Boolean.TRUE, "Etarting text", Boolean.TRUE });
// Full PUT
parameterSets.add(new Object[] {
"", null, PATCH_TEXT, Boolean.TRUE });
Expand Down

0 comments on commit 5593c81

Please sign in to comment.