Package io.netty.handler.codec.http
Class HttpObjectEncoder<H extends HttpMessage>
java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelOutboundHandlerAdapter
io.netty.handler.codec.MessageToMessageEncoder<Object>
io.netty.handler.codec.http.HttpObjectEncoder<H>
- All Implemented Interfaces:
ChannelHandler
,ChannelOutboundHandler
- Direct Known Subclasses:
HttpRequestEncoder
,HttpResponseEncoder
,RtspEncoder
,RtspObjectEncoder
public abstract class HttpObjectEncoder<H extends HttpMessage>
extends MessageToMessageEncoder<Object>
Encodes an
HttpMessage
or an HttpContent
into
a ByteBuf
.
Extensibility
Please note that this encoder is designed to be extended to implement a protocol derived from HTTP, such as RTSP and ICAP. To implement the encoder of such a derived protocol, extend this class and implement all abstract methods properly.-
Nested Class Summary
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final int
private static final ByteBuf
(package private) static final int
private static final float
private static final float
private float
Used to calculate an exponential moving average of the encoded size of the initial line and the headers for a guess for future buffer allocations.private static final int
private static final int
private static final int
private static final int
private int
private static final float
private static final float
private float
Used to calculate an exponential moving average of the encoded size of the trailers for a guess for future buffer allocations.private static final byte[]
private static final ByteBuf
private static final int
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
Returnstrue
if the given message should be handled.private static void
addEncodedLengthHex
(ChannelHandlerContext ctx, long contentLength, List<Object> out) private static boolean
bypassEncoderIfEmpty
(ByteBuf msg, List<Object> out) private static boolean
checkContentState
(int state) protected void
encode
(ChannelHandlerContext ctx, Object msg, List<Object> out) Encode from one message to an other.protected static void
encodeAscii
(String s, ByteBuf buf) Deprecated.private void
encodeByteBufAndTrailers
(int state, ChannelHandlerContext ctx, List<Object> out, ByteBuf content, HttpHeaders trailingHeaders) private void
encodeByteBufContent
(ChannelHandlerContext ctx, ByteBuf content, List<Object> out) private void
encodeByteBufHttpContent
(int state, ChannelHandlerContext ctx, ByteBuf buf, ByteBuf content, HttpHeaders trailingHeaders, List<Object> out) private void
encodeChunkedHttpContent
(ChannelHandlerContext ctx, ByteBuf content, HttpHeaders trailingHeaders, List<Object> out) private static boolean
encodeContentNonChunk
(List<Object> out, ByteBuf buf, ByteBuf content) private static void
encodedChunkedFileRegionContent
(ChannelHandlerContext ctx, FileRegion msg, List<Object> out) private static int
encodeEmptyLastHttpContent
(int state, List<Object> out) private void
encodeFileRegionContent
(ChannelHandlerContext ctx, FileRegion msg, List<Object> out) private void
encodeFullHttpMessage
(ChannelHandlerContext ctx, Object o, List<Object> out) protected void
encodeHeaders
(HttpHeaders headers, ByteBuf buf) Encode theHttpHeaders
into aByteBuf
.private void
encodeHttpContent
(ChannelHandlerContext ctx, HttpContent msg, List<Object> out) private void
encodeHttpMessageLastContent
(ChannelHandlerContext ctx, H m, List<Object> out) private void
encodeHttpMessageNotLastContent
(ChannelHandlerContext ctx, H m, List<Object> out) private ByteBuf
protected abstract void
encodeInitialLine
(ByteBuf buf, H message) private void
encodeJustHttpMessage
(ChannelHandlerContext ctx, H m, List<Object> out) private void
encodeLastHttpContent
(ChannelHandlerContext ctx, LastHttpContent msg, List<Object> out) private void
encodeNotHttpMessageContentTypes
(ChannelHandlerContext ctx, Object msg, List<Object> out) private void
encodeTrailingHeaders
(ChannelHandlerContext ctx, HttpHeaders trailingHeaders, List<Object> out) protected boolean
isContentAlwaysEmpty
(H msg) Determine whether a message has a content or not.private static int
padSizeForAccumulation
(int readableBytes) Add some additional overhead to the buffer.protected void
sanitizeHeadersBeforeEncode
(H msg, boolean isAlwaysEmpty) Allows to sanitize headers of the message before encoding these.private static void
throwUnexpectedMessageTypeEx
(Object msg, int state) void
write
(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) CallsChannelOutboundInvoker.write(Object, ChannelPromise)
to forward to the nextChannelOutboundHandler
in theChannelPipeline
.private static void
writeOutList
(ChannelHandlerContext ctx, List<Object> out, ChannelPromise promise) private static void
writePromiseCombiner
(ChannelHandlerContext ctx, List<Object> out, ChannelPromise promise) private static void
writeVoidPromise
(ChannelHandlerContext ctx, List<Object> out) Methods inherited from class io.netty.channel.ChannelOutboundHandlerAdapter
bind, close, connect, deregister, disconnect, flush, read
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, exceptionCaught, handlerAdded, handlerRemoved, isSharable
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.netty.channel.ChannelHandler
exceptionCaught, handlerAdded, handlerRemoved
-
Field Details
-
COPY_CONTENT_THRESHOLD
private static final int COPY_CONTENT_THRESHOLD- See Also:
-
CRLF_SHORT
static final int CRLF_SHORT- See Also:
-
ZERO_CRLF_MEDIUM
private static final int ZERO_CRLF_MEDIUM- See Also:
-
ZERO_CRLF_CRLF
private static final byte[] ZERO_CRLF_CRLF -
CRLF_BUF
-
ZERO_CRLF_CRLF_BUF
-
HEADERS_WEIGHT_NEW
private static final float HEADERS_WEIGHT_NEW- See Also:
-
HEADERS_WEIGHT_HISTORICAL
private static final float HEADERS_WEIGHT_HISTORICAL- See Also:
-
TRAILERS_WEIGHT_NEW
private static final float TRAILERS_WEIGHT_NEW- See Also:
-
TRAILERS_WEIGHT_HISTORICAL
private static final float TRAILERS_WEIGHT_HISTORICAL- See Also:
-
ST_INIT
private static final int ST_INIT- See Also:
-
ST_CONTENT_NON_CHUNK
private static final int ST_CONTENT_NON_CHUNK- See Also:
-
ST_CONTENT_CHUNK
private static final int ST_CONTENT_CHUNK- See Also:
-
ST_CONTENT_ALWAYS_EMPTY
private static final int ST_CONTENT_ALWAYS_EMPTY- See Also:
-
state
private int state -
headersEncodedSizeAccumulator
private float headersEncodedSizeAccumulatorUsed to calculate an exponential moving average of the encoded size of the initial line and the headers for a guess for future buffer allocations. -
trailersEncodedSizeAccumulator
private float trailersEncodedSizeAccumulatorUsed to calculate an exponential moving average of the encoded size of the trailers for a guess for future buffer allocations. -
out
-
-
Constructor Details
-
HttpObjectEncoder
public HttpObjectEncoder()
-
-
Method Details
-
checkContentState
private static boolean checkContentState(int state) -
write
Description copied from class:ChannelOutboundHandlerAdapter
CallsChannelOutboundInvoker.write(Object, ChannelPromise)
to forward to the nextChannelOutboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
write
in interfaceChannelOutboundHandler
- Overrides:
write
in classMessageToMessageEncoder<Object>
- Parameters:
ctx
- theChannelHandlerContext
for which the write operation is mademsg
- the message to writepromise
- theChannelPromise
to notify once the operation completes- Throws:
Exception
- thrown if an error occurs
-
writeOutList
private static void writeOutList(ChannelHandlerContext ctx, List<Object> out, ChannelPromise promise) -
writeVoidPromise
-
writePromiseCombiner
private static void writePromiseCombiner(ChannelHandlerContext ctx, List<Object> out, ChannelPromise promise) -
encode
Description copied from class:MessageToMessageEncoder
Encode from one message to an other. This method will be called for each written message that can be handled by this encoder.- Specified by:
encode
in classMessageToMessageEncoder<Object>
- Parameters:
ctx
- theChannelHandlerContext
which thisMessageToMessageEncoder
belongs tomsg
- the message to encode to an other oneout
- theList
into which the encoded msg should be added needs to do some kind of aggregation- Throws:
Exception
- is thrown if an error occurs
-
encodeJustHttpMessage
private void encodeJustHttpMessage(ChannelHandlerContext ctx, H m, List<Object> out) throws Exception - Throws:
Exception
-
encodeByteBufHttpContent
private void encodeByteBufHttpContent(int state, ChannelHandlerContext ctx, ByteBuf buf, ByteBuf content, HttpHeaders trailingHeaders, List<Object> out) -
encodeHttpMessageNotLastContent
private void encodeHttpMessageNotLastContent(ChannelHandlerContext ctx, H m, List<Object> out) throws Exception - Throws:
Exception
-
encodeHttpMessageLastContent
private void encodeHttpMessageLastContent(ChannelHandlerContext ctx, H m, List<Object> out) throws Exception - Throws:
Exception
-
encodeNotHttpMessageContentTypes
private void encodeNotHttpMessageContentTypes(ChannelHandlerContext ctx, Object msg, List<Object> out) -
encodeFullHttpMessage
private void encodeFullHttpMessage(ChannelHandlerContext ctx, Object o, List<Object> out) throws Exception - Throws:
Exception
-
encodeContentNonChunk
-
throwUnexpectedMessageTypeEx
-
encodeFileRegionContent
-
bypassEncoderIfEmpty
-
encodeByteBufContent
-
encodeEmptyLastHttpContent
-
encodeLastHttpContent
private void encodeLastHttpContent(ChannelHandlerContext ctx, LastHttpContent msg, List<Object> out) -
encodeHttpContent
-
encodeByteBufAndTrailers
private void encodeByteBufAndTrailers(int state, ChannelHandlerContext ctx, List<Object> out, ByteBuf content, HttpHeaders trailingHeaders) -
encodeChunkedHttpContent
private void encodeChunkedHttpContent(ChannelHandlerContext ctx, ByteBuf content, HttpHeaders trailingHeaders, List<Object> out) -
encodeTrailingHeaders
private void encodeTrailingHeaders(ChannelHandlerContext ctx, HttpHeaders trailingHeaders, List<Object> out) -
encodeInitHttpMessage
- Throws:
Exception
-
encodeHeaders
Encode theHttpHeaders
into aByteBuf
. -
encodedChunkedFileRegionContent
private static void encodedChunkedFileRegionContent(ChannelHandlerContext ctx, FileRegion msg, List<Object> out) -
addEncodedLengthHex
private static void addEncodedLengthHex(ChannelHandlerContext ctx, long contentLength, List<Object> out) -
sanitizeHeadersBeforeEncode
Allows to sanitize headers of the message before encoding these. -
isContentAlwaysEmpty
Determine whether a message has a content or not. Some message may have headers indicating a content without having an actual content, e.g the response to an HEAD or CONNECT request.- Parameters:
msg
- the message to test- Returns:
true
to signal the message has no content
-
acceptOutboundMessage
Description copied from class:MessageToMessageEncoder
Returnstrue
if the given message should be handled. Iffalse
it will be passed to the nextChannelOutboundHandler
in theChannelPipeline
.- Overrides:
acceptOutboundMessage
in classMessageToMessageEncoder<Object>
- Throws:
Exception
-
padSizeForAccumulation
private static int padSizeForAccumulation(int readableBytes) Add some additional overhead to the buffer. The rational is that it is better to slightly over allocate and waste some memory, rather than under allocate and require a resize/copy.- Parameters:
readableBytes
- The readable bytes in the buffer.- Returns:
- The
readableBytes
with some additional padding.
-
encodeAscii
Deprecated. -
encodeInitialLine
- Throws:
Exception
-