-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HTTP response compression - fix Undertow and RESTEasy Classsic support
- honor the quarkus.http.compress-media-types in Undertow Servlet and RESTEasy Classsic extensions - fixes #31415 and #26112
- Loading branch information
Showing
10 changed files
with
187 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...ployment/src/test/java/io/quarkus/undertow/test/compress/CompressionDisabledTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.quarkus.undertow.test.compress; | ||
|
||
import static io.restassured.RestAssured.get; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.response.ExtractableResponse; | ||
import io.restassured.response.Response; | ||
|
||
public class CompressionDisabledTestCase { | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> root | ||
.addClasses(SimpleServlet.class)) | ||
.overrideConfigKey("quarkus.http.enable-compression", "false"); | ||
|
||
@Test | ||
public void testCompressed() throws Exception { | ||
ExtractableResponse<Response> response = get(SimpleServlet.SERVLET_ENDPOINT) | ||
.then().statusCode(200).extract(); | ||
assertTrue(response.header("Content-Encoding") == null, response.headers().toString()); | ||
assertEquals("ok", response.asString()); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...eployment/src/test/java/io/quarkus/undertow/test/compress/CompressionEnabledTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.quarkus.undertow.test.compress; | ||
|
||
import static io.restassured.RestAssured.get; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class CompressionEnabledTestCase { | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> root | ||
.addClasses(SimpleServlet.class)) | ||
.overrideConfigKey("quarkus.http.enable-compression", "true"); | ||
|
||
@Test | ||
public void testCompressed() throws Exception { | ||
String bodyStr = get(SimpleServlet.SERVLET_ENDPOINT).then().statusCode(200).header("Content-Encoding", "gzip").extract() | ||
.asString(); | ||
assertEquals("ok", bodyStr); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...ns/undertow/deployment/src/test/java/io/quarkus/undertow/test/compress/SimpleServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.quarkus.undertow.test.compress; | ||
|
||
import java.io.IOException; | ||
|
||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.annotation.WebServlet; | ||
import jakarta.servlet.http.HttpServlet; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
@WebServlet(urlPatterns = SimpleServlet.SERVLET_ENDPOINT) | ||
public class SimpleServlet extends HttpServlet { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
public static final String SERVLET_ENDPOINT = "/simple"; | ||
|
||
@Override | ||
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { | ||
// this one must be listed in the quarkus.http.compress-media-types | ||
resp.setHeader("Content-type", "text/plain"); | ||
resp.getWriter().write("ok"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...ertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/HttpCompressionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package io.quarkus.vertx.http.runtime; | ||
|
||
import java.util.Set; | ||
|
||
import io.vertx.core.AsyncResult; | ||
import io.vertx.core.Handler; | ||
import io.vertx.core.MultiMap; | ||
import io.vertx.core.http.HttpHeaders; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
/** | ||
* A simple wrapping handler that removes the {@code Content-Encoding: identity} HTTP header if the {@code Content-Type} | ||
* header is set and the value is a compressed media type as configured via | ||
* {@link io.quarkus.vertx.http.runtime.HttpBuildTimeConfig#compressMediaTypes}. | ||
*/ | ||
public class HttpCompressionHandler implements Handler<RoutingContext> { | ||
|
||
private final Handler<RoutingContext> routeHandler; | ||
private final Set<String> compressedMediaTypes; | ||
|
||
public HttpCompressionHandler(Handler<RoutingContext> routeHandler, Set<String> compressedMediaTypes) { | ||
this.routeHandler = routeHandler; | ||
this.compressedMediaTypes = compressedMediaTypes; | ||
} | ||
|
||
@Override | ||
public void handle(RoutingContext context) { | ||
context.addEndHandler(new Handler<AsyncResult<Void>>() { | ||
@Override | ||
public void handle(AsyncResult<Void> result) { | ||
if (result.succeeded()) { | ||
compressIfNeeded(context, compressedMediaTypes); | ||
} | ||
} | ||
}); | ||
routeHandler.handle(context); | ||
} | ||
|
||
public static void compressIfNeeded(RoutingContext context, Set<String> compressedMediaTypes) { | ||
MultiMap headers = context.response().headers(); | ||
// "Content-Encoding: identity" header means that compression is enabled in the config | ||
// and this header is set to disable the compression by default | ||
String contentEncoding = headers.get(HttpHeaders.CONTENT_ENCODING); | ||
if (contentEncoding != null && HttpHeaders.IDENTITY.toString().equals(contentEncoding)) { | ||
// Just remove the header if the compression should be enabled for the current HTTP response | ||
String contentType = headers.get(HttpHeaders.CONTENT_TYPE); | ||
if (contentType != null) { | ||
int paramIndex = contentType.indexOf(';'); | ||
if (paramIndex > -1) { | ||
contentType = contentType.substring(0, paramIndex); | ||
} | ||
if (compressedMediaTypes.contains(contentType)) { | ||
headers.remove(HttpHeaders.CONTENT_ENCODING); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |