Skip to content

Commit

Permalink
jetty#7880 PrecompressedFormats set on ResourceService are used in De…
Browse files Browse the repository at this point in the history
…faultServlet unless overridden by an InitParameter.
  • Loading branch information
markslater committed Apr 14, 2022
1 parent 0c6489e commit 8e51112
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.StringTokenizer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
Expand Down Expand Up @@ -170,7 +171,7 @@ public void init()
_resourceService.setAcceptRanges(getInitBoolean("acceptRanges", _resourceService.isAcceptRanges()));
_resourceService.setDirAllowed(getInitBoolean("dirAllowed", _resourceService.isDirAllowed()));
_resourceService.setRedirectWelcome(getInitBoolean("redirectWelcome", _resourceService.isRedirectWelcome()));
_resourceService.setPrecompressedFormats(parsePrecompressedFormats(getInitParameter("precompressed"), getInitBoolean("gzip", false)));
_resourceService.setPrecompressedFormats(parsePrecompressedFormats(getInitParameter("precompressed"), getInitBoolean("gzip"), _resourceService.getPrecompressedFormats()));
_resourceService.setPathInfoOnly(getInitBoolean("pathInfoOnly", _resourceService.isPathInfoOnly()));
_resourceService.setEtags(getInitBoolean("etags", _resourceService.isEtags()));

Expand Down Expand Up @@ -303,8 +304,11 @@ public void init()
LOG.debug("resource base = {}", _resourceBase);
}

private CompressedContentFormat[] parsePrecompressedFormats(String precompressed, boolean gzip)
private CompressedContentFormat[] parsePrecompressedFormats(String precompressed, Boolean gzip, CompressedContentFormat[] dft)
{
if (precompressed == null && gzip == null) {
return dft;
}
List<CompressedContentFormat> ret = new ArrayList<>();
if (precompressed != null && precompressed.indexOf('=') > 0)
{
Expand Down Expand Up @@ -367,18 +371,23 @@ public String getInitParameter(String name)
return value;
}

private boolean getInitBoolean(String name, boolean dft)
private Boolean getInitBoolean(String name)
{
String value = getInitParameter(name);
if (value == null || value.length() == 0)
return dft;
return null;
return (value.startsWith("t") ||
value.startsWith("T") ||
value.startsWith("y") ||
value.startsWith("Y") ||
value.startsWith("1"));
}

private boolean getInitBoolean(String name, boolean dft)
{
return Optional.ofNullable(getInitBoolean(name)).orElse(dft);
}

private int getInitInt(String name, int dft)
{
String value = getInitParameter(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.http.DateGenerator;
import org.eclipse.jetty.http.HttpContent;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.HttpTester;
import org.eclipse.jetty.http.*;
import org.eclipse.jetty.logging.StacklessLogging;
import org.eclipse.jetty.server.AllowedResourceAliasChecker;
import org.eclipse.jetty.server.HttpConfiguration;
Expand Down Expand Up @@ -1968,6 +1963,51 @@ public void testCustomCompressionFormats() throws Exception
assertThat(body, containsString("fake gzip"));
}

@Test
public void testProgrammaticCustomCompressionFormats() throws Exception
{
createFile(docRoot.resolve("data0.txt"), "Hello Text 0");
createFile(docRoot.resolve("data0.txt.br"), "fake brotli");
createFile(docRoot.resolve("data0.txt.gz"), "fake gzip");
createFile(docRoot.resolve("data0.txt.bz2"), "fake bzip2");

ResourceService resourceService = new ResourceService();
resourceService.setPrecompressedFormats(new CompressedContentFormat[]{
new CompressedContentFormat("bzip2", ".bz2"),
new CompressedContentFormat("gzip", ".gz"),
new CompressedContentFormat("br", ".br")
});
ServletHolder defholder = new ServletHolder(new DefaultServlet(resourceService));
context.addServlet(defholder, "/");
defholder.setInitParameter("resourceBase", docRoot.toString());

String rawResponse;
HttpTester.Response response;
String body;

rawResponse = connector.getResponse("GET /context/data0.txt HTTP/1.0\r\nHost:localhost:8080\r\nAccept-Encoding:bzip2, br, gzip\r\n\r\n");
response = HttpTester.parseResponse(rawResponse);
assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200));
assertThat(response, containsHeaderValue(HttpHeader.CONTENT_LENGTH, "10"));
assertThat(response, containsHeaderValue(HttpHeader.CONTENT_TYPE, "text/plain"));
assertThat(response, containsHeaderValue(HttpHeader.VARY, "Accept-Encoding"));
assertThat(response, containsHeaderValue(HttpHeader.CONTENT_ENCODING, "bzip2"));
body = response.getContent();
assertThat(body, containsString("fake bzip2"));

// TODO: show accept-encoding search order issue (shouldn't this request return data0.txt.br?)

rawResponse = connector.getResponse("GET /context/data0.txt HTTP/1.0\r\nHost:localhost:8080\r\nAccept-Encoding:br, gzip\r\n\r\n");
response = HttpTester.parseResponse(rawResponse);
assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200));
assertThat(response, containsHeaderValue(HttpHeader.CONTENT_LENGTH, "9"));
assertThat(response, containsHeaderValue(HttpHeader.CONTENT_TYPE, "text/plain"));
assertThat(response, containsHeaderValue(HttpHeader.VARY, "Accept-Encoding"));
assertThat(response, containsHeaderValue(HttpHeader.CONTENT_ENCODING, "gzip"));
body = response.getContent();
assertThat(body, containsString("fake gzip"));
}

@Test
public void testControlCharacter() throws Exception
{
Expand Down

0 comments on commit 8e51112

Please sign in to comment.