Class Http2CodecUtil


  • @UnstableApi
    public final class Http2CodecUtil
    extends Object
    Constants and utility method used for encoding/decoding HTTP2 frames.
    • Field Detail

      • HTTP_UPGRADE_SETTINGS_HEADER

        public static final CharSequence HTTP_UPGRADE_SETTINGS_HEADER
      • HTTP_UPGRADE_PROTOCOL_NAME

        public static final CharSequence HTTP_UPGRADE_PROTOCOL_NAME
      • TLS_UPGRADE_PROTOCOL_NAME

        public static final CharSequence TLS_UPGRADE_PROTOCOL_NAME
      • PING_FRAME_PAYLOAD_LENGTH

        public static final int PING_FRAME_PAYLOAD_LENGTH
        See Also:
        Constant Field Values
      • MAX_PADDING

        public static final int MAX_PADDING
        The maximum number of padding bytes. That is the 255 padding bytes appended to the end of a frame and the 1 byte pad length field.
        See Also:
        Constant Field Values
      • CONNECTION_PREFACE_BUFFER

        public static final Supplier<Buffer> CONNECTION_PREFACE_BUFFER
      • DATA_FRAME_HEADER_LENGTH

        public static final int DATA_FRAME_HEADER_LENGTH
        See Also:
        Constant Field Values
      • HEADERS_FRAME_HEADER_LENGTH

        public static final int HEADERS_FRAME_HEADER_LENGTH
        See Also:
        Constant Field Values
      • RST_STREAM_FRAME_LENGTH

        public static final int RST_STREAM_FRAME_LENGTH
        See Also:
        Constant Field Values
      • PUSH_PROMISE_FRAME_HEADER_LENGTH

        public static final int PUSH_PROMISE_FRAME_HEADER_LENGTH
        See Also:
        Constant Field Values
      • GO_AWAY_FRAME_HEADER_LENGTH

        public static final int GO_AWAY_FRAME_HEADER_LENGTH
        See Also:
        Constant Field Values
      • WINDOW_UPDATE_FRAME_LENGTH

        public static final int WINDOW_UPDATE_FRAME_LENGTH
        See Also:
        Constant Field Values
      • CONTINUATION_FRAME_HEADER_LENGTH

        public static final int CONTINUATION_FRAME_HEADER_LENGTH
        See Also:
        Constant Field Values
      • SETTINGS_HEADER_TABLE_SIZE

        public static final char SETTINGS_HEADER_TABLE_SIZE
        See Also:
        Constant Field Values
      • SETTINGS_MAX_CONCURRENT_STREAMS

        public static final char SETTINGS_MAX_CONCURRENT_STREAMS
        See Also:
        Constant Field Values
      • SETTINGS_INITIAL_WINDOW_SIZE

        public static final char SETTINGS_INITIAL_WINDOW_SIZE
        See Also:
        Constant Field Values
      • SETTINGS_MAX_FRAME_SIZE

        public static final char SETTINGS_MAX_FRAME_SIZE
        See Also:
        Constant Field Values
      • SETTINGS_MAX_HEADER_LIST_SIZE

        public static final char SETTINGS_MAX_HEADER_LIST_SIZE
        See Also:
        Constant Field Values
      • MAX_CONCURRENT_STREAMS

        public static final long MAX_CONCURRENT_STREAMS
        See Also:
        Constant Field Values
      • MAX_INITIAL_WINDOW_SIZE

        public static final int MAX_INITIAL_WINDOW_SIZE
        See Also:
        Constant Field Values
      • MAX_FRAME_SIZE_LOWER_BOUND

        public static final int MAX_FRAME_SIZE_LOWER_BOUND
        See Also:
        Constant Field Values
      • MAX_FRAME_SIZE_UPPER_BOUND

        public static final int MAX_FRAME_SIZE_UPPER_BOUND
        See Also:
        Constant Field Values
      • MIN_CONCURRENT_STREAMS

        public static final long MIN_CONCURRENT_STREAMS
        See Also:
        Constant Field Values
      • MIN_INITIAL_WINDOW_SIZE

        public static final int MIN_INITIAL_WINDOW_SIZE
        See Also:
        Constant Field Values
      • DEFAULT_PRIORITY_WEIGHT

        public static final short DEFAULT_PRIORITY_WEIGHT
        See Also:
        Constant Field Values
      • DEFAULT_HEADER_TABLE_SIZE

        public static final int DEFAULT_HEADER_TABLE_SIZE
        See Also:
        Constant Field Values
      • DEFAULT_HEADER_LIST_SIZE

        public static final long DEFAULT_HEADER_LIST_SIZE
        The initial value of this setting is unlimited. However in practice we don't want to allow our peers to use unlimited memory by default. So we take advantage of the For any given request, a lower limit than what is advertised MAY be enforced. loophole.
        See Also:
        Constant Field Values
      • SMALLEST_MAX_CONCURRENT_STREAMS

        public static final int SMALLEST_MAX_CONCURRENT_STREAMS
        The assumed minimum value for SETTINGS_MAX_CONCURRENT_STREAMS as recommended by the HTTP/2 spec.
        See Also:
        Constant Field Values
      • DEFAULT_GRACEFUL_SHUTDOWN_TIMEOUT_MILLIS

        public static final long DEFAULT_GRACEFUL_SHUTDOWN_TIMEOUT_MILLIS
      • DEFAULT_MAX_QUEUED_CONTROL_FRAMES

        public static final int DEFAULT_MAX_QUEUED_CONTROL_FRAMES
        See Also:
        Constant Field Values
    • Method Detail

      • calculateMaxHeaderListSizeGoAway

        public static long calculateMaxHeaderListSizeGoAway​(long maxHeaderListSize)
        Calculate the threshold in bytes which should trigger a GO_AWAY if a set of headers exceeds this amount.
        Parameters:
        maxHeaderListSize - SETTINGS_MAX_HEADER_LIST_SIZE for the local endpoint.
        Returns:
        the threshold in bytes which should trigger a GO_AWAY if a set of headers exceeds this amount.
      • isOutboundStream

        public static boolean isOutboundStream​(boolean server,
                                               int streamId)
        Returns true if the stream is an outbound stream.
        Parameters:
        server - true if the endpoint is a server, false otherwise.
        streamId - the stream identifier
      • isStreamIdValid

        public static boolean isStreamIdValid​(int streamId)
        Returns true if the streamId is a valid HTTP/2 stream identifier.
      • isMaxFrameSizeValid

        public static boolean isMaxFrameSizeValid​(int maxFrameSize)
        Indicates whether or not the given value for max frame size falls within the valid range.
      • getEmbeddedHttp2Exception

        public static Http2Exception getEmbeddedHttp2Exception​(Throwable cause)
        Iteratively looks through the causality chain for the given exception and returns the first Http2Exception or null if none.
      • toBuffer

        public static Buffer toBuffer​(ChannelHandlerContext ctx,
                                      Throwable cause)
        Creates a buffer containing the error message from the given exception. If the cause is null returns an empty buffer.
      • readUnsignedInt

        public static int readUnsignedInt​(Buffer buf)
        Reads a big-endian (31-bit) integer from the buffer.
      • writeFrameHeader

        public static void writeFrameHeader​(Buffer out,
                                            int payloadLength,
                                            byte type,
                                            Http2Flags flags,
                                            int streamId)
        Writes an HTTP/2 frame header to the output buffer.
      • streamableBytes

        public static int streamableBytes​(StreamByteDistributor.StreamState state)
        Calculate the amount of bytes that can be sent by state. The lower bound is 0.
      • headerListSizeExceeded

        public static void headerListSizeExceeded​(int streamId,
                                                  long maxHeaderListSize,
                                                  boolean onDecode)
                                           throws Http2Exception
        Results in a RST_STREAM being sent for streamId due to violating SETTINGS_MAX_HEADER_LIST_SIZE.
        Parameters:
        streamId - The stream ID that was being processed when the exceptional condition occurred.
        maxHeaderListSize - The max allowed size for a list of headers in bytes which was exceeded.
        onDecode - true if the exception was encountered during decoder. false for encode.
        Throws:
        Http2Exception - a stream error.
      • headerListSizeExceeded

        public static void headerListSizeExceeded​(long maxHeaderListSize)
                                           throws Http2Exception
        Results in a GO_AWAY being sent due to violating SETTINGS_MAX_HEADER_LIST_SIZE in an unrecoverable manner.
        Parameters:
        maxHeaderListSize - The max allowed size for a list of headers in bytes which was exceeded.
        Throws:
        Http2Exception - a connection error.
      • verifyPadding

        public static void verifyPadding​(int padding)