Skip to content

Commit bc46039

Browse files
committed
Configure Jetty to compress responses to requests other than GET
Closes gh-8184
1 parent 6df1be3 commit bc46039

File tree

3 files changed

+36
-49
lines changed

3 files changed

+36
-49
lines changed

spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyServletWebServerFactory.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import javax.servlet.http.HttpServletRequest;
3333
import javax.servlet.http.HttpServletResponse;
3434

35+
import org.eclipse.jetty.http.HttpMethod;
3536
import org.eclipse.jetty.http.HttpVersion;
3637
import org.eclipse.jetty.http.MimeTypes;
3738
import org.eclipse.jetty.server.AbstractConnector;
@@ -229,6 +230,9 @@ private HandlerWrapper createGzipHandler() {
229230
Compression compression = getCompression();
230231
handler.setMinGzipSize(compression.getMinResponseSize());
231232
handler.setIncludedMimeTypes(compression.getMimeTypes());
233+
for (HttpMethod httpMethod : HttpMethod.values()) {
234+
handler.addIncludedMethods(httpMethod.name());
235+
}
232236
if (compression.getExcludedUserAgents() != null) {
233237
handler.setExcludedAgentPatterns(compression.getExcludedUserAgents());
234238
}
@@ -581,8 +585,8 @@ public void setServerCustomizers(
581585
}
582586

583587
/**
584-
* Returns a mutable collection of Jetty {@link JettyServerCustomizer}s that will be applied
585-
* to the {@link Server} before the it is created.
588+
* Returns a mutable collection of Jetty {@link JettyServerCustomizer}s that will be
589+
* applied to the {@link Server} before the it is created.
586590
* @return the {@link JettyServerCustomizer}s
587591
*/
588592
public Collection<JettyServerCustomizer> getServerCustomizers() {

spring-boot/src/test/java/org/springframework/boot/web/embedded/jetty/JettyServletWebServerFactoryTests.java

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,12 @@
1616

1717
package org.springframework.boot.web.embedded.jetty;
1818

19-
import java.io.IOException;
2019
import java.nio.charset.Charset;
2120
import java.util.Arrays;
2221
import java.util.Locale;
2322
import java.util.Map;
2423
import java.util.concurrent.TimeUnit;
2524

26-
import javax.servlet.ServletException;
27-
import javax.servlet.http.HttpServlet;
28-
import javax.servlet.http.HttpServletRequest;
29-
import javax.servlet.http.HttpServletResponse;
30-
3125
import org.apache.jasper.servlet.JspServlet;
3226
import org.eclipse.jetty.server.Handler;
3327
import org.eclipse.jetty.server.Server;
@@ -43,13 +37,10 @@
4337
import org.junit.Test;
4438
import org.mockito.InOrder;
4539

46-
import org.springframework.boot.web.server.Compression;
4740
import org.springframework.boot.web.server.PortInUseException;
4841
import org.springframework.boot.web.server.Ssl;
49-
import org.springframework.boot.web.servlet.ServletRegistrationBean;
5042
import org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory;
5143
import org.springframework.boot.web.servlet.server.AbstractServletWebServerFactoryTests;
52-
import org.springframework.http.HttpHeaders;
5344

5445
import static org.assertj.core.api.Assertions.assertThat;
5546
import static org.hamcrest.CoreMatchers.isA;
@@ -296,38 +287,6 @@ public void customize(Server server) {
296287
factory.getWebServer().start();
297288
}
298289

299-
@Override
300-
@SuppressWarnings("serial")
301-
// Workaround for Jetty issue - https://bugs.eclipse.org/bugs/show_bug.cgi?id=470646
302-
protected String setUpFactoryForCompression(final int contentSize, String[] mimeTypes,
303-
String[] excludedUserAgents) throws Exception {
304-
char[] chars = new char[contentSize];
305-
Arrays.fill(chars, 'F');
306-
final String testContent = new String(chars);
307-
AbstractServletWebServerFactory factory = getFactory();
308-
Compression compression = new Compression();
309-
compression.setEnabled(true);
310-
if (mimeTypes != null) {
311-
compression.setMimeTypes(mimeTypes);
312-
}
313-
if (excludedUserAgents != null) {
314-
compression.setExcludedUserAgents(excludedUserAgents);
315-
}
316-
factory.setCompression(compression);
317-
this.webServer = factory
318-
.getWebServer(new ServletRegistrationBean<HttpServlet>(new HttpServlet() {
319-
@Override
320-
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
321-
throws ServletException, IOException {
322-
resp.setContentLength(contentSize);
323-
resp.setHeader(HttpHeaders.CONTENT_TYPE, "text/plain");
324-
resp.getWriter().print(testContent);
325-
}
326-
}, "/test.txt"));
327-
this.webServer.start();
328-
return testContent;
329-
}
330-
331290
@Override
332291
protected JspServlet getJspServlet() throws Exception {
333292
WebAppContext context = (WebAppContext) ((JettyWebServer) this.webServer)

spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@
5858
import javax.servlet.ServletException;
5959
import javax.servlet.ServletRequest;
6060
import javax.servlet.ServletResponse;
61+
import javax.servlet.http.HttpServlet;
6162
import javax.servlet.http.HttpServletRequest;
63+
import javax.servlet.http.HttpServletResponse;
6264
import javax.servlet.http.HttpSession;
6365

6466
import org.apache.http.client.HttpClient;
@@ -775,10 +777,15 @@ public void getValidSessionStoreWhenSessionStoreReferencesFile() throws Exceptio
775777
}
776778

777779
@Test
778-
public void compression() throws Exception {
780+
public void compressionOfResposeToGetRequest() throws Exception {
779781
assertThat(doTestCompression(10000, null, null)).isTrue();
780782
}
781783

784+
@Test
785+
public void compressionOfResposeToPostRequest() throws Exception {
786+
assertThat(doTestCompression(10000, null, null, HttpMethod.POST)).isTrue();
787+
}
788+
782789
@Test
783790
public void noCompressionForSmallResponse() throws Exception {
784791
assertThat(doTestCompression(100, null, null)).isFalse();
@@ -991,28 +998,31 @@ protected abstract void handleExceptionCausedByBlockedPort(RuntimeException ex,
991998

992999
private boolean doTestCompression(int contentSize, String[] mimeTypes,
9931000
String[] excludedUserAgents) throws Exception {
1001+
return doTestCompression(contentSize, mimeTypes, excludedUserAgents,
1002+
HttpMethod.GET);
1003+
}
1004+
1005+
private boolean doTestCompression(int contentSize, String[] mimeTypes,
1006+
String[] excludedUserAgents, HttpMethod method) throws Exception {
9941007
String testContent = setUpFactoryForCompression(contentSize, mimeTypes,
9951008
excludedUserAgents);
9961009
TestGzipInputStreamFactory inputStreamFactory = new TestGzipInputStreamFactory();
9971010
Map<String, InputStreamFactory> contentDecoderMap = Collections
9981011
.singletonMap("gzip", (InputStreamFactory) inputStreamFactory);
999-
String response = getResponse(getLocalUrl("/test.txt"),
1012+
String response = getResponse(getLocalUrl("/test.txt"), method,
10001013
new HttpComponentsClientHttpRequestFactory(
10011014
HttpClientBuilder.create().setUserAgent("testUserAgent")
10021015
.setContentDecoderRegistry(contentDecoderMap).build()));
10031016
assertThat(response).isEqualTo(testContent);
10041017
return inputStreamFactory.wasCompressionUsed();
10051018
}
10061019

1007-
protected String setUpFactoryForCompression(int contentSize, String[] mimeTypes,
1020+
private String setUpFactoryForCompression(int contentSize, String[] mimeTypes,
10081021
String[] excludedUserAgents) throws Exception {
10091022
char[] chars = new char[contentSize];
10101023
Arrays.fill(chars, 'F');
10111024
String testContent = new String(chars);
10121025
AbstractServletWebServerFactory factory = getFactory();
1013-
FileCopyUtils.copy(testContent,
1014-
new FileWriter(this.temporaryFolder.newFile("test.txt")));
1015-
factory.setDocumentRoot(this.temporaryFolder.getRoot());
10161026
Compression compression = new Compression();
10171027
compression.setEnabled(true);
10181028
if (mimeTypes != null) {
@@ -1022,6 +1032,20 @@ protected String setUpFactoryForCompression(int contentSize, String[] mimeTypes,
10221032
compression.setExcludedUserAgents(excludedUserAgents);
10231033
}
10241034
factory.setCompression(compression);
1035+
factory.addInitializers(
1036+
new ServletRegistrationBean<HttpServlet>(new HttpServlet() {
1037+
1038+
@Override
1039+
protected void service(HttpServletRequest req,
1040+
HttpServletResponse resp)
1041+
throws ServletException, IOException {
1042+
resp.setContentType("text/plain");
1043+
resp.setContentLength(testContent.length());
1044+
resp.getWriter().write(testContent);
1045+
resp.getWriter().flush();
1046+
}
1047+
1048+
}, "/test.txt"));
10251049
this.webServer = factory.getWebServer();
10261050
this.webServer.start();
10271051
return testContent;

0 commit comments

Comments
 (0)