You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2015/06/04 17:32:01 UTC
svn commit: r1683577 -
/tomcat/trunk/java/org/apache/coyote/http2/Http2Parser.java
Author: markt
Date: Thu Jun 4 15:32:01 2015
New Revision: 1683577
URL: http://svn.apache.org/r1683577
Log:
Start to refactor validation. This aims to:
- reduce duplication
- improve readability
- concentrate validation code so it is easier to verify against the spec
Modified:
tomcat/trunk/java/org/apache/coyote/http2/Http2Parser.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=1683577&r1=1683576&r2=1683577&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/coyote/http2/Http2Parser.java (original)
+++ tomcat/trunk/java/org/apache/coyote/http2/Http2Parser.java Thu Jun 4 15:32:01 2015
@@ -90,30 +90,7 @@ class Http2Parser {
int flags = ByteUtil.getOneByte(frameHeaderBuffer, 4);
int streamId = ByteUtil.get31Bits(frameHeaderBuffer, 5);
- if (expected != null && frameType != expected.intValue()) {
- throw new Http2Exception(sm.getString("http2Parser.processFrame.unexpectedType",
- expected, Integer.toString(frameType)),
- streamId, ErrorCode.PROTOCOL_ERROR);
- }
-
- if (payloadSize > maxPayloadSize) {
- throw new Http2Exception(sm.getString("http2Parser.payloadTooBig",
- Integer.toString(payloadSize), Integer.toString(maxPayloadSize)),
- streamId, ErrorCode.FRAME_SIZE_ERROR);
- }
-
- if (headersCurrentStream != -1) {
- if (headersCurrentStream != streamId) {
- throw new Http2Exception(sm.getString("http2Parser.headers.wrongStream",
- connectionId, Integer.toString(headersCurrentStream),
- Integer.toString(streamId)), streamId, ErrorCode.COMPRESSION_ERROR);
- }
- if (frameType != FRAME_TYPE_CONTINUATION) {
- throw new Http2Exception(sm.getString("http2Parser.headers.wrongFrameType",
- connectionId, Integer.toString(headersCurrentStream),
- Integer.toString(frameType)), streamId, ErrorCode.COMPRESSION_ERROR);
- }
- }
+ validateFrame(expected, frameType, streamId, flags, payloadSize);
switch (frameType) {
case FRAME_TYPE_DATA:
@@ -153,12 +130,6 @@ class Http2Parser {
private void readDataFrame(int streamId, int flags, int payloadSize) throws IOException {
- if (log.isDebugEnabled()) {
- log.debug(sm.getString("http2Parser.processFrame", connectionId,
- Integer.toString(streamId), Integer.toString(flags),
- Integer.toString(payloadSize)));
- }
-
// Validate the stream
if (streamId == 0) {
throw new Http2Exception(sm.getString("http2Parser.processFrameData.invalidStream"),
@@ -197,12 +168,6 @@ class Http2Parser {
private void readHeadersFrame(int streamId, int flags, int payloadSize) throws IOException {
- if (log.isDebugEnabled()) {
- log.debug(sm.getString("http2Parser.processFrame", connectionId,
- Integer.toString(streamId), Integer.toString(flags),
- Integer.toString(payloadSize)));
- }
-
// Validate the stream
if (streamId == 0) {
throw new Http2Exception(sm.getString("http2Parser.processFrameHeaders.invalidStream"),
@@ -264,11 +229,6 @@ class Http2Parser {
private void readPriorityFrame(int streamId, int flags, int payloadSize) throws IOException {
- if (log.isDebugEnabled()) {
- log.debug(sm.getString("http2Parser.processFrame", connectionId,
- Integer.toString(streamId), Integer.toString(flags),
- Integer.toString(payloadSize)));
- }
// Validate the frame
if (streamId == 0) {
throw new Http2Exception(sm.getString("http2Parser.processFramePriority.invalidStream"),
@@ -291,12 +251,6 @@ class Http2Parser {
private void readSettingsFrame(int streamId, int flags, int payloadSize) throws IOException {
- if (log.isDebugEnabled()) {
- log.debug(sm.getString("http2Parser.processFrame", connectionId,
- Integer.toString(streamId), Integer.toString(flags),
- Integer.toString(payloadSize)));
- }
-
// Validate the frame
if (streamId != 0) {
throw new Http2Exception(sm.getString("http2Parser.processFrameSettings.invalidStream",
@@ -326,24 +280,14 @@ class Http2Parser {
}
- private void readPushPromiseFrame(int streamId, int flags, int payloadSize) {
- if (log.isDebugEnabled()) {
- log.debug(sm.getString("http2Parser.processFrame", connectionId,
- Integer.toString(streamId), Integer.toString(flags),
- Integer.toString(payloadSize)));
- }
-
+ private void readPushPromiseFrame(int streamId, int flags, int payloadSize) throws IOException {
// TODO: Should never be received by a server
+ swallow(payloadSize);
}
private void readPingFrame(int streamId, int flags, int payloadSize)
throws IOException {
- if (log.isDebugEnabled()) {
- log.debug(sm.getString("http2Parser.processFrame", connectionId,
- Integer.toString(streamId), Integer.toString(flags),
- Integer.toString(payloadSize)));
- }
// Validate the frame
if (streamId != 0) {
throw new Http2Exception(sm.getString("http2Parser.processFramePing.invalidStream",
@@ -366,12 +310,6 @@ class Http2Parser {
private void readGoawayFrame(int streamId, int flags, int payloadSize)
throws IOException {
- if (log.isDebugEnabled()) {
- log.debug(sm.getString("http2Parser.processFrame", connectionId,
- Integer.toString(streamId), Integer.toString(flags),
- Integer.toString(payloadSize)));
- }
-
// Validate the frame
if (streamId != 0) {
throw new Http2Exception(sm.getString("http2Parser.processFrameGoaway.invalidStream",
@@ -397,11 +335,6 @@ class Http2Parser {
private void readWindowUpdateFrame(int streamId, int flags, int payloadSize)
throws IOException {
- if (log.isDebugEnabled()) {
- log.debug(sm.getString("http2Parser.processFrame", connectionId,
- Integer.toString(streamId), Integer.toString(flags),
- Integer.toString(payloadSize)));
- }
// Validate the frame
if (payloadSize != 4) {
// Use stream 0 since this is always a connection error
@@ -430,12 +363,6 @@ class Http2Parser {
private void readContinuationFrame(int streamId, int flags, int payloadSize)
throws IOException {
- if (log.isDebugEnabled()) {
- log.debug(sm.getString("http2Parser.processFrame", connectionId,
- Integer.toString(streamId), Integer.toString(flags),
- Integer.toString(payloadSize)));
- }
-
if (streamId == 0) {
throw new Http2Exception(sm.getString(
"http2Parser.processFrameContinuation.invalidStream", connectionId),
@@ -511,6 +438,43 @@ class Http2Parser {
}
+ private void validateFrame(Integer expected, int frameType, int streamId, int flags,
+ int payloadSize) throws Http2Exception {
+
+ if (log.isDebugEnabled()) {
+ log.debug(sm.getString("http2Parser.processFrame", connectionId,
+ Integer.toString(streamId), Integer.toString(flags),
+ Integer.toString(payloadSize)));
+ }
+
+ if (expected != null && frameType != expected.intValue()) {
+ throw new Http2Exception(sm.getString("http2Parser.processFrame.unexpectedType",
+ expected, Integer.toString(frameType)),
+ streamId, ErrorCode.PROTOCOL_ERROR);
+ }
+
+ if (payloadSize > maxPayloadSize) {
+ throw new Http2Exception(sm.getString("http2Parser.payloadTooBig",
+ Integer.toString(payloadSize), Integer.toString(maxPayloadSize)),
+ streamId, ErrorCode.FRAME_SIZE_ERROR);
+ }
+
+ if (headersCurrentStream != -1) {
+ if (headersCurrentStream != streamId) {
+ throw new Http2Exception(sm.getString("http2Parser.headers.wrongStream",
+ connectionId, Integer.toString(headersCurrentStream),
+ Integer.toString(streamId)), streamId, ErrorCode.COMPRESSION_ERROR);
+ }
+ if (frameType != FRAME_TYPE_CONTINUATION) {
+ throw new Http2Exception(sm.getString("http2Parser.headers.wrongFrameType",
+ connectionId, Integer.toString(headersCurrentStream),
+ Integer.toString(frameType)), streamId, ErrorCode.COMPRESSION_ERROR);
+ }
+ }
+
+ }
+
+
/**
* Read and validate the connection preface from input using blocking IO.
*
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org