Class HPackContext

java.lang.Object
org.omegazero.http.h2.hpack.HPackContext
All Implemented Interfaces:
Serializable

public class HPackContext extends Object implements Serializable
A HPackContext contains one encoder and decoder for use on a HTTP/2 connection.

Creating a HPackContext also requires a HPackContext.Session. A HPack session is useful when forwarding HTTP/2 messages because it shares the list of headers that are not allowed to be compressed. By default, creating a HPackContext also creates a new session.

This class is not thread-safe. The Session class is thread-safe.

Since:
1.2.1
See Also:
  • Constructor Details

    • HPackContext

      public HPackContext(boolean useHuffmanEncoding, int decoderDynamicTableMaxSize)
      Creates a new HPackContext with a new Session.
      Parameters:
      useHuffmanEncoding - Whether to use Huffman Encoding
      decoderDynamicTableMaxSize - The maximum table size of the decoder, as sent in a SETTINGS frame
    • HPackContext

      public HPackContext(HPackContext.Session session, boolean useHuffmanEncoding, int decoderDynamicTableMaxSize)
      Creates a new HPackContext.
      Parameters:
      session - The HPackContext.Session
      useHuffmanEncoding - Whether to use Huffman Encoding
      decoderDynamicTableMaxSize - The maximum table size of the decoder, as sent in a SETTINGS frame
  • Method Details

    • decodeHeaderBlock

      public HTTPHeaderContainer decodeHeaderBlock(byte[] data)
      Decodes the given full header block data according to RFC 7541.
      Parameters:
      data - The full header data
      Returns:
      A HTTPHeaderContainer containing the decoded header data, or null if a decoding error occurred
    • encodeHeaders

      public byte[] encodeHeaders(Map<String,String> headers)
      Encodes the given header name-value pairs according to RFC 7541 with a single new HPackContext.EncoderContext.
      Parameters:
      headers - The header pairs
      Returns:
      The encoded data
      See Also:
    • encodeHeader

      public void encodeHeader(HPackContext.EncoderContext context, String name, String value)
      Encodes the given header in the EncoderContext according to RFC 7541.
      Parameters:
      context - The EncoderContext to encode this header in
      name - The name of the header
      value - The value of the header
      Throws:
      IllegalStateException - If setEncoderDynamicTableMaxSizeSettings(int) was never called
      See Also:
    • isUseHuffmanEncoding

      public boolean isUseHuffmanEncoding()
      Returns true if Huffman Encoding was enabled in the constructor.
      Returns:
      Whether Huffman Encoding is enabled
    • getEncoderDynamicTableMaxSizeSettings

      public int getEncoderDynamicTableMaxSizeSettings()
      Returns the maximum size of the dynamic table of the encoder, as received in a SETTINGS frame.
      Returns:
      The maximum encoder dynamic table size
      Throws:
      IllegalStateException - If setEncoderDynamicTableMaxSizeSettings(int) was never called
      See Also:
    • setEncoderDynamicTableMaxSizeSettings

      public void setEncoderDynamicTableMaxSizeSettings(int dynamicTableMaxSize)
      Sets the maximum size of the dynamic table of the encoder, as received in a SETTINGS frame.

      This method must be called before encoding headers using this HPackContext.

      Parameters:
      dynamicTableMaxSize - The maximum encoder dynamic table size
      See Also:
    • getEncoderDynamicTableMaxSizeCurrent

      public int getEncoderDynamicTableMaxSizeCurrent()
      Returns the current maximum size of the dynamic table of the encoder, as received in a SETTINGS frame, possibly lowered using setEncoderDynamicTableMaxSizeCurrent(int).
      Returns:
      The maximum encoder dynamic table size
      See Also:
    • setEncoderDynamicTableMaxSizeCurrent

      public void setEncoderDynamicTableMaxSizeCurrent(int dynamicTableMaxSize)
      Sets the current maximum size of the dynamic table of the encoder.

      If setEncoderDynamicTableMaxSizeSettings(int) was called with the value received in a SETTINGS frame, this must be lower than or equal to that value. If lower, a Dynamic Table Size Update will be sent in the next encoded HPACK headers.

      This method may be called before a call to setEncoderDynamicTableMaxSizeSettings(int) to set a maximum value before a SETTINGS frame was received. If the SETTINGS maximum is lower than this value, the maximum set by this method will be lowered to the received value, otherwise, it will be unchanged.

      The given dynamicTableMaxSize may be -1, to always use the value set by setEncoderDynamicTableMaxSizeSettings(int) as the maximum dynamic table size. This is the default behavior.

      Parameters:
      dynamicTableMaxSize - The new maximum encoder dynamic table size
      See Also:
    • getEncoderDynamicTableSize

      public int getEncoderDynamicTableSize()
      Returns the current size of the dynamic table of the encoder.
      Returns:
      The current encoder dynamic table size
    • getDecoderDynamicTableMaxSizeSettings

      public int getDecoderDynamicTableMaxSizeSettings()
      Returns the maximum size of the dynamic table of the decoder, as sent in a SETTINGS frame.
      Returns:
      The maximum decoder dynamic table size
      See Also:
    • setDecoderDynamicTableMaxSizeSettings

      public void setDecoderDynamicTableMaxSizeSettings(int dynamicTableMaxSize)
      Sets the maximum size of the dynamic table of the decoder, as sent in a SETTINGS frame.
      Parameters:
      dynamicTableMaxSize - The maximum decoder dynamic table size
      See Also:
    • getDecoderDynamicTableMaxSizeCurrent

      public int getDecoderDynamicTableMaxSizeCurrent()
      Returns the current maximum size of the dynamic table of the decoder. This value is lower or equal to a value sent in a SETTINGS frame (set by setDecoderDynamicTableMaxSizeSettings(int)), possibly modified by a received Dynamic Table Size Update.
      Returns:
      The maximum decoder dynamic table size
      See Also:
    • getDecoderDynamicTableSize

      public int getDecoderDynamicTableSize()
      Returns the current size of the dynamic table of the decoder.
      Returns:
      The current decoder dynamic table size
    • getSession

      public HPackContext.Session getSession()
      Returns the Session configured in the constructor.
      Returns:
      The Session
    • writeInteger

      public static int writeInteger(byte[] dest, int offset, int n, long value)
      Encodes an integer into a byte array according to RFC 7541, section 5.1.
      Parameters:
      dest - The destination array
      offset - The offset in the array where to start writing to
      n - The prefix size N
      value - The integer value to write
      Returns:
      The number of additional bytes written. A value of 0 means only the byte at index offset was modified
      Throws:
      IndexOutOfBoundsException - The full value could not be written because the array is too short; all bytes starting at offset may or may not have been modified in that case
      See Also:
    • readInteger

      public static long readInteger(byte[] data, int offset, int n, HPackContext.IntRef read)
      Decodes an integer from a byte array according to RFC 7541, section 5.1.
      Parameters:
      data - The byte array
      offset - The offset where to start reading from
      n - The prefix size N
      read - Returns the number of additional bytes read, by adding it to the current value; if unchanged means only the byte at offset was read. Only valid if the return value of this function is positive. May be null
      Returns:
      The decoded integer, or a negative value if an error occurred
      See Also:
    • readInteger32

      public static int readInteger32(byte[] data, int offset, int n, HPackContext.IntRef read)
      Equivalent to readInteger(byte[], int, int, IntRef), except that -1 will be returned if the decoded integer is higher than a signed 32-bit value can represent (larger than Integer.MAX_VALUE).
      Parameters:
      data - The byte array
      offset - The offset where to start reading from
      n - The prefix size N
      read - Returns the number of additional bytes read
      Returns:
      The decoded integer, or a negative value if an error occurred or the decoded integer is larger than 31 bits
    • writeString

      public static int writeString(byte[] dest, int offset, byte[] strdata, boolean huffman)
      Encodes string data into the given byte array according to RFC 7541, section 5.2.
      Parameters:
      dest - The destination array
      offset - The offset in the array where to start writing to
      strdata - The encoded string data to write
      huffman - Whether the string should be encoded using huffman coding
      Returns:
      The number of additional bytes written. A value of 0 means only the byte at index offset was modified
      Throws:
      IndexOutOfBoundsException - The full string could not be written because the array is too short; all bytes starting at offset may or may not have been modified in that case
    • readString

      public static byte[] readString(byte[] data, int offset, HPackContext.IntRef read)
      Decodes a UTF-8 string from the given byte array according to RFC 7541, section 5.2.
      Parameters:
      data - The byte array
      offset - The offset where to start reading from
      read - Returns the number of additional bytes read, by adding it to the current value; if unchanged means only the byte at offset was read and the string is empty. Only valid if the return value of this function is not null. May be null
      Returns:
      The decoded string data, or null if the array is truncated or a decoding error occurred