Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly ensure that transfer-encoding is not set when content-length exists #26467

Merged
merged 1 commit into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
import io.vertx.core.Context;
import io.vertx.core.Handler;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpServerRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import javax.ws.rs.core.MultivaluedMap;
import org.jboss.logging.Logger;
import org.jboss.resteasy.reactive.server.core.LazyResponse;
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext;

public class ResteasyReactiveOutputStream extends OutputStream {
Expand Down Expand Up @@ -233,12 +236,24 @@ private void prepareWrite(ByteBuf buffer, boolean finished) throws IOException {
} else {
context.serverResponse().setResponseHeader(HttpHeaderNames.CONTENT_LENGTH, "" + buffer.readableBytes());
}
} else if (!request.response().headers().contains(HttpHeaderNames.CONTENT_LENGTH)) {
} else if (!contentLengthSet()) {
request.response().setChunked(true);
}
}
}

private boolean contentLengthSet() {
if (request.response().headers().contains(HttpHeaderNames.CONTENT_LENGTH)) {
return true;
}
LazyResponse lazyResponse = context.getResponse();
if (!lazyResponse.isCreated()) {
return false;
}
MultivaluedMap<String, Object> responseHeaders = lazyResponse.get().getHeaders();
return (responseHeaders != null) && responseHeaders.containsKey(HttpHeaders.CONTENT_LENGTH);
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.jboss.resteasy.reactive.server.vertx.test.response;

import static io.restassured.RestAssured.when;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.core.IsNull.nullValue;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.UUID;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
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 ContentLengthTest {

private static final int NUMBER_OF_COPIES = 500;

@RegisterExtension
static ResteasyReactiveUnitTest runner = new ResteasyReactiveUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(FileResource.class));

@Test
void testResponseHeaders() {
when()
.get("/file")
.then()
.statusCode(200)
.header(HttpHeaders.CONTENT_LENGTH,
greaterThan(
"" + (NUMBER_OF_COPIES * UUID.randomUUID().toString().getBytes(StandardCharsets.UTF_8).length)))
.header("transfer-encoding", nullValue());
}

@Path("/file")
public static class FileResource {

@GET
@Produces(MediaType.TEXT_PLAIN)
public Response hello() {
byte[] bytes = String.join(";", Collections.nCopies(NUMBER_OF_COPIES, UUID.randomUUID().toString()))
.getBytes(StandardCharsets.UTF_8);
return Response.ok(new ByteArrayInputStream(bytes), "text/plain")
.header(HttpHeaders.CONTENT_LENGTH, bytes.length)
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename = " + "uuid.txt")
.build();
}
}
}