CachingHttpContentFactory#getContent may return null if httpContent size is not set (original) (raw)
Jetty version(s)
12.1.6
Jetty Environment
ee11
HTTP version
1.1
Java version/vendor (use: java -version)
openjdk version "21.0.3" 2024-04-16 LTS
OpenJDK Runtime Environment Temurin-21.0.3+9 (build 21.0.3+9-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.3+9 (build 21.0.3+9-LTS, mixed mode, sharing)
OS type/version
Ubuntu 24.04.4 LTS
Description
In CachingHttpContentFactory#getContent, when HttpContent size is set to -1, isCacheable may return true and lead to a case where getContent() return null instead of the httpContent.
How to reproduce?
import org.eclipse.jetty.http.content.CachingHttpContentFactory;
import org.eclipse.jetty.http.content.HttpContent;
import org.eclipse.jetty.http.content.ResourceHttpContent;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.util.resource.MemoryResource;
import java.io.IOException;
import java.net.URI;
public class Test {
private static final String CONTENT_TYPE = "text/html; charset=utf-8";
private static final URI AN_URI = URI.create("file://test");
public static void main(String[] args) throws IOException {
ResourceHttpContent content = new ResourceHttpContent(new MemoryResource(AN_URI, new byte[0]), CONTENT_TYPE);
HttpContent.Factory delegate = path -> new UnknownLengthHttpContent(content);
CachingHttpContentFactory cache = new CachingHttpContentFactory(delegate, ByteBufferPool.SIZED_NON_POOLING);
HttpContent httpContent = cache.getContent("/something");
System.out.println("httpContent is " + httpContent);
}
private static class UnknownLengthHttpContent extends HttpContent.Wrapper {
public UnknownLengthHttpContent(HttpContent content) {
super(content);
}
@Override
public long getContentLengthValue() {
return -1;
}
}
}