Class HpackEncoder


  • final class HpackEncoder
    extends java.lang.Object
    An HPACK encoder.

    Implementation note: This class is security sensitive, and depends on users correctly identifying their headers as security sensitive or not. If a header is considered not sensitive, methods names "insensitive" are used which are fast, but don't provide any security guarantees.

    • Constructor Detail

      • HpackEncoder

        HpackEncoder()
        Creates a new encoder.
      • HpackEncoder

        HpackEncoder​(boolean ignoreMaxHeaderListSize)
        Creates a new encoder.
      • HpackEncoder

        HpackEncoder​(boolean ignoreMaxHeaderListSize,
                     int arraySizeHint,
                     int huffCodeThreshold)
        Creates a new encoder.
    • Method Detail

      • encodeHeader

        private void encodeHeader​(ByteBuf out,
                                  java.lang.CharSequence name,
                                  java.lang.CharSequence value,
                                  boolean sensitive,
                                  long headerSize)
        Encode the header field into the header block.

        The given CharSequences must be immutable!

      • encodeAndAddEntries

        private void encodeAndAddEntries​(ByteBuf out,
                                         java.lang.CharSequence name,
                                         int nameHash,
                                         java.lang.CharSequence value,
                                         int valueHash)
      • setMaxHeaderTableSize

        public void setMaxHeaderTableSize​(ByteBuf out,
                                          long maxHeaderTableSize)
                                   throws Http2Exception
        Set the maximum table size.
        Throws:
        Http2Exception
      • getMaxHeaderTableSize

        public long getMaxHeaderTableSize()
        Return the maximum table size.
      • getMaxHeaderListSize

        public long getMaxHeaderListSize()
      • encodeInteger

        private static void encodeInteger​(ByteBuf out,
                                          int mask,
                                          int n,
                                          int i)
        Encode integer according to Section 5.1.
      • encodeInteger

        private static void encodeInteger​(ByteBuf out,
                                          int mask,
                                          int n,
                                          long i)
        Encode integer according to Section 5.1.
      • encodeStringLiteral

        private void encodeStringLiteral​(ByteBuf out,
                                         java.lang.CharSequence string)
        Encode string literal according to Section 5.2.
      • encodeLiteral

        private void encodeLiteral​(ByteBuf out,
                                   java.lang.CharSequence name,
                                   java.lang.CharSequence value,
                                   HpackUtil.IndexType indexType,
                                   int nameIndex)
        Encode literal header field according to Section 6.2.
      • getNameIndex

        private int getNameIndex​(java.lang.CharSequence name)
      • ensureCapacity

        private void ensureCapacity​(long headerSize)
        Ensure that the dynamic table has enough room to hold 'headerSize' more bytes. Removes the oldest entry from the dynamic table until sufficient space is available.
      • length

        int length()
        Return the number of header fields in the dynamic table. Exposed for testing.
      • size

        long size()
        Return the size of the dynamic table. Exposed for testing.
      • getHeaderField

        HpackHeaderField getHeaderField​(int index)
        Return the header field at the given index. Exposed for testing.
      • getEntryInsensitive

        private HpackEncoder.NameValueEntry getEntryInsensitive​(java.lang.CharSequence name,
                                                                int nameHash,
                                                                java.lang.CharSequence value,
                                                                int valueHash)
        Returns the header entry with the lowest index value for the header field. Returns null if header field is not in the dynamic table.
      • getEntry

        private HpackEncoder.NameEntry getEntry​(java.lang.CharSequence name,
                                                int nameHash)
        Returns the lowest index value for the header field name in the dynamic table. Returns -1 if the header field name is not in the dynamic table.
      • getIndexPlusOffset

        private int getIndexPlusOffset​(int counter)
      • getIndex

        private int getIndex​(int counter)
        Compute the index into the dynamic table given the counter in the header entry.
      • latestCounter

        private int latestCounter()
      • addNameEntry

        private void addNameEntry​(java.lang.CharSequence name,
                                  int nameHash,
                                  int nextCounter)
      • addNameValueEntry

        private void addNameValueEntry​(java.lang.CharSequence name,
                                       java.lang.CharSequence value,
                                       int nameHash,
                                       int valueHash,
                                       int nextCounter)
      • remove

        private void remove()
        Remove the oldest header field from the dynamic table.
      • isEmpty

        private boolean isEmpty()
      • removeNameEntryMatchingCounter

        private void removeNameEntryMatchingCounter​(java.lang.CharSequence name,
                                                    int counter)
      • bucket

        private int bucket​(int h)
        Returns the bucket of the hash table for the hash code h.
      • hash

        private static int hash​(int nameHash,
                                int valueHash)