Merge branch '2.1.x'

Closes gh-18027
This commit is contained in:
Andy Wilkinson
2019-08-31 08:26:05 +01:00
3 changed files with 33 additions and 6 deletions

View File

@@ -28,6 +28,7 @@ import reactor.netty.http.server.HttpServerRequest;
import reactor.netty.http.server.HttpServerResponse;
import org.springframework.boot.web.server.Compression;
import org.springframework.util.InvalidMimeTypeException;
import org.springframework.util.MimeType;
import org.springframework.util.MimeTypeUtils;
import org.springframework.util.ObjectUtils;
@@ -73,8 +74,13 @@ final class CompressionCustomizer implements NettyServerCustomizer {
if (StringUtils.isEmpty(contentType)) {
return false;
}
MimeType contentMimeType = MimeTypeUtils.parseMimeType(contentType);
return mimeTypes.stream().anyMatch((candidate) -> candidate.isCompatibleWith(contentMimeType));
try {
MimeType contentMimeType = MimeTypeUtils.parseMimeType(contentType);
return mimeTypes.stream().anyMatch((candidate) -> candidate.isCompatibleWith(contentMimeType));
}
catch (InvalidMimeTypeException ex) {
return false;
}
};
}

View File

@@ -16,6 +16,7 @@
package org.springframework.boot.web.embedded.netty;
import java.time.Duration;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
@@ -24,7 +25,10 @@ import reactor.netty.http.server.HttpServer;
import org.springframework.boot.web.reactive.server.AbstractReactiveWebServerFactory;
import org.springframework.boot.web.reactive.server.AbstractReactiveWebServerFactoryTests;
import org.springframework.boot.web.server.Compression;
import org.springframework.boot.web.server.PortInUseException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.reactive.function.client.WebClient;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -83,4 +87,15 @@ class NettyReactiveWebServerFactoryTests extends AbstractReactiveWebServerFactor
assertForwardHeaderIsUsed(factory);
}
@Test
void noCompressionForResponseWithInvalidContentType() {
Compression compression = new Compression();
compression.setEnabled(true);
compression.setMimeTypes(new String[] { "application/json" });
WebClient client = prepareCompressionTest(compression, "test~plain");
ResponseEntity<Void> response = client.get().exchange().flatMap((res) -> res.toEntity(Void.class))
.block(Duration.ofSeconds(30));
assertResponseIsNotCompressed(response);
}
}

View File

@@ -51,6 +51,7 @@ import org.springframework.boot.web.server.WebServer;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
@@ -268,6 +269,7 @@ public abstract class AbstractReactiveWebServerFactoryTests {
@Test
void noCompressionForMimeType() {
Compression compression = new Compression();
compression.setEnabled(true);
compression.setMimeTypes(new String[] { "application/json" });
WebClient client = prepareCompressionTest(compression);
ResponseEntity<Void> response = client.get().exchange().flatMap((res) -> res.toEntity(Void.class))
@@ -300,9 +302,13 @@ public abstract class AbstractReactiveWebServerFactoryTests {
}
protected WebClient prepareCompressionTest(Compression compression) {
return prepareCompressionTest(compression, MediaType.TEXT_PLAIN_VALUE);
}
protected WebClient prepareCompressionTest(Compression compression, String responseContentType) {
AbstractReactiveWebServerFactory factory = getFactory();
factory.setCompression(compression);
this.webServer = factory.getWebServer(new CharsHandler(3000, MediaType.TEXT_PLAIN));
this.webServer = factory.getWebServer(new CharsHandler(3000, responseContentType));
this.webServer.start();
HttpClient client = HttpClient.create().wiretap(true).compress(true)
@@ -363,9 +369,9 @@ public abstract class AbstractReactiveWebServerFactoryTests {
private final DataBuffer bytes;
private final MediaType mediaType;
private final String mediaType;
CharsHandler(int contentSize, MediaType mediaType) {
CharsHandler(int contentSize, String mediaType) {
char[] chars = new char[contentSize];
Arrays.fill(chars, 'F');
this.bytes = factory.wrap(new String(chars).getBytes(StandardCharsets.UTF_8));
@@ -375,7 +381,7 @@ public abstract class AbstractReactiveWebServerFactoryTests {
@Override
public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
response.setStatusCode(HttpStatus.OK);
response.getHeaders().setContentType(this.mediaType);
response.getHeaders().set(HttpHeaders.CONTENT_TYPE, this.mediaType);
response.getHeaders().setContentLength(this.bytes.readableByteCount());
return response.writeWith(Mono.just(this.bytes));
}