Skip to content

Commit

Permalink
Add Content-Range header to 206 Partial Content file response
Browse files Browse the repository at this point in the history
  • Loading branch information
Maldivia committed Dec 1, 2023
1 parent 31b92a0 commit 1a9e2e7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,37 @@ public void testFiles() throws Exception {
.then()
.statusCode(206)
.header(HttpHeaders.CONTENT_LENGTH, "10")
.header("Content-Range", "bytes 0-9/" + contentLength)
.body(Matchers.equalTo(content.substring(0, 10)));
RestAssured.given().header("Range", "bytes=10-19").get("/providers/file/file")
.then()
.statusCode(206)
.header(HttpHeaders.CONTENT_LENGTH, "10")
.header("Content-Range", "bytes 10-19/" + contentLength)
.body(Matchers.equalTo(content.substring(10, 20)));
RestAssured.given().header("Range", "bytes=10-").get("/providers/file/file")
.then()
.statusCode(206)
.header(HttpHeaders.CONTENT_LENGTH, String.valueOf(content.length() - 10))
.header("Content-Range", "bytes 10-" + (content.length() - 1) + "/" + contentLength)
.body(Matchers.equalTo(content.substring(10)));
RestAssured.given().header("Range", "bytes=-10").get("/providers/file/file")
.then()
.statusCode(206)
.header(HttpHeaders.CONTENT_LENGTH, "10")
.header("Content-Range",
"bytes " + (content.length() - 10) + "-" + (content.length() - 1) + "/" + contentLength)
.body(Matchers.equalTo(content.substring((content.length() - 10))));
RestAssured.given().header("Range", "bytes=" + (content.length() + 1) + "-").get("/providers/file/file")
.then()
.statusCode(200)
.header(HttpHeaders.CONTENT_LENGTH, contentLength)
.body(Matchers.equalTo(content));
RestAssured.given().header("Range", "bytes=0-1, 3-4").get("/providers/file/file")
.then()
.statusCode(200)
.header(HttpHeaders.CONTENT_LENGTH, contentLength)
.body(Matchers.equalTo(content));
RestAssured.get("/providers/file/file-partial")
.then()
.statusCode(200)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,25 @@ static void sendFile(File file, ServerRequestContext context) {
ResteasyReactiveRequestContext ctx = ((ResteasyReactiveRequestContext) context);
Object rangeObj = ctx.getHeader("Range", true);
ByteRange byteRange = rangeObj == null ? null : ByteRange.parse(rangeObj.toString());
long fileLength = file.length();
if ((byteRange != null) && (byteRange.ranges.size() == 1)) {
ByteRange.Range range = byteRange.ranges.get(0);
long length = range.getEnd() == -1 ? Long.MAX_VALUE : range.getEnd() - range.getStart() + 1;
context.serverResponse()
.setStatusCode(Response.Status.PARTIAL_CONTENT.getStatusCode())
.sendFile(file.getAbsolutePath(), range.getStart(), length);

} else {
context.serverResponse().sendFile(file.getAbsolutePath(), 0, file.length());
ByteRange.Range fileRange = (range.getStart() == -1)
? new ByteRange.Range(fileLength - range.getEnd(), fileLength - 1)
: new ByteRange.Range(range.getStart(), Math.min(fileLength - 1, range.getEnd()));

if ((fileRange.getStart() >= 0) && (fileRange.getStart() <= fileRange.getEnd())) {
String contentRange = "bytes " + fileRange.getStart() + "-" + fileRange.getEnd() + "/" + fileLength;
long length = fileRange.getEnd() - fileRange.getStart() + 1;
context.serverResponse()
.setStatusCode(Response.Status.PARTIAL_CONTENT.getStatusCode())
.setResponseHeader("Content-Range", contentRange)
.sendFile(file.getAbsolutePath(), fileRange.getStart(), length);
return;
}
}
context.serverResponse().sendFile(file.getAbsolutePath(), 0, fileLength);
}

/**
Expand Down Expand Up @@ -138,7 +147,7 @@ public static ByteRange parse(String rangeHeader) {
if (index + 1 < part.length()) {
end = Long.parseLong(part.substring(index + 1));
} else {
end = -1;
end = Long.MAX_VALUE;
}
ranges.add(new Range(start, end));
}
Expand Down

0 comments on commit 1a9e2e7

Please sign in to comment.