Skip to content

Commit

Permalink
Issue #11925 - Fix NPE in EtagUtils with URLResource
Browse files Browse the repository at this point in the history
  • Loading branch information
joakime committed Jun 19, 2024
1 parent 037ae7b commit adb9f11
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,9 @@ public static HttpField createWeakEtagField(Resource resource)
*/
public static HttpField createWeakEtagField(Resource resource, String etagSuffix)
{
Path path = resource.getPath();
if (path == null)
return null;

String etagValue = EtagUtils.computeWeakEtag(path, etagSuffix);
if (etagValue != null)
return new PreEncodedHttpField(HttpHeader.ETAG, etagValue);
String etag = EtagUtils.computeWeakEtag(resource, etagSuffix);
if (etag != null)
return new PreEncodedHttpField(HttpHeader.ETAG, etag);
return null;
}

Expand Down Expand Up @@ -213,9 +209,9 @@ private static String computeWeakEtag(String name, Instant lastModified, long si
*/
public static String rewriteWithSuffix(String etag, String newSuffix)
{
// if (etag == null)
// return null;
//
if (etag == null)
return null;

StringBuilder ret = new StringBuilder();
boolean weak = etag.startsWith("W/");
int start = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import org.eclipse.jetty.util.resource.FileSystemPool;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.resource.ResourceFactory;
import org.eclipse.jetty.util.resource.URLResourceFactory;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -2568,13 +2569,23 @@ public void testGetUtf8NfdFile() throws Exception
@Test
public void testGetPrecompressedSuffixMapping() throws Exception
{
FS.ensureEmpty(docRoot);
Path docRoot = workDir.getEmptyPathDir().resolve("docroot");
FS.ensureDirExists(docRoot);

startServer((context) ->
{
ResourceFactory.registerResourceFactory("file", new URLResourceFactory());
Resource resource = ResourceFactory.of(context).newResource(docRoot);
assertThat("Expecting URLResource", resource.getClass().getName(), endsWith("URLResource"));
context.setBaseResource(resource);

ServletHolder defholder = context.addServlet(DefaultServlet.class, "*.js");
defholder.setInitParameter("cacheControl", "no-store");
defholder.setInitParameter("dirAllowed", "false");
defholder.setInitParameter("gzip", "false");
defholder.setInitParameter("precompressed", "gzip=.gz");
});

ServletHolder defholder = context.addServlet(DefaultServlet.class, "*.js");
defholder.setInitParameter("cacheControl", "no-store");
defholder.setInitParameter("dirAllowed", "false");
defholder.setInitParameter("gzip", "false");
defholder.setInitParameter("precompressed", "gzip=.gz");

FS.ensureDirExists(docRoot.resolve("scripts"));

Expand Down

0 comments on commit adb9f11

Please sign in to comment.