Home / Class/ HpackEncoder Class — netty Architecture

HpackEncoder Class — netty Architecture

Architecture documentation for the HpackEncoder class in HpackEncoder.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  616b4418_6852_a9a9_188d_063a8768b35a["HpackEncoder"]
  b04c7124_b021_2376_03d8_147c4787bd78["HpackEncoder.java"]
  616b4418_6852_a9a9_188d_063a8768b35a -->|defined in| b04c7124_b021_2376_03d8_147c4787bd78
  a2f11a6d_cea0_0712_5bec_f0dd8d6c1e0e["HpackEncoder()"]
  616b4418_6852_a9a9_188d_063a8768b35a -->|method| a2f11a6d_cea0_0712_5bec_f0dd8d6c1e0e
  01b56066_223e_92d6_96df_fd4dd2e7dddb["encodeHeaders()"]
  616b4418_6852_a9a9_188d_063a8768b35a -->|method| 01b56066_223e_92d6_96df_fd4dd2e7dddb
  a657774a_7ae2_3ee7_53d1_bce01150a34f["encodeHeadersEnforceMaxHeaderListSize()"]
  616b4418_6852_a9a9_188d_063a8768b35a -->|method| a657774a_7ae2_3ee7_53d1_bce01150a34f
  a8618c2b_623b_24d4_8ec8_e02e3e01f3c3["encodeHeadersIgnoreMaxHeaderListSize()"]
  616b4418_6852_a9a9_188d_063a8768b35a -->|method| a8618c2b_623b_24d4_8ec8_e02e3e01f3c3
  d416f498_d09b_3bf6_6f3c_bec7a647a54f["encodeHeader()"]
  616b4418_6852_a9a9_188d_063a8768b35a -->|method| d416f498_d09b_3bf6_6f3c_bec7a647a54f
  67aada40_9838_0523_a316_eceb773154ab["encodeAndAddEntries()"]
  616b4418_6852_a9a9_188d_063a8768b35a -->|method| 67aada40_9838_0523_a316_eceb773154ab
  adfd1aa5_7f5f_afdb_ebcb_3b15451068ea["setMaxHeaderTableSize()"]
  616b4418_6852_a9a9_188d_063a8768b35a -->|method| adfd1aa5_7f5f_afdb_ebcb_3b15451068ea
  8c282970_ae90_8507_dcc8_8783e2d07002["getMaxHeaderTableSize()"]
  616b4418_6852_a9a9_188d_063a8768b35a -->|method| 8c282970_ae90_8507_dcc8_8783e2d07002
  ec73f8cc_c4f0_6fb1_16f5_ab299f521e9b["setMaxHeaderListSize()"]
  616b4418_6852_a9a9_188d_063a8768b35a -->|method| ec73f8cc_c4f0_6fb1_16f5_ab299f521e9b
  e8127c00_1132_4f04_e340_06777151e5b6["getMaxHeaderListSize()"]
  616b4418_6852_a9a9_188d_063a8768b35a -->|method| e8127c00_1132_4f04_e340_06777151e5b6
  ff646124_1e6a_47c4_ccf7_8d4b7525d870["encodeInteger()"]
  616b4418_6852_a9a9_188d_063a8768b35a -->|method| ff646124_1e6a_47c4_ccf7_8d4b7525d870
  772d7221_f578_9e4a_8e58_b93602d19372["encodeStringLiteral()"]
  616b4418_6852_a9a9_188d_063a8768b35a -->|method| 772d7221_f578_9e4a_8e58_b93602d19372
  23158887_3faf_9358_cf8e_ccef0456523f["encodeLiteral()"]
  616b4418_6852_a9a9_188d_063a8768b35a -->|method| 23158887_3faf_9358_cf8e_ccef0456523f

Relationship Graph

Source Code

codec-http2/src/main/java/io/netty/handler/codec/http2/HpackEncoder.java lines 63–555

