From e8e3c4daefd1c00a6f638fd4c526cb6f049405ea Mon Sep 17 00:00:00 2001 From: kkolinko Date: Thu, 29 Sep 2011 02:13:50 +0000 Subject: [PATCH] No functional change. Simplify code and add comments: - unwrapped nested if(). - s/while()/if()/ where it has only one iteration. git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1177152 13f79535-47bb-0310-9956-ffa450edef68 --- .../coyote/http11/InternalAprInputBuffer.java | 10 +-- .../apache/coyote/http11/InternalInputBuffer.java | 10 +-- .../coyote/http11/InternalNioInputBuffer.java | 76 ++++++++++++++++++---- 3 files changed, 74 insertions(+), 22 deletions(-) diff --git a/java/org/apache/coyote/http11/InternalAprInputBuffer.java b/java/org/apache/coyote/http11/InternalAprInputBuffer.java index 294c5b10d..74706fdc1 100644 --- a/java/org/apache/coyote/http11/InternalAprInputBuffer.java +++ b/java/org/apache/coyote/http11/InternalAprInputBuffer.java @@ -340,11 +340,11 @@ public class InternalAprInputBuffer extends AbstractInputBuffer { chr = buf[pos]; - if ((chr == Constants.CR) || (chr == Constants.LF)) { - if (chr == Constants.LF) { - pos++; - return false; - } + if (chr == Constants.CR) { + // Skip + } else if (chr == Constants.LF) { + pos++; + return false; } else { break; } diff --git a/java/org/apache/coyote/http11/InternalInputBuffer.java b/java/org/apache/coyote/http11/InternalInputBuffer.java index 8a3222eeb..b4d2d5be1 100644 --- a/java/org/apache/coyote/http11/InternalInputBuffer.java +++ b/java/org/apache/coyote/http11/InternalInputBuffer.java @@ -296,11 +296,11 @@ public class InternalInputBuffer extends AbstractInputBuffer { chr = buf[pos]; - if ((chr == Constants.CR) || (chr == Constants.LF)) { - if (chr == Constants.LF) { - pos++; - return false; - } + if (chr == Constants.CR) { + // Skip + } else if (chr == Constants.LF) { + pos++; + return false; } else { break; } diff --git a/java/org/apache/coyote/http11/InternalNioInputBuffer.java b/java/org/apache/coyote/http11/InternalNioInputBuffer.java index e05a8070b..a310dbbcb 100644 --- a/java/org/apache/coyote/http11/InternalNioInputBuffer.java +++ b/java/org/apache/coyote/http11/InternalNioInputBuffer.java @@ -48,9 +48,42 @@ public class InternalNioInputBuffer extends AbstractInputBuffer { // -------------------------------------------------------------- Constants - enum HeaderParseStatus {DONE, HAVE_MORE_HEADERS, NEED_MORE_DATA} - enum HeaderParsePosition {HEADER_START, HEADER_NAME, HEADER_VALUE, - HEADER_MULTI_LINE, HEADER_SKIPLINE} + enum HeaderParseStatus { + DONE, HAVE_MORE_HEADERS, NEED_MORE_DATA + } + + enum HeaderParsePosition { + /** + * Start of a new header. A CRLF here means that there are no more + * headers. Any other character starts a header name. + */ + HEADER_START, + /** + * Reading a header name. All characters of header are HTTP_TOKEN_CHAR. + * Header name is followed by ':'. No whitespace is allowed.
+ * Any non-HTTP_TOKEN_CHAR (this includes any whitespace) encountered + * before ':' will result in the whole line being ignored. + */ + HEADER_NAME, + /** + * Reading the header value. We come into this state by two ways:
+ * a) just after the ':' on the first line of the header, b) on the + * start of a new line when it is known that it starts with SP or HT. + */ + HEADER_VALUE, + /** + * Before reading a new line of a header. Once the next byte is peeked, + * the state changes without advancing our position. The state becomes + * either HEADER_VALUE (if that first byte is SP or HT), or HEADER_START + * (otherwise). + */ + HEADER_MULTI_LINE, + /** + * Reading all bytes until the next CRLF. The line is being ignored. + */ + HEADER_SKIPLINE + } + // ----------------------------------------------------------- Constructors @@ -483,11 +516,11 @@ public class InternalNioInputBuffer extends AbstractInputBuffer { chr = buf[pos]; - if ((chr == Constants.CR) || (chr == Constants.LF)) { - if (chr == Constants.LF) { - pos++; - return HeaderParseStatus.DONE; - } + if (chr == Constants.CR) { + // Skip + } else if (chr == Constants.LF) { + pos++; + return HeaderParseStatus.DONE; } else { break; } @@ -500,14 +533,13 @@ public class InternalNioInputBuffer extends AbstractInputBuffer { // Mark the current buffer position headerData.start = pos; headerParsePos = HeaderParsePosition.HEADER_NAME; - } + } // // Reading the header name // Header name is always US-ASCII // - while (headerParsePos == HeaderParsePosition.HEADER_NAME) { // Read new bytes if needed @@ -538,8 +570,8 @@ public class InternalNioInputBuffer extends AbstractInputBuffer { } } - - while (headerParsePos == HeaderParsePosition.HEADER_SKIPLINE) { + // Skip the line and ignore the header + if (headerParsePos == HeaderParsePosition.HEADER_SKIPLINE) { return skipLine(); } @@ -681,9 +713,29 @@ public class InternalNioInputBuffer extends AbstractInputBuffer { private HeaderParseData headerData = new HeaderParseData(); public static class HeaderParseData { + /** + * When parsing header name: first character of the header.
+ * When parsing header value: first character after ':'. + */ int start = 0; + /** + * When parsing header name: not used (stays as 0).
+ * When parsing header value: starts as the first character after ':'. + * Then is increased as far as more bytes of the header are harvested. + * Bytes from buf[pos] are copied to buf[realPos]. Thus the string from + * [start] to [realPos-1] is the prepared value of the header, with + * whitespaces removed as needed. + */ int realPos = 0; + /** + * When parsing header name: not used (stays as 0).
+ * When parsing header value: position after the last not-LWS character. + */ int lastSignificantChar = 0; + /** + * MB that will store the value of the header. It is null while parsing + * header name and is created after the name has been parsed. + */ MessageBytes headerValue = null; public void recycle() { start = 0; -- 2.11.0