Author: markt Date: Fri Jun 5 08:30:33 2015 New Revision: 1683675 URL: http://svn.apache.org/r1683675 Log: Fix a (the?) root cause of current unit test failures for HTTP/2. Ensure that the client preface is fully processed (including sending the ACK for the settings) before sending any response related frames to the client.
Modified: tomcat/trunk/java/org/apache/coyote/http2/Http2Parser.java tomcat/trunk/java/org/apache/coyote/http2/Http2UpgradeHandler.java Modified: tomcat/trunk/java/org/apache/coyote/http2/Http2Parser.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/coyote/http2/Http2Parser.java?rev=1683675&r1=1683674&r2=1683675&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/coyote/http2/Http2Parser.java (original) +++ tomcat/trunk/java/org/apache/coyote/http2/Http2Parser.java Fri Jun 5 08:30:33 2015 @@ -20,6 +20,7 @@ import java.io.IOException; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; +import org.apache.coyote.ProtocolException; import org.apache.coyote.http2.HpackDecoder.HeaderEmitter; import org.apache.juli.logging.Log; import org.apache.juli.logging.LogFactory; @@ -43,7 +44,6 @@ class Http2Parser { private volatile int headersCurrentStream = -1; private volatile boolean headersEndStream = false; - private volatile boolean readPreface = false; private volatile int maxPayloadSize = ConnectionSettings.DEFAULT_MAX_FRAME_SIZE; @@ -421,39 +421,23 @@ class Http2Parser { /** * Read and validate the connection preface from input using blocking IO. - * - * @return <code>true</code> if a valid preface was read, otherwise false. */ - boolean readConnectionPreface() throws IOException { - if (readPreface) { - return true; - } - + void readConnectionPreface() { byte[] data = new byte[CLIENT_PREFACE_START.length]; try { input.fill(true, data); - } catch (IOException ioe) { - if (log.isDebugEnabled()) { - log.debug(sm.getString("http2Parser.preface.io"), ioe); - } - return false; - } - for (int i = 0; i < CLIENT_PREFACE_START.length; i++) { - if (CLIENT_PREFACE_START[i] != data[i]) { - if (log.isDebugEnabled()) { - log.debug(sm.getString("http2Parser.preface.invalid", - new String(data, StandardCharsets.ISO_8859_1))); + for (int i = 0; i < CLIENT_PREFACE_START.length; i++) { + if (CLIENT_PREFACE_START[i] != data[i]) { + throw new ProtocolException(sm.getString("http2Parser.preface.invalid")); } - return false; } - } - - // Must always be followed by a settings frame - readFrame(true, FrameType.SETTINGS); - readPreface = true; - return true; + // Must always be followed by a settings frame + readFrame(true, FrameType.SETTINGS); + } catch (IOException ioe) { + throw new ProtocolException(sm.getString("http2Parser.preface.io"), ioe); + } } Modified: tomcat/trunk/java/org/apache/coyote/http2/Http2UpgradeHandler.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/coyote/http2/Http2UpgradeHandler.java?rev=1683675&r1=1683674&r2=1683675&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/coyote/http2/Http2UpgradeHandler.java (original) +++ tomcat/trunk/java/org/apache/coyote/http2/Http2UpgradeHandler.java Fri Jun 5 08:30:33 2015 @@ -190,6 +190,10 @@ public class Http2UpgradeHandler extends throw new IllegalStateException(sm.getString("upgradeHandler.sendPrefaceFail"), ioe); } + // Make sure the client has sent a valid connection preface before we + // send the response to the original request over HTTP/2. + parser.readConnectionPreface(); + if (webConnection != null) { // Process the initial request on a container thread StreamProcessor streamProcessor = new StreamProcessor(stream, adapter, socketWrapper); @@ -227,12 +231,6 @@ public class Http2UpgradeHandler extends switch(status) { case OPEN_READ: try { - if (!parser.readConnectionPreface()) { - close(); - result = SocketState.CLOSED; - break; - } - while (parser.readFrame(false)) { } } catch (Http2Exception h2e) { --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org