diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java index 9a864b3a34..4ebd776474 100644 --- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java +++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java @@ -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("