Home / Class/ HeaderParser Class — netty Architecture

HeaderParser Class — netty Architecture

Architecture documentation for the HeaderParser class in HttpObjectDecoder.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  ef24e48c_0b83_21cd_41f1_a114c4a0039f["HeaderParser"]
  f15bbbf6_e315_8ba2_24a1_7970ea7b58eb["HttpObjectDecoder.java"]
  ef24e48c_0b83_21cd_41f1_a114c4a0039f -->|defined in| f15bbbf6_e315_8ba2_24a1_7970ea7b58eb
  95dc2497_69c5_73b9_cff8_9a5be1f8dff9["HeaderParser()"]
  ef24e48c_0b83_21cd_41f1_a114c4a0039f -->|method| 95dc2497_69c5_73b9_cff8_9a5be1f8dff9
  4c5fc4a7_9083_906d_2990_e8117307a129["ByteBuf()"]
  ef24e48c_0b83_21cd_41f1_a114c4a0039f -->|method| 4c5fc4a7_9083_906d_2990_e8117307a129
  1cee7556_656b_babe_0a65_ce33ca4e7d44["reset()"]
  ef24e48c_0b83_21cd_41f1_a114c4a0039f -->|method| 1cee7556_656b_babe_0a65_ce33ca4e7d44
  f5d893cd_599b_001e_7e33_291d8c194e8d["TooLongFrameException()"]
  ef24e48c_0b83_21cd_41f1_a114c4a0039f -->|method| f5d893cd_599b_001e_7e33_291d8c194e8d

Relationship Graph

Source Code

codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectDecoder.java lines 1160–1226

    private static class HeaderParser {
        protected final ByteBuf seq;
        protected final int maxLength;
        int size;

        HeaderParser(ByteBuf seq, int maxLength) {
            this.seq = seq;
            this.maxLength = maxLength;
        }

        public ByteBuf parse(ByteBuf buffer, Runnable strictCRLFCheck) {
            final int readableBytes = buffer.readableBytes();
            final int readerIndex = buffer.readerIndex();
            final int maxBodySize = maxLength - size;
            assert maxBodySize >= 0;
            // adding 2 to account for both CR (if present) and LF
            // don't remove 2L: it's key to cover maxLength = Integer.MAX_VALUE
            final long maxBodySizeWithCRLF = maxBodySize + 2L;
            final int toProcess = (int) Math.min(maxBodySizeWithCRLF, readableBytes);
            final int toIndexExclusive = readerIndex + toProcess;
            assert toIndexExclusive >= readerIndex;
            final int indexOfLf = buffer.indexOf(readerIndex, toIndexExclusive, HttpConstants.LF);
            if (indexOfLf == -1) {
                if (readableBytes > maxBodySize) {
                    // TODO: Respond with Bad Request and discard the traffic
                    //    or close the connection.
                    //       No need to notify the upstream handlers - just log.
                    //       If decoding a response, just throw an exception.
                    throw newException(maxLength);
                }
                return null;
            }
            final int endOfSeqIncluded;
            if (indexOfLf > readerIndex && buffer.getByte(indexOfLf - 1) == HttpConstants.CR) {
                // Drop CR if we had a CRLF pair
                endOfSeqIncluded = indexOfLf - 1;
            } else {
                if (strictCRLFCheck != null) {
                    strictCRLFCheck.run();
                }
                endOfSeqIncluded = indexOfLf;
            }
            final int newSize = endOfSeqIncluded - readerIndex;
            if (newSize == 0) {
                seq.clear();
                buffer.readerIndex(indexOfLf + 1);
                return seq;
            }
            int size = this.size + newSize;
            if (size > maxLength) {
                throw newException(maxLength);
            }
            this.size = size;
            seq.clear();
            seq.writeBytes(buffer, readerIndex, newSize);
            buffer.readerIndex(indexOfLf + 1);
            return seq;
        }

        public void reset() {
            size = 0;
        }

        protected TooLongFrameException newException(int maxLength) {
            return new TooLongHttpHeaderException("HTTP header is larger than " + maxLength + " bytes.");
        }
    }

Frequently Asked Questions

What is the HeaderParser class?
HeaderParser is a class in the netty codebase, defined in codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectDecoder.java.
Where is HeaderParser defined?
HeaderParser is defined in codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectDecoder.java at line 1160.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free