mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
feat(s3): add Range header support for GetObject (#44)
Implement HTTP Range header parsing for S3 GetObject requests. Supports standard range formats: bytes=start-end, bytes=start-, and bytes=-suffix. Returns 206 Partial Content with Content-Range header for valid ranges, and 416 Range Not Satisfiable for invalid ranges. Also adds Accept-Ranges: bytes to GET and HEAD responses. Fixes #40
This commit is contained in:
1 parent
293aeeb6c6
commit
b0f5ae22cd
2 files changed
+197
-2
No files matched your search
@@ -368,6 +368,7 @@ public class S3Controller {
|
||||
@HeaderParam("If-None-Match") String ifNoneMatch,
|
||||
@HeaderParam("If-Modified-Since") String ifModifiedSince,
|
||||
@HeaderParam("If-Unmodified-Since") String ifUnmodifiedSince,
|
||||
@HeaderParam("Range") String rangeHeader,
|
||||
@Context UriInfo uriInfo,
|
||||
@Context HttpHeaders httpHeaders) {
|
||||
try {
|
||||
@@ -399,11 +400,17 @@ public class S3Controller {
|
||||
}
|
||||
}
|
||||
S3Object obj = s3Service.getObject(bucket, key, versionId);
|
||||
|
||||
if (rangeHeader != null && rangeHeader.startsWith("bytes=")) {
|
||||
return handleRangeRequest(obj, rangeHeader);
|
||||
}
|
||||
|
||||
var resp = Response.ok(obj.getData())
|
||||
.header("Content-Type", obj.getContentType())
|
||||
.header("Content-Length", obj.getSize())
|
||||
.header("ETag", obj.getETag())
|
||||
.header("Last-Modified", RFC_822.format(obj.getLastModified()));
|
||||
.header("Last-Modified", RFC_822.format(obj.getLastModified()))
|
||||
.header("Accept-Ranges", "bytes");
|
||||
if (obj.getVersionId() != null) {
|
||||
resp.header("x-amz-version-id", obj.getVersionId());
|
||||
}
|
||||
@@ -414,6 +421,69 @@ public class S3Controller {
|
||||
}
|
||||
}
|
||||
|
||||
private Response handleRangeRequest(S3Object obj, String rangeHeader) {
|
||||
byte[] data = obj.getData();
|
||||
int totalSize = data.length;
|
||||
String rangeSpec = rangeHeader.substring("bytes=".length()).trim();
|
||||
|
||||
int start, end;
|
||||
try {
|
||||
int dash = rangeSpec.indexOf('-');
|
||||
if (dash < 0) {
|
||||
return invalidRangeResponse(totalSize);
|
||||
}
|
||||
String before = rangeSpec.substring(0, dash);
|
||||
String after = rangeSpec.substring(dash + 1);
|
||||
if (before.isEmpty() && after.isEmpty()) {
|
||||
return invalidRangeResponse(totalSize);
|
||||
}
|
||||
if (before.isEmpty()) {
|
||||
int suffix = Integer.parseInt(after);
|
||||
if (suffix <= 0) {
|
||||
return invalidRangeResponse(totalSize);
|
||||
}
|
||||
start = Math.max(0, totalSize - suffix);
|
||||
end = totalSize - 1;
|
||||
} else {
|
||||
start = Integer.parseInt(before);
|
||||
end = after.isEmpty() ? totalSize - 1 : Math.min(Integer.parseInt(after), totalSize - 1);
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
return invalidRangeResponse(totalSize);
|
||||
}
|
||||
|
||||
if (start < 0 || start >= totalSize || start > end) {
|
||||
return invalidRangeResponse(totalSize);
|
||||
}
|
||||
|
||||
byte[] rangeData = java.util.Arrays.copyOfRange(data, start, end + 1);
|
||||
return Response.status(206)
|
||||
.entity(rangeData)
|
||||
.header("Content-Type", obj.getContentType())
|
||||
.header("Content-Length", rangeData.length)
|
||||
.header("Content-Range", "bytes " + start + "-" + end + "/" + totalSize)
|
||||
.header("ETag", obj.getETag())
|
||||
.header("Last-Modified", RFC_822.format(obj.getLastModified()))
|
||||
.header("Accept-Ranges", "bytes")
|
||||
.build();
|
||||
}
|
||||
|
||||
private Response invalidRangeResponse(int totalSize) {
|
||||
String xml = new XmlBuilder()
|
||||
.raw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
|
||||
.start("Error")
|
||||
.elem("Code", "InvalidRange")
|
||||
.elem("Message", "The requested range is not satisfiable.")
|
||||
.elem("RequestId", java.util.UUID.randomUUID().toString())
|
||||
.end("Error")
|
||||
.build();
|
||||
return Response.status(416)
|
||||
.entity(xml)
|
||||
.type(MediaType.APPLICATION_XML)
|
||||
.header("Content-Range", "bytes */" + totalSize)
|
||||
.build();
|
||||
}
|
||||
|
||||
@HEAD
|
||||
@Path("/{bucket}/{key:.+}")
|
||||
public Response headObject(@PathParam("bucket") String bucket,
|
||||
@@ -433,7 +503,8 @@ public class S3Controller {
|
||||
.header("Content-Type", obj.getContentType())
|
||||
.header("Content-Length", obj.getSize())
|
||||
.header("ETag", obj.getETag())
|
||||
.header("Last-Modified", RFC_822.format(obj.getLastModified()));
|
||||
.header("Last-Modified", RFC_822.format(obj.getLastModified()))
|
||||
.header("Accept-Ranges", "bytes");
|
||||
if (obj.getVersionId() != null) {
|
||||
resp.header("x-amz-version-id", obj.getVersionId());
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user