mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(s3): serve custom error document for missing keys in static websi… (#1334)
* fix(s3): serve custom error document for missing keys in static website hosting When a bucket has a website configuration with an ErrorDocument, requests to missing keys now return the error document with a 404 status instead of the default XML NoSuchKey error. * fix(test): handle null output in waitForExecution to prevent flaky CI failures * fix(s3): address review feedback on static website hosting * fix(s3): recognize s3-website hostnames in virtual host filter * fix(s3): gate website error document on website endpoint host * fix(s3): restore validateRawUri inside try block after rebase
This commit is contained in:
1 parent
398535ed55
commit
28a1427ea8
4 files changed
+132
-16
No files matched your search
@@ -307,7 +307,8 @@ public class S3Controller {
|
||||
@QueryParam("encoding-type") String encodingType,
|
||||
@QueryParam("key-marker") String keyMarker,
|
||||
@QueryParam("marker") String marker,
|
||||
@Context UriInfo uriInfo) {
|
||||
@Context UriInfo uriInfo,
|
||||
@Context HttpHeaders httpHeaders) {
|
||||
try {
|
||||
validateRawUri();
|
||||
if (hasQueryParam(uriInfo, "uploads")) {
|
||||
@@ -368,7 +369,7 @@ public class S3Controller {
|
||||
}
|
||||
|
||||
// --- Website Hosting Redirection Logic ---
|
||||
if (uriInfo.getQueryParameters().isEmpty() || (uriInfo.getQueryParameters().size() == 1 && hasQueryParam(uriInfo, "list-type"))) {
|
||||
if (isWebsiteRequest(httpHeaders) && (uriInfo.getQueryParameters().isEmpty() || (uriInfo.getQueryParameters().size() == 1 && hasQueryParam(uriInfo, "list-type")))) {
|
||||
try {
|
||||
WebsiteConfiguration webConfig = s3Service.getBucketWebsite(bucket);
|
||||
if (webConfig.getIndexDocument() != null) {
|
||||
@@ -381,7 +382,8 @@ public class S3Controller {
|
||||
.header("x-amz-website-redirect-location", "index")
|
||||
.build();
|
||||
} catch (AwsException e) {
|
||||
// If index.html is missing, we could serve ErrorDocument, but for now we fall back to listObjects
|
||||
Response r = serveErrorDocument(bucket, webConfig);
|
||||
if (r != null) return r;
|
||||
}
|
||||
}
|
||||
} catch (AwsException e) {
|
||||
@@ -634,6 +636,14 @@ public class S3Controller {
|
||||
|
||||
return fullObjectResponse(bucket, key, versionId, obj, overrides);
|
||||
} catch (AwsException e) {
|
||||
if ("NoSuchKey".equals(e.getErrorCode()) && isWebsiteRequest(httpHeaders)) {
|
||||
try {
|
||||
WebsiteConfiguration webConfig = s3Service.getBucketWebsite(bucket);
|
||||
Response r = serveErrorDocument(bucket, webConfig);
|
||||
if (r != null) return r;
|
||||
} catch (AwsException ignored) {
|
||||
}
|
||||
}
|
||||
return xmlErrorResponse(e);
|
||||
}
|
||||
}
|
||||
@@ -1988,6 +1998,34 @@ public class S3Controller {
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isWebsiteRequest(HttpHeaders httpHeaders) {
|
||||
String host = httpHeaders.getHeaderString("Host");
|
||||
return host != null && host.contains("s3-website");
|
||||
}
|
||||
|
||||
private Response serveErrorDocument(String bucket, WebsiteConfiguration cfg) {
|
||||
if (cfg.getErrorDocument() == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
S3Object err = s3Service.getObject(bucket, cfg.getErrorDocument());
|
||||
return Response.status(404)
|
||||
.entity(err.getData())
|
||||
.type(err.getContentType())
|
||||
.header("Content-Length", err.getSize())
|
||||
.header("x-amz-error-code", "NoSuchKey")
|
||||
.header("x-amz-error-message", "The specified key does not exist.")
|
||||
.build();
|
||||
} catch (AwsException ignored) {
|
||||
return Response.status(404)
|
||||
.entity("<html><head><title>404 Not Found</title></head>\n<body><h1>404 Not Found</h1>\n<ul><li>Code: NoSuchKey</li><li>Message: The specified key does not exist.</li></ul></body></html>")
|
||||
.type(MediaType.TEXT_HTML)
|
||||
.header("x-amz-error-code", "NoSuchKey")
|
||||
.header("x-amz-error-message", "The specified key does not exist.")
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
private Response xmlErrorResponse(AwsException e) {
|
||||
String condition = e instanceof S3PreconditionFailedException preconditionFailedException
|
||||
? preconditionFailedException.condition()
|
||||
|
||||
@@ -205,6 +205,10 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {
|
||||
if (remainder.endsWith(".localhost.floci.io") || "localhost.floci.io".equals(remainder)) {
|
||||
return remainder.startsWith("s3.") || "localhost.floci.io".equals(remainder);
|
||||
}
|
||||
// S3 website endpoints: bucket.s3-website-<region>.amazonaws.com, bucket.s3-website-<region>.localhost, etc.
|
||||
if (remainder.startsWith("s3-website")) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user