Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(s3): strip charset parameter from XML response Content-Type (#809)

RyotaroKoshio6f57af3edc44
diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3ContentTypeCharsetFilter.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3ContentTypeCharsetFilter.java
new file mode 100644
index 0000000000..3913422401
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3ContentTypeCharsetFilter.java
@@ -0,0 +1,38 @@
+package io.github.hectorvent.floci.services.s3;
+
+import io.quarkus.vertx.http.runtime.CurrentVertxRequest;
+import io.vertx.ext.web.RoutingContext;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import jakarta.ws.rs.container.ContainerRequestContext;
+import jakarta.ws.rs.container.ContainerRequestFilter;
+import jakarta.ws.rs.ext.Provider;
+
+/**
+ * Strips the {@code charset} parameter from {@code application/xml} and
+ * {@code text/xml} response Content-Type headers so they match the real AWS
+ * XML protocol wire format.
+ */
+@Provider
+@ApplicationScoped
+public class S3ContentTypeCharsetFilter implements ContainerRequestFilter {
+
+ @Inject
+ CurrentVertxRequest currentVertxRequest;
+
+ @Override
+ public void filter(ContainerRequestContext requestContext) {
+ RoutingContext rc = currentVertxRequest.getCurrent();
+ if (rc == null) return;
+ rc.addHeadersEndHandler(v -> {
+ String ct = rc.response().headers().get("Content-Type");
+ if (ct == null) return;
+ int semi = ct.indexOf(';');
+ if (semi < 0) return;
+ String base = ct.substring(0, semi).trim();
+ if ("application/xml".equalsIgnoreCase(base) || "text/xml".equalsIgnoreCase(base)) {
+ rc.response().headers().set("Content-Type", base);
+ }
+ });
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3ContentTypeCharsetFilterTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3ContentTypeCharsetFilterTest.java
new file mode 100644
index 0000000000..16c295b398
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3ContentTypeCharsetFilterTest.java
@@ -0,0 +1,46 @@
+package io.github.hectorvent.floci.services.s3;
+
+import io.quarkus.test.junit.QuarkusTest;
+import org.junit.jupiter.api.Test;
+
+import static io.restassured.RestAssured.given;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.startsWith;
+
+/**
+ * Verifies S3 XML responses match the real AWS wire format
+ * ({@code Content-Type: application/xml}, no {@code charset} parameter).
+ */
+@QuarkusTest
+class S3ContentTypeCharsetFilterTest {
+
+ @Test
+ void listBucketsContentTypeHasNoCharset() {
+ given()
+ .when()
+ .get("/")
+ .then()
+ .statusCode(200)
+ .header("Content-Type", equalTo("application/xml"));
+ }
+
+ @Test
+ void noSuchBucketErrorContentTypeHasNoCharset() {
+ given()
+ .when()
+ .get("/charset-test-missing-bucket-xyz")
+ .then()
+ .statusCode(404)
+ .header("Content-Type", equalTo("application/xml"));
+ }
+
+ @Test
+ void jsonResponseContentTypeUntouched() {
+ given()
+ .when()
+ .get("/_floci/health")
+ .then()
+ .statusCode(200)
+ .header("Content-Type", startsWith("application/json"));
+ }
+}