From b632c9e138875198fda69f0f48d8986767235253 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Thu, 24 Nov 2022 10:08:45 +0200 Subject: [PATCH] Ensure that Transfer-Encoding and Content-Length are not both set The combination of these two HTTP headers is illegal, so let's make sure RESTEasy Reactive does not send the Content-Length header when Transfer-Encoding is set Fixes: #29059 --- .../server/core/ServerSerialisers.java | 3 ++ .../vertx/test/headers/ChunkedHeaderTest.java | 40 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/headers/ChunkedHeaderTest.java diff --git a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/ServerSerialisers.java b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/ServerSerialisers.java index 0a27fbc05fa8c..2a2377cc3e021 100644 --- a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/ServerSerialisers.java +++ b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/ServerSerialisers.java @@ -522,6 +522,9 @@ public static void encodeResponseHeaders(ResteasyReactiveRequestContext requestC vertxResponse.addResponseHeader(header, (CharSequence) HeaderUtil.headerToString(o)); } } + if (header.equals("Transfer-Encoding")) { // using both headers together is not allowed + vertxResponse.removeResponseHeader("Content-Length"); + } } else { List strValues = new ArrayList<>(entry.getValue().size()); for (Object o : entry.getValue()) { diff --git a/independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/headers/ChunkedHeaderTest.java b/independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/headers/ChunkedHeaderTest.java new file mode 100644 index 0000000000000..3e94842869249 --- /dev/null +++ b/independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/headers/ChunkedHeaderTest.java @@ -0,0 +1,40 @@ +package org.jboss.resteasy.reactive.server.vertx.test.headers; + +import static io.restassured.RestAssured.*; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.core.Response; + +import org.jboss.resteasy.reactive.server.vertx.test.framework.ResteasyReactiveUnitTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +public class ChunkedHeaderTest { + + @RegisterExtension + static ResteasyReactiveUnitTest TEST = new ResteasyReactiveUnitTest() + .withApplicationRoot((jar) -> jar.addClasses(TestResource.class)); + + @Test + public void testReturnUni() { + given() + .get("/test/hello") + .then() + .statusCode(200) + .headers("Transfer-Encoding", "chunked") + .headers("Content-Length", is(nullValue())); + } + + @Path("/test") + public static class TestResource { + + @GET + @Path("hello") + public Response hello() { + return Response.ok("hello").header("Transfer-Encoding", "chunked").build(); + } + } +}