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 use headers set in Vert.x from RESTEasy Reactive #25323

Merged
merged 1 commit into from
May 18, 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 @@ -99,6 +99,11 @@
<artifactId>quarkus-jaxrs-client-reactive-deployment</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-reactive-routes-deployment</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.quarkus.resteasy.reactive.server.test.headers;

import static io.restassured.RestAssured.when;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.ext.Provider;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.vertx.web.RouteFilter;
import io.vertx.ext.web.RoutingContext;

public class VertxHeadersTest {

@RegisterExtension
static QuarkusUnitTest TEST = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar.addClasses(VertxFilter.class, JaxRsFilter.class, TestResource.class));

@Test
void testVaryHeaderValues() {
var headers = when().get("/test")
.then()
.statusCode(200)
.extract().headers();
assertThat(headers.getValues(HttpHeaders.VARY)).containsExactlyInAnyOrder("Origin", "Prefer");
}

public static class VertxFilter {
@RouteFilter
void addVary(final RoutingContext rc) {
rc.response().headers().add(HttpHeaders.VARY, "Origin");
rc.next();
}
}

@Provider
public static class JaxRsFilter implements ContainerResponseFilter {
@Override
public void filter(final ContainerRequestContext requestContext, final ContainerResponseContext responseContext)
throws IOException {
responseContext.getHeaders().add(HttpHeaders.VARY, "Prefer");
}
}

@Path("test")
public static class TestResource {

@GET
public String test() {
return "test";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -71,7 +72,13 @@ public void accept(ResteasyReactiveRequestContext context) {
}
};

private static final String CONTENT_TYPE = "Content-Type"; // use this instead of the Vert.x constant because the TCK expects upper case
private static final String CONTENT = "Content";
private static final String CONTENT_LOWER = "content";
private static final String TYPE = "Type";
private static final String TYPE_LOWER = "type";
private static final String LENGTH = "Length";
private static final String LENGTH_LOWER = "length";
private static final String CONTENT_TYPE = CONTENT + "-" + TYPE; // use this instead of the Vert.x constant because the TCK expects upper case

static {
primitivesToWrappers.put(boolean.class, Boolean.class);
Expand Down Expand Up @@ -484,13 +491,27 @@ public static void encodeResponseHeaders(ResteasyReactiveRequestContext requestC
MultivaluedMap<String, Object> headers = response.getHeaders();
for (Map.Entry<String, List<Object>> entry : headers.entrySet()) {
if (entry.getValue().size() == 1) {
String header = entry.getKey();
boolean useSet = requireSingleHeader(header);
Object o = entry.getValue().get(0);
if (o == null) {
vertxResponse.setResponseHeader(entry.getKey(), "");
if (useSet) {
vertxResponse.setResponseHeader(header, "");
} else {
vertxResponse.addResponseHeader(header, "");
}
} else if (o instanceof CharSequence) {
vertxResponse.setResponseHeader(entry.getKey(), (CharSequence) o);
if (useSet) {
vertxResponse.setResponseHeader(header, (CharSequence) o);
} else {
vertxResponse.addResponseHeader(header, (CharSequence) o);
}
} else {
vertxResponse.setResponseHeader(entry.getKey(), (CharSequence) HeaderUtil.headerToString(o));
if (useSet) {
vertxResponse.setResponseHeader(header, (CharSequence) HeaderUtil.headerToString(o));
} else {
vertxResponse.addResponseHeader(header, (CharSequence) HeaderUtil.headerToString(o));
}
}
} else {
List<CharSequence> strValues = new ArrayList<>(entry.getValue().size());
Expand All @@ -502,4 +523,15 @@ public static void encodeResponseHeaders(ResteasyReactiveRequestContext requestC
}
}

private static boolean requireSingleHeader(String header) {
if (!(header.startsWith(CONTENT) || header.startsWith(CONTENT_LOWER))) {
return false;
}
if (header.length() < CONTENT.length() + 2) {
return false;
}
String substring = header.substring(CONTENT.length() + 1).toLowerCase(Locale.ROOT);
return substring.equals(TYPE_LOWER) || substring.equals(LENGTH_LOWER);
}
gastaldi marked this conversation as resolved.
Show resolved Hide resolved

}