Package io.netty.handler.codec.http2
Class CompressorHttp2ConnectionEncoder
- java.lang.Object
-
- io.netty.handler.codec.http2.DecoratingHttp2FrameWriter
-
- io.netty.handler.codec.http2.DecoratingHttp2ConnectionEncoder
-
- io.netty.handler.codec.http2.CompressorHttp2ConnectionEncoder
-
- All Implemented Interfaces:
Http2ConnectionEncoder,Http2DataWriter,Http2FrameWriter,Http2SettingsReceivedConsumer,java.io.Closeable,java.lang.AutoCloseable
@UnstableApi public class CompressorHttp2ConnectionEncoder extends DecoratingHttp2ConnectionEncoder
A decorating HTTP2 encoder that will compress data frames according to thecontent-encodingheader for each stream. The compression provided by this class will be applied to the data for the entire stream.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface io.netty.handler.codec.http2.Http2FrameWriter
Http2FrameWriter.Configuration
-
-
Field Summary
Fields Modifier and Type Field Description private intcompressionLevelstatic intDEFAULT_COMPRESSION_LEVELstatic intDEFAULT_MEM_LEVELstatic intDEFAULT_WINDOW_BITSprivate intmemLevelprivate Http2Connection.PropertyKeypropertyKeyprivate intwindowBits
-
Constructor Summary
Constructors Constructor Description CompressorHttp2ConnectionEncoder(Http2ConnectionEncoder delegate)CompressorHttp2ConnectionEncoder(Http2ConnectionEncoder delegate, int compressionLevel, int windowBits, int memLevel)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidbindCompressorToStream(EmbeddedChannel compressor, int streamId)Called after the super class has written the headers and created any associated stream objects.(package private) voidcleanup(Http2Stream stream, EmbeddedChannel compressor)Release remaining content fromEmbeddedChanneland remove the compressor from theHttp2Stream.protected java.lang.CharSequencegetTargetContentEncoding(java.lang.CharSequence contentEncoding)Returns the expected content encoding of the decoded content.private EmbeddedChannelnewCompressionChannel(ChannelHandlerContext ctx, ZlibWrapper wrapper)Generate a new instance of anEmbeddedChannelcapable of compressing dataprivate EmbeddedChannelnewCompressor(ChannelHandlerContext ctx, Http2Headers headers, boolean endOfStream)Checks if a new compressor object is needed for the stream identified bystreamId.protected EmbeddedChannelnewContentCompressor(ChannelHandlerContext ctx, java.lang.CharSequence contentEncoding)Returns a newEmbeddedChannelthat encodes the HTTP2 message content encoded in the specifiedcontentEncoding.private static ByteBufnextReadableBuf(EmbeddedChannel compressor)ChannelFuturewriteData(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream, ChannelPromise promise)Writes aDATAframe to the remote endpoint.ChannelFuturewriteHeaders(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int padding, boolean endStream, ChannelPromise promise)Writes a HEADERS frame to the remote endpoint.ChannelFuturewriteHeaders(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endOfStream, ChannelPromise promise)Writes a HEADERS frame with priority specified to the remote endpoint.-
Methods inherited from class io.netty.handler.codec.http2.DecoratingHttp2ConnectionEncoder
connection, consumeReceivedSettings, flowController, frameWriter, lifecycleManager, pollSentSettings, remoteSettings
-
Methods inherited from class io.netty.handler.codec.http2.DecoratingHttp2FrameWriter
close, configuration, writeFrame, writeGoAway, writePing, writePriority, writePushPromise, writeRstStream, writeSettings, writeSettingsAck, writeWindowUpdate
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.netty.handler.codec.http2.Http2ConnectionEncoder
writeFrame
-
Methods inherited from interface io.netty.handler.codec.http2.Http2FrameWriter
close, configuration, writeGoAway, writePing, writePriority, writePushPromise, writeRstStream, writeSettings, writeSettingsAck, writeWindowUpdate
-
-
-
-
Field Detail
-
DEFAULT_COMPRESSION_LEVEL
public static final int DEFAULT_COMPRESSION_LEVEL
- See Also:
- Constant Field Values
-
DEFAULT_WINDOW_BITS
public static final int DEFAULT_WINDOW_BITS
- See Also:
- Constant Field Values
-
DEFAULT_MEM_LEVEL
public static final int DEFAULT_MEM_LEVEL
- See Also:
- Constant Field Values
-
compressionLevel
private final int compressionLevel
-
windowBits
private final int windowBits
-
memLevel
private final int memLevel
-
propertyKey
private final Http2Connection.PropertyKey propertyKey
-
-
Constructor Detail
-
CompressorHttp2ConnectionEncoder
public CompressorHttp2ConnectionEncoder(Http2ConnectionEncoder delegate)
-
CompressorHttp2ConnectionEncoder
public CompressorHttp2ConnectionEncoder(Http2ConnectionEncoder delegate, int compressionLevel, int windowBits, int memLevel)
-
-
Method Detail
-
writeData
public ChannelFuture writeData(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream, ChannelPromise promise)
Description copied from interface:Http2DataWriterWrites aDATAframe to the remote endpoint. This will result in one or more frames being written to the context.- Specified by:
writeDatain interfaceHttp2DataWriter- Overrides:
writeDatain classDecoratingHttp2FrameWriter- Parameters:
ctx- the context to use for writing.streamId- the stream for which to send the frame.data- the payload of the frame. This will be released by this method.padding- additional bytes that should be added to obscure the true content size. Must be between 0 and 256 (inclusive). A 1 byte padding is encoded as just the pad length field with value 0. A 256 byte padding is encoded as the pad length field with value 255 and 255 padding bytes appended to the end of the frame.endOfStream- indicates if this is the last frame to be sent for the stream.promise- the promise for the write.- Returns:
- the future for the write.
-
writeHeaders
public ChannelFuture writeHeaders(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int padding, boolean endStream, ChannelPromise promise)
Description copied from interface:Http2FrameWriterWrites a HEADERS frame to the remote endpoint.- Specified by:
writeHeadersin interfaceHttp2FrameWriter- Overrides:
writeHeadersin classDecoratingHttp2FrameWriter- Parameters:
ctx- the context to use for writing.streamId- the stream for which to send the frame.headers- the headers to be sent.padding- additional bytes that should be added to obscure the true content size. Must be between 0 and 256 (inclusive).endStream- indicates if this is the last frame to be sent for the stream.promise- the promise for the write.- Returns:
- the future for the write.
Section 10.5.1 states the following:
The header block MUST be processed to ensure a consistent connection state, unless the connection is closed.
If this call has modified the HPACK header state you MUST throw a connection error.If this call has NOT modified the HPACK header state you are free to throw a stream error.
-
writeHeaders
public ChannelFuture writeHeaders(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endOfStream, ChannelPromise promise)
Description copied from interface:Http2FrameWriterWrites a HEADERS frame with priority specified to the remote endpoint.- Specified by:
writeHeadersin interfaceHttp2FrameWriter- Overrides:
writeHeadersin classDecoratingHttp2FrameWriter- Parameters:
ctx- the context to use for writing.streamId- the stream for which to send the frame.headers- the headers to be sent.streamDependency- the stream on which this stream should depend, or 0 if it should depend on the connection.weight- the weight for this stream.exclusive- whether this stream should be the exclusive dependant of its parent.padding- additional bytes that should be added to obscure the true content size. Must be between 0 and 256 (inclusive).endOfStream- indicates if this is the last frame to be sent for the stream.promise- the promise for the write.- Returns:
- the future for the write.
Section 10.5.1 states the following:
The header block MUST be processed to ensure a consistent connection state, unless the connection is closed.
If this call has modified the HPACK header state you MUST throw a connection error.If this call has NOT modified the HPACK header state you are free to throw a stream error.
-
newContentCompressor
protected EmbeddedChannel newContentCompressor(ChannelHandlerContext ctx, java.lang.CharSequence contentEncoding) throws Http2Exception
Returns a newEmbeddedChannelthat encodes the HTTP2 message content encoded in the specifiedcontentEncoding.- Parameters:
ctx- the context.contentEncoding- the value of thecontent-encodingheader- Returns:
- a new
ByteToMessageDecoderif the specified encoding is supported.nullotherwise (alternatively, you can throw aHttp2Exceptionto block unknown encoding). - Throws:
Http2Exception- If the specified encoding is not not supported and warrants an exception
-
getTargetContentEncoding
protected java.lang.CharSequence getTargetContentEncoding(java.lang.CharSequence contentEncoding) throws Http2ExceptionReturns the expected content encoding of the decoded content. ReturningcontentEncodingis the default behavior, which is the case for most compressors.- Parameters:
contentEncoding- the value of thecontent-encodingheader- Returns:
- the expected content encoding of the new content.
- Throws:
Http2Exception- if thecontentEncodingis not supported and warrants an exception
-
newCompressionChannel
private EmbeddedChannel newCompressionChannel(ChannelHandlerContext ctx, ZlibWrapper wrapper)
Generate a new instance of anEmbeddedChannelcapable of compressing data- Parameters:
ctx- the context.wrapper- Defines what type of encoder should be used
-
newCompressor
private EmbeddedChannel newCompressor(ChannelHandlerContext ctx, Http2Headers headers, boolean endOfStream) throws Http2Exception
Checks if a new compressor object is needed for the stream identified bystreamId. This method will modify thecontent-encodingheader contained inheaders.- Parameters:
ctx- the context.headers- Object representing headers which are to be writtenendOfStream- Indicates if the stream has ended- Returns:
- The channel used to compress data.
- Throws:
Http2Exception- if any problems occur during initialization.
-
bindCompressorToStream
private void bindCompressorToStream(EmbeddedChannel compressor, int streamId)
Called after the super class has written the headers and created any associated stream objects.- Parameters:
compressor- The compressor associated with the stream identified bystreamId.streamId- The stream id for which the headers were written.
-
cleanup
void cleanup(Http2Stream stream, EmbeddedChannel compressor)
Release remaining content fromEmbeddedChanneland remove the compressor from theHttp2Stream.- Parameters:
stream- The stream for whichcompressoris the compressor forcompressor- The compressor forstream
-
nextReadableBuf
private static ByteBuf nextReadableBuf(EmbeddedChannel compressor)
- Parameters:
compressor- The channel to read from- Returns:
- The next decoded
ByteBuffrom theEmbeddedChannelornullif one does not exist
-
-