2016-05-13 89 views
2

我只是建立一個小的PoC,我使用帶有http/2的Jetty 9.3.9.M1嵌入式服務器和PushBuilder API與Apache Wicket一起將資源推送到客戶。Jetty響應狀態200而不是304,而使用http2

我用下面的服務器設置:

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請求的資源是這樣的:

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

狀態代碼304在第二個請求後顯示在chrome調試器中

如果我對同一個資源執行http/2.0請求:

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

狀態碼200顯示在每個請求 - 這似乎是客戶端不緩存它。

這裏是鏈接到Git項目:提前https://github.com/klopfdreh/jetty-http2-example

親切的問候和感謝。

+0

試用HTTPS和1.1。 –

+0

當我用http/1.1使用HTTPS時,它也像預期的那樣工作。在第二次請求之後,資源被緩存,並且304被打印爲狀態碼。我只改變了SslConnectionFactory的構造函數,並在http1.getDefaultProtocol中作爲第二個參數。 – klopfdreh

+0

你是在推'css'還是直接使用瀏覽器請求它? – sbordet

回答

1

這個問題已經在github上討論和解決。

看一看這裏:

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

要獲得相關answeres。

總結:

這個問題是對PushBuilder API的一個誤解。如果向索引頁面發出請求,則將使用關於此頁面的緩存信息來確定是否將推送更多資源。大部分標題項「If-Modified-Since」用於檢測是否需要進一步的推送操作。

所以應用程序必須提供關於緩存的邏輯。

最簡單的實現方式是查看索引頁請求的「If-Modified-Since」標頭是否在索引頁文件本身的最後修改日期之前。

相關問題