Commit 49bbcced authored by Brian Clozel's avatar Brian Clozel

Fix Undertow compression config with invalid Mime Types

Prior to this commit, the Undertow compression configuration provided by
Spring Boot would fail and throw an exception for invalid MIME Types
when trying to check them against the list of configured types for
compression.

This commit ensures that invalid MIME Types are ignored and that
compression is disabled for those.

Fixes gh-20955
parent 60f726a0
...@@ -32,6 +32,7 @@ import io.undertow.util.HttpString; ...@@ -32,6 +32,7 @@ import io.undertow.util.HttpString;
import org.springframework.boot.web.server.Compression; import org.springframework.boot.web.server.Compression;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.util.InvalidMimeTypeException;
import org.springframework.util.MimeType; import org.springframework.util.MimeType;
import org.springframework.util.MimeTypeUtils; import org.springframework.util.MimeTypeUtils;
...@@ -90,12 +91,18 @@ final class UndertowCompressionConfigurer { ...@@ -90,12 +91,18 @@ final class UndertowCompressionConfigurer {
public boolean resolve(HttpServerExchange value) { public boolean resolve(HttpServerExchange value) {
String contentType = value.getResponseHeaders().getFirst(HttpHeaders.CONTENT_TYPE); String contentType = value.getResponseHeaders().getFirst(HttpHeaders.CONTENT_TYPE);
if (contentType != null) { if (contentType != null) {
try {
MimeType parsed = MimeTypeUtils.parseMimeType(contentType);
for (MimeType mimeType : this.mimeTypes) { for (MimeType mimeType : this.mimeTypes) {
if (mimeType.isCompatibleWith(MimeTypeUtils.parseMimeType(contentType))) { if (mimeType.isCompatibleWith(parsed)) {
return true; return true;
} }
} }
} }
catch (InvalidMimeTypeException ex) {
return false;
}
}
return false; return false;
} }
......
...@@ -357,10 +357,12 @@ public abstract class AbstractReactiveWebServerFactoryTests { ...@@ -357,10 +357,12 @@ public abstract class AbstractReactiveWebServerFactoryTests {
} }
protected void assertResponseIsCompressed(ResponseEntity<Void> response) { protected void assertResponseIsCompressed(ResponseEntity<Void> response) {
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(response.getHeaders().getFirst("X-Test-Compressed")).isEqualTo("true"); assertThat(response.getHeaders().getFirst("X-Test-Compressed")).isEqualTo("true");
} }
protected void assertResponseIsNotCompressed(ResponseEntity<Void> response) { protected void assertResponseIsNotCompressed(ResponseEntity<Void> response) {
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(response.getHeaders().keySet()).doesNotContain("X-Test-Compressed"); assertThat(response.getHeaders().keySet()).doesNotContain("X-Test-Compressed");
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment