Skip to content

Commit

Permalink
Fix location and content location headers in Resteasy Reactive
Browse files Browse the repository at this point in the history
When providing a location, the URI was being decoded, so the value was being altered from what users set. 

Note that these changes are based on what Resteasy already does: https://github.com/resteasy/resteasy/blob/dadddfb699a875c44ba05c0abe176873acbd9aa2/resteasy-core/src/main/java/org/jboss/resteasy/specimpl/ResponseBuilderImpl.java#L187

Fix quarkusio#33419
  • Loading branch information
Sgitario committed May 17, 2023
1 parent aad9ce5 commit ced8b0a
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ public Response.ResponseBuilder location(URI location) {
prefix = deployment.getPrefix();
}
// Spec says relative to request, but TCK tests relative to Base URI, so we do that
location = new URI(req.getRequestScheme(), null, host, port,
prefix +
(location.getPath().startsWith("/") ? location.getPath() : "/" + location.getPath()),
location.getQuery(), null);
String path = location.toString();
if (!path.startsWith("/")) {
path = "/" + path;
}
URI baseUri = new URI(req.getRequestScheme(), null, host, port, null, null, null);
location = baseUri.resolve(prefix + path);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -72,9 +74,12 @@ public Response.ResponseBuilder contentLocation(URI location) {
port = Integer.parseInt(host.substring(index + 1));
host = host.substring(0, index);
}
location = new URI(req.getRequestScheme(), null, host, port,
location.getPath().startsWith("/") ? location.getPath() : "/" + location.getPath(),
location.getQuery(), null);
String path = location.toString();
if (!path.startsWith("/")) {
path = "/" + path;
}
location = new URI(req.getRequestScheme(), null, host, port, null, null, null)
.resolve(path);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ public RestResponse.ResponseBuilder<T> location(URI location) {
prefix = deployment.getPrefix();
}
// Spec says relative to request, but TCK tests relative to Base URI, so we do that
location = new URI(req.getRequestScheme(), null, host, port,
prefix +
(location.getPath().startsWith("/") ? location.getPath() : "/" + location.getPath()),
location.getQuery(), null);
String path = location.toString();
if (!path.startsWith("/")) {
path = "/" + path;
}
URI baseUri = new URI(req.getRequestScheme(), null, host, port, null, null, null);
location = baseUri.resolve(prefix + path);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -72,9 +74,12 @@ public RestResponse.ResponseBuilder<T> contentLocation(URI location) {
port = Integer.parseInt(host.substring(index + 1));
host = host.substring(0, index);
}
location = new URI(req.getRequestScheme(), null, host, port,
location.getPath().startsWith("/") ? location.getPath() : "/" + location.getPath(),
location.getQuery(), null);
String path = location.toString();
if (!path.startsWith("/")) {
path = "/" + path;
}
location = new URI(req.getRequestScheme(), null, host, port, null, null, null)
.resolve(path);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriBuilder;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand All @@ -16,4 +17,20 @@ public void testCaseInsensitivity() {
Assertions.assertEquals("HEAD", response.getHeaders().getFirst("allow"));
Assertions.assertEquals("HEAD", response.getHeaders().getFirst(HttpHeaders.ALLOW));
}

@Test
public void testLocation() {
final var location = UriBuilder.fromUri("http://localhost:8080").path("{language}")
.build("en/us");
Response response = Response.ok("Hello").location(location).build();
Assertions.assertEquals("http://localhost:8080/en%2Fus", response.getLocation().toString());
}

@Test
public void testContentLocation() {
final var location = UriBuilder.fromUri("http://localhost:8080").path("{language}")
.build("en/us");
Response response = Response.ok("Hello").contentLocation(location).build();
Assertions.assertEquals("http://localhost:8080/en%2Fus", response.getHeaderString("Content-Location"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import jakarta.ws.rs.core.CacheControl;
import jakarta.ws.rs.core.NewCookie;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriBuilder;
import jakarta.ws.rs.core.Variant;

import org.jboss.resteasy.reactive.RestResponse;
Expand Down Expand Up @@ -47,6 +48,24 @@ public RestResponse<?> wildcard() {
return RestResponse.ResponseBuilder.ok("Hello").header("content-type", "text/plain").build();
}

@GET
@Path("rest-response-location")
public RestResponse<?> location() {
final var location = UriBuilder.fromResource(RestResponseResource.class).path("{language}")
.queryParam("user", "John")
.build("en/us");
return RestResponse.ResponseBuilder.ok("Hello").location(location).build();
}

@GET
@Path("rest-response-content-location")
public RestResponse<?> contentLocation() {
final var location = UriBuilder.fromResource(RestResponseResource.class).path("{language}")
.queryParam("user", "John")
.build("en/us");
return RestResponse.ResponseBuilder.ok("Hello").contentLocation(location).build();
}

@GET
@Path("rest-response-full")
@SuppressWarnings("deprecation")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.jboss.resteasy.reactive.server.vertx.test.response;

import static org.hamcrest.CoreMatchers.endsWith;

import java.util.function.Supplier;

import org.hamcrest.Matchers;
Expand Down Expand Up @@ -107,5 +109,11 @@ public void test() {
.then().statusCode(200)
.and().body(Matchers.equalTo("Uni<RestResponse> request filter"))
.and().contentType("text/plain");
RestAssured.get("/rest-response-location")
.then().statusCode(200)
.header("Location", endsWith("/en%2Fus?user=John"));
RestAssured.get("/rest-response-content-location")
.then().statusCode(200)
.header("Content-Location", endsWith("/en%2Fus?user=John"));
}
}

0 comments on commit ced8b0a

Please sign in to comment.