final class HpackEncoder {
    static final int NOT_FOUND = -1;
    static final int HUFF_CODE_THRESHOLD = 512;
    // a hash map of header fields keyed by header name
    private final NameEntry[] nameEntries;

    // a hash map of header fields keyed by header name and value
    private final NameValueEntry[] nameValueEntries;

    private final NameValueEntry head = new NameValueEntry(-1, AsciiString.EMPTY_STRING,
      AsciiString.EMPTY_STRING, Integer.MAX_VALUE, null);

    private NameValueEntry latest = head;

    private final HpackHuffmanEncoder hpackHuffmanEncoder = new HpackHuffmanEncoder();
    private final byte hashMask;
    private final boolean ignoreMaxHeaderListSize;
    private final int huffCodeThreshold;
    private long size;
    private long maxHeaderTableSize;
    private long maxHeaderListSize;

    /**
     * Creates a new encoder.
     */
    HpackEncoder() {
        this(false);
    }

    /**
     * Creates a new encoder.
     */
    HpackEncoder(boolean ignoreMaxHeaderListSize) {
        this(ignoreMaxHeaderListSize, 64, HUFF_CODE_THRESHOLD);
    }

    /**
     * Creates a new encoder.
     */
    HpackEncoder(boolean ignoreMaxHeaderListSize, int arraySizeHint, int huffCodeThreshold) {
        this.ignoreMaxHeaderListSize = ignoreMaxHeaderListSize;
        maxHeaderTableSize = DEFAULT_HEADER_TABLE_SIZE;
        maxHeaderListSize = MAX_HEADER_LIST_SIZE;
        // Enforce a bound of [2, 128] because hashMask is a byte. The max possible value of hashMask is one less
        // than the length of this array, and we want the mask to be > 0.
        nameEntries = new NameEntry[findNextPositivePowerOfTwo(max(2, min(arraySizeHint, 128)))];
        nameValueEntries = new NameValueEntry[nameEntries.length];
        hashMask = (byte) (nameEntries.length - 1);
        this.huffCodeThreshold = huffCodeThreshold;
    }

    /**
     * Encode the header field into the header block.
     * <p>
     * <strong>The given {@link CharSequence}s must be immutable!</strong>
     */
    public void encodeHeaders(int streamId, ByteBuf out, Http2Headers headers, SensitivityDetector sensitivityDetector)
      throws Http2Exception {
        if (ignoreMaxHeaderListSize) {
            encodeHeadersIgnoreMaxHeaderListSize(out, headers, sensitivityDetector);
        } else {
            encodeHeadersEnforceMaxHeaderListSize(streamId, out, headers, sensitivityDetector);
        }
    }

    private void encodeHeadersEnforceMaxHeaderListSize(int streamId, ByteBuf out, Http2Headers headers,
                                                       SensitivityDetector sensitivityDetector)
      throws Http2Exception {
        long headerSize = 0;
        // To ensure we stay consistent with our peer check the size is valid before we potentially modify HPACK state.
        for (Map.Entry<CharSequence, CharSequence> header : headers) {
            CharSequence name = header.getKey();
            CharSequence value = header.getValue();
            // OK to increment now and check for bounds after because this value is limited to unsigned int and will not
            // overflow.
            headerSize += HpackHeaderField.sizeOf(name, value);
            if (headerSize > maxHeaderListSize) {
                headerListSizeExceeded(streamId, maxHeaderListSize, false);
            }
        }
        encodeHeadersIgnoreMaxHeaderListSize(out, headers, sensitivityDetector);

Frequently Asked Questions

What is the HpackEncoder class?
HpackEncoder is a class in the netty codebase, defined in codec-http2/src/main/java/io/netty/handler/codec/http2/HpackEncoder.java.
Where is HpackEncoder defined?
HpackEncoder is defined in codec-http2/src/main/java/io/netty/handler/codec/http2/HpackEncoder.java at line 63.

Analyze Your Own Codebase

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

Try Supermodel Free