简体   繁体   中英

Jetty respond with status 200 instead of 304 while using http2

I just build a little PoC in which I use the Jetty 9.3.9.M1 embedded server with http/2 and the PushBuilder API together with Apache Wicket to push resources to the client.

I use the following server setup:

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();

Now I am facing the issue that when I do a http/1.1 request to a resource like this:

http://127.0.0.1:8080/wicket/resource/de.jetty.wicket.http2.example.resources.TestResourceReference/TestResourceReference-ver-1463040221000.css

the status code 304 is shown in the chrome debugger after the second request

If I do a http/2.0 request to the same resource:

https://127.0.0.1:8443/wicket/resource/de.jetty.wicket.http2.example.resources.TestResourceReference/TestResourceReference-ver-1463040221000.css

the status code 200 is shown on each request - it seems that the client does not cache it.

Here is the link to the git project: https://github.com/klopfdreh/jetty-http2-example

kind regards and thanks a lot in advance.

This question has been discussed and solved as an issue on github.

Have a look here:

https://github.com/eclipse/jetty.project/issues/801

To get the related answeres.

Summary:

The issue was a misunderstanding about the PushBuilder API. If a request is made to the index page the cache information about this page are going to be used to determine if further resources are going to be pushed. Mostly the header item "If-Modified-Since" is used to detect if further push operations are required.

So the application has to provide the logic about caching.

Easiest implementation is to look if the "If-Modified-Since" header of the request of the index page is before the last modified date of the index page file itself for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM