我只是构建了一个小的 PoC,我在其中使用带有 http/2 的 Jetty 9.3.9.M1 嵌入式服务器和 PushBuilder API 以及 Apache Wicket 将资源推送到客户端。
我使用以下服务器设置:
Server server = new Server();
// HTTP Configuration
HttpConfiguration http_config = new HttpConfiguration();
http_config.setSecureScheme("https");
http_config.setSecurePort(8443);
http_config.setSendXPoweredBy(true);
http_config.setSendServerVersion(true);
// keytool -keystore keystore -alias jetty -genkey -keyalg RSA
SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setKeyStorePath(new File(".","keystore").getCanonicalPath());
sslContextFactory.setKeyStorePassword("123456789");
sslContextFactory.setKeyManagerPassword("123456789");
sslContextFactory.setCipherComparator(HTTP2Cipher.COMPARATOR);
sslContextFactory.setUseCipherSuitesOrder(true);
// HTTPS Configuration
HttpConfiguration https_config = new HttpConfiguration(http_config);
https_config.addCustomizer(new SecureRequestCustomizer());
// HTTP Connector
ServerConnector http1 = new ServerConnector(server, new HttpConnectionFactory(http_config),
new HTTP2CServerConnectionFactory(http_config));
http1.setPort(8080);
server.addConnector(http1);
// HTTP/2 Connection Factory
HTTP2ServerConnectionFactory http2 = new HTTP2ServerConnectionFactory(https_config);
NegotiatingServerConnectionFactory.checkProtocolNegotiationAvailable();
ALPNServerConnectionFactory alpn = new ALPNServerConnectionFactory();
alpn.setDefaultProtocol(http1.getDefaultProtocol());
// SSL Connection Factory
SslConnectionFactory ssl = new SslConnectionFactory(sslContextFactory, alpn.getProtocol());
// HTTP/2 Connector
ServerConnector http2Connector = new ServerConnector(server, ssl, alpn, http2,
new HttpConnectionFactory(https_config));
http2Connector.setPort(8443);
server.addConnector(http2Connector);
WebAppContext webAppContext = new WebAppContext();
webAppContext.setServer(server);
webAppContext.setContextPath("/");
webAppContext.setWar("src/main/webapp");
server.setHandler(webAppContext);
ContextHandlerCollection contexts = new ContextHandlerCollection();
contexts.addHandler(webAppContext);
server.setHandler(contexts);
ALPN.debug = false;
server.start();
server.join();
现在我面临的问题是,当我向这样的资源发出 http/1.1 请求时:
第二次请求后chrome调试器显示状态码304
如果我对同一资源发出 http/2.0 请求:
每个请求都显示状态码 200 - 客户端似乎没有缓存它。
这是 git 项目的链接:https://github.com/klopfdreh/jetty-http2-example
提前致以诚挚的问候和感谢。
最佳答案
这个问题已经在github上作为一个issue进行了讨论和解决。
看这里:
https://github.com/eclipse/jetty.project/issues/801
获取相关答案。
总结:
问题是对 PushBuilder API 的误解。如果向索引页面发出请求,则有关此页面的缓存信息将用于确定是否要推送更多资源。大多数情况下,标题项“If-Modified-Since”用于检测是否需要进一步的推送操作。
因此应用程序必须提供有关缓存的逻辑。
例如,最简单的实现是查看索引页面请求的“If-Modified-Since” header 是否在索引页面文件本身的最后修改日期之前。
关于java - Jetty 在使用 http2 时以状态 200 而不是 304 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37211883/