Top Description Inners Fields Constructors Methods
io.netty5.handler.codec.http

public abstract Class HttpObjectDecoder

extends ByteToMessageDecoder
Class Inheritance
Known Direct Subclasses
io.netty5.handler.codec.http.HttpRequestDecoder, io.netty5.handler.codec.http.HttpResponseDecoder, io.netty5.handler.codec.rtsp.RtspDecoder
Imports
io.netty5.buffer.Buffer, .BufferAllocator, io.netty5.channel.ChannelHandlerContext, .ChannelPipeline, io.netty5.handler.codec.ByteToMessageDecoder, .DecoderResult, .PrematureChannelClosureException, .TooLongFrameException, io.netty5.handler.codec.http.headers.HttpHeaders, io.netty5.util.AsciiString, .ByteProcessor, io.netty5.util.internal.AppendableCharSequence, java.util.Iterator

Decodes Buffers into HttpMessages and HttpContents.

Parameters that prevents excessive memory consumption

NameDefault valueMeaning
maxInitialLineLength 4096 The maximum length of the initial line (e.g. "GET / HTTP/1.0" or "HTTP/1.0 200 OK") If the length of the initial line exceeds this value, a TooLongHttpLineException will be raised.
maxHeaderSize 8192 The maximum length of all headers. If the sum of the length of each header exceeds this value, a TooLongHttpHeaderException will be raised.

Parameters that control parsing behavior

NameDefault valueMeaning
allowDuplicateContentLengths false When set to false, will reject any messages that contain multiple Content-Length header fields. When set to true, will allow multiple Content-Length headers only if they are all the same decimal value. The duplicated field-values will be replaced with a single valid Content-Length field. See RFC 7230, Section 3.3.2.

Chunked Content

If the content of an HTTP message is greater than maxChunkSize or the transfer encoding of the HTTP message is 'chunked', this decoder generates one HttpMessage instance and its following HttpContents per single HTTP message to avoid excessive memory consumption. For example, the following HTTP message:
GET / HTTP/1.1
Transfer-Encoding: chunked

1a
abcdefghijklmnopqrstuvwxyz
10
1234567890abcdef
0
Content-MD5: ...
[blank line]
triggers HttpRequestDecoder to generate 3 objects:
  1. An HttpRequest,
  2. The first HttpContent whose content is 'abcdefghijklmnopqrstuvwxyz',
  3. The second LastHttpContent whose content is '1234567890abcdef', which marks the end of the content.
If you prefer not to handle HttpContents by yourself for your convenience, insert HttpObjectAggregator after this decoder in the ChannelPipeline. However, please note that your server might not be as memory efficient as without the aggregator.

Extensibility

Please note that this decoder is designed to be extended to implement a protocol derived from HTTP, such as RTSP and ICAP. To implement the decoder of such a derived protocol, extend this class and implement all abstract methods properly.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private class
private static enum

Field Summary

Modifier and TypeField and Description
private final boolean
private final boolean
private long
private long
private HttpObjectDecoder.State
public static final boolean
public static final boolean
public static final int
public static final int
public static final int
public static final boolean
private static final String
private final HttpObjectDecoder.HeaderParser
private final HttpObjectDecoder.LineParser
private HttpMessage
private CharSequence
private volatile boolean
private LastHttpContent<?>
protected final boolean
private CharSequence
Inherited from io.netty5.handler.codec.ByteToMessageDecoder:
COMPOSITE_CUMULATORMERGE_CUMULATOR

Constructor Summary

AccessConstructor and Description
protected
HttpObjectDecoder()

Creates a new instance with the default maxInitialLineLength (4096}, maxHeaderSize (8192), and maxChunkSize (8192).

protected
HttpObjectDecoder(int maxInitialLineLength, int maxHeaderSize, boolean chunkedSupported)

Creates a new instance with the specified parameters.

protected
HttpObjectDecoder(int maxInitialLineLength, int maxHeaderSize, boolean chunkedSupported, boolean validateHeaders)

Creates a new instance with the specified parameters.

protected
HttpObjectDecoder(int maxInitialLineLength, int maxHeaderSize, boolean chunkedSupported, boolean validateHeaders, int initialBufferSize)

Creates a new instance with the specified parameters.

protected
HttpObjectDecoder(int maxInitialLineLength, int maxHeaderSize, boolean chunkedSupported, boolean validateHeaders, int initialBufferSize, boolean allowDuplicateContentLengths)

Method Summary

Modifier and TypeMethod and Description
public void
channelInboundEvent(ChannelHandlerContext ctx, Object evt)

Overrides default io.netty5.channel.ChannelHandler.channelInboundEvent.

Gets called if a custom inbound event happened.
private long
protected abstract HttpMessage
protected abstract HttpMessage
createMessage(String[] initialLine)

protected void
decode(ChannelHandlerContext ctx, Buffer
the Buffer from which to read data
buffer
)

Implements abstract io.netty5.handler.codec.ByteToMessageDecoder.decode.

Decode the from one Buffer to another.
protected void
private static int
private static int
private static int
private static int
private static int
protected void
handleTransferEncodingChunkedWithContentLength(HttpMessage message)

Invoked when a message with both a "Transfer-Encoding: chunked" and a "Content-Length" header field is detected.

private HttpContent<?>
private HttpMessage
private HttpMessage
protected boolean
protected abstract boolean
private static boolean
isOWS(char ch)

private static boolean
isSPLenient(char c)

protected boolean
isSwitchingToNonHttp1Protocol(HttpResponse msg)

Returns true if the server switched to a different protocol than HTTP/1.0 or HTTP/1.1, e.g. HTTP/2 or Websocket.

private HttpObjectDecoder.State
private LastHttpContent<?>
public void
reset()

Resets the state of the decoder so that it is ready to decode a new message.

private void
private void
private static String[]
Inherited from io.netty5.handler.codec.ByteToMessageDecoder:
actualReadableByteschannelInactivechannelReadchannelReadCompletechannelShutdowndiscardSomeReadByteshandlerAddedhandlerAdded0handlerRemovedhandlerRemoved0internalBufferisSharableisSingleDecodesetSingleDecode

Field Detail

allowDuplicateContentLengthsback to summary
private final boolean allowDuplicateContentLengths
chunkedSupportedback to summary
private final boolean chunkedSupported
chunkSizeback to summary
private long chunkSize
contentLengthback to summary
private long contentLength
currentStateback to summary
private HttpObjectDecoder.State currentState
DEFAULT_ALLOW_DUPLICATE_CONTENT_LENGTHSback to summary
public static final boolean DEFAULT_ALLOW_DUPLICATE_CONTENT_LENGTHS
DEFAULT_CHUNKED_SUPPORTEDback to summary
public static final boolean DEFAULT_CHUNKED_SUPPORTED
DEFAULT_INITIAL_BUFFER_SIZEback to summary
public static final int DEFAULT_INITIAL_BUFFER_SIZE
DEFAULT_MAX_HEADER_SIZEback to summary
public static final int DEFAULT_MAX_HEADER_SIZE
DEFAULT_MAX_INITIAL_LINE_LENGTHback to summary
public static final int DEFAULT_MAX_INITIAL_LINE_LENGTH
DEFAULT_VALIDATE_HEADERSback to summary
public static final boolean DEFAULT_VALIDATE_HEADERS
EMPTY_VALUEback to summary
private static final String EMPTY_VALUE
headerParserback to summary
private final HttpObjectDecoder.HeaderParser headerParser
lineParserback to summary
private final HttpObjectDecoder.LineParser lineParser
messageback to summary
private HttpMessage message
nameback to summary
private CharSequence name
resetRequestedback to summary
private volatile boolean resetRequested
trailerback to summary
private LastHttpContent<?> trailer
validateHeadersback to summary
protected final boolean validateHeaders
valueback to summary
private CharSequence value

Constructor Detail

HttpObjectDecoderback to summary
protected HttpObjectDecoder()

Creates a new instance with the default maxInitialLineLength (4096}, maxHeaderSize (8192), and maxChunkSize (8192).

HttpObjectDecoderback to summary
protected HttpObjectDecoder(int maxInitialLineLength, int maxHeaderSize, boolean chunkedSupported)

Creates a new instance with the specified parameters.

HttpObjectDecoderback to summary
protected HttpObjectDecoder(int maxInitialLineLength, int maxHeaderSize, boolean chunkedSupported, boolean validateHeaders)

Creates a new instance with the specified parameters.

HttpObjectDecoderback to summary
protected HttpObjectDecoder(int maxInitialLineLength, int maxHeaderSize, boolean chunkedSupported, boolean validateHeaders, int initialBufferSize)

Creates a new instance with the specified parameters.

HttpObjectDecoderback to summary
protected HttpObjectDecoder(int maxInitialLineLength, int maxHeaderSize, boolean chunkedSupported, boolean validateHeaders, int initialBufferSize, boolean allowDuplicateContentLengths)

Method Detail

channelInboundEventback to summary
public void channelInboundEvent(ChannelHandlerContext ctx, Object evt) throws Exception

Overrides default io.netty5.channel.ChannelHandler.channelInboundEvent.

Doc from io.netty5.channel.ChannelHandler.channelInboundEvent.

Gets called if a custom inbound event happened.

Annotations
@Override
contentLengthback to summary
private long contentLength()
createInvalidMessageback to summary
protected abstract HttpMessage createInvalidMessage(ChannelHandlerContext ctx)
createMessageback to summary
protected abstract HttpMessage createMessage(String[] initialLine) throws Exception
decodeback to summary
protected void decode(ChannelHandlerContext ctx, Buffer buffer) throws Exception

Implements abstract io.netty5.handler.codec.ByteToMessageDecoder.decode.

Doc from io.netty5.handler.codec.ByteToMessageDecoder.decode.

Decode the from one Buffer to another. This method will be called till either the input Buffer has nothing to read when return from this method or till nothing was read from the input Buffer.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext which this ByteToMessageDecoder belongs to

buffer:Buffer

the Buffer from which to read data

Annotations
@Override
Exceptions
Exception:
is thrown if an error occurs
decodeLastback to summary
protected void decodeLast(ChannelHandlerContext ctx, Buffer in) throws Exception

Overrides io.netty5.handler.codec.ByteToMessageDecoder.decodeLast.

Doc from io.netty5.handler.codec.ByteToMessageDecoder.decodeLast.

Is called one last time when the ChannelHandlerContext goes in-active. Which means the channelInactive(ChannelHandlerContext) was triggered. By default this will just call decode(ChannelHandlerContext, Buffer) but sub-classes may override this for some special cleanup operation.

Annotations
@Override
findEndOfStringback to summary
private static int findEndOfString(AppendableCharSequence sb)
findNonSPLenientback to summary
private static int findNonSPLenient(AppendableCharSequence sb, int offset)
findNonWhitespaceback to summary
private static int findNonWhitespace(AppendableCharSequence sb, int offset)
findSPLenientback to summary
private static int findSPLenient(AppendableCharSequence sb, int offset)
getChunkSizeback to summary
private static int getChunkSize(String hex)
handleTransferEncodingChunkedWithContentLengthback to summary
protected void handleTransferEncodingChunkedWithContentLength(HttpMessage message)

Invoked when a message with both a "Transfer-Encoding: chunked" and a "Content-Length" header field is detected. The default behavior is to remove the Content-Length field, but this method could be overridden to change the behavior (to, e.g., throw an exception and produce an invalid message).

See: RFC 7230 section-3.3.3:

    If a message is received with both a Transfer-Encoding and a
    Content-Length header field, the Transfer-Encoding overrides the
    Content-Length.  Such a message might indicate an attempt to
    perform request smuggling (Section 9.5) or response splitting
    (Section 9.4) and ought to be handled as an error.  A sender MUST
    remove the received Content-Length field prior to forwarding such
    a message downstream.
Also see: https://github.com/apache/tomcat/blob/b693d7c1981/java/org/apache/coyote/http11/Http11Processor.java#L747-L755 https://github.com/nginx/nginx/blob/0ad4393e30c11/src/http/ngx_http_request.c#L1946-L1953
invalidChunkback to summary
private HttpContent<?> invalidChunk(BufferAllocator allocator, Buffer in, Exception cause)
invalidMessageback to summary
private HttpMessage invalidMessage(ChannelHandlerContext ctx, Buffer in, Exception cause)
invalidMessageback to summary
private HttpMessage invalidMessage(ChannelHandlerContext ctx, Exception cause)
isContentAlwaysEmptyback to summary
protected boolean isContentAlwaysEmpty(HttpMessage msg)
isDecodingRequestback to summary
protected abstract boolean isDecodingRequest()
isOWSback to summary
private static boolean isOWS(char ch)
isSPLenientback to summary
private static boolean isSPLenient(char c)
isSwitchingToNonHttp1Protocolback to summary
protected boolean isSwitchingToNonHttp1Protocol(HttpResponse msg)

Returns true if the server switched to a different protocol than HTTP/1.0 or HTTP/1.1, e.g. HTTP/2 or Websocket. Returns false if the upgrade happened in a different layer, e.g. upgrade from HTTP/1.1 to HTTP/1.1 over TLS.

readHeadersback to summary
private HttpObjectDecoder.State readHeaders(Buffer buffer)
readTrailingHeadersback to summary
private LastHttpContent<?> readTrailingHeaders(BufferAllocator allocator, Buffer buffer)
resetback to summary
public void reset()

Resets the state of the decoder so that it is ready to decode a new message. This method is useful for handling a rejected request with Expect: 100-continue header.

resetNowback to summary
private void resetNow()
splitHeaderback to summary
private void splitHeader(AppendableCharSequence sb)
splitInitialLineback to summary
private static String[] splitInitialLine(AppendableCharSequence sb)
io.netty5.handler.codec.http back to summary

private Class HttpObjectDecoder.HeaderParser

extends Object
implements ByteProcessor
Class Inheritance
All Implemented Interfaces
io.netty5.util.ByteProcessor
Known Direct Subclasses
io.netty5.handler.codec.http.HttpObjectDecoder.LineParser

Field Summary

Modifier and TypeField and Description
private final int
private final AppendableCharSequence
pack-priv int

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
protected final void
protected TooLongFrameException
newException(int maxLength)

public AppendableCharSequence
parse(Buffer buffer)

public boolean
public void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

maxLengthback to summary
private final int maxLength
seqback to summary
private final AppendableCharSequence seq
sizeback to summary
pack-priv int size

Constructor Detail

HeaderParserback to summary
pack-priv HeaderParser(AppendableCharSequence seq, int maxLength)

Method Detail

increaseCountback to summary
protected final void increaseCount()
newExceptionback to summary
protected TooLongFrameException newException(int maxLength)
parseback to summary
public AppendableCharSequence parse(Buffer buffer)
processback to summary
public boolean process(byte value)

Implements io.netty5.util.ByteProcessor.process.

Returns:boolean

Doc from io.netty5.util.ByteProcessor.process.

true if the processor wants to continue the loop and handle the next byte in the buffer. false if the processor wants to stop handling bytes and abort the loop.

Annotations
@Override
resetback to summary
public void reset()
io.netty5.handler.codec.http back to summary

private final Class HttpObjectDecoder.LineParser

extends HeaderParser
Class Inheritance

Field Summary

Inherited from io.netty5.handler.codec.http.HttpObjectDecoder.HeaderParser:
size

Constructor Summary

AccessConstructor and Description
pack-priv
LineParser(AppendableCharSequence seq, int maxLength)

Method Summary

Modifier and TypeMethod and Description
protected TooLongFrameException
public AppendableCharSequence
public boolean
Inherited from io.netty5.handler.codec.http.HttpObjectDecoder.HeaderParser:
increaseCountreset

Constructor Detail

LineParserback to summary
pack-priv LineParser(AppendableCharSequence seq, int maxLength)

Method Detail

newExceptionback to summary
protected TooLongFrameException newException(int maxLength)

Overrides io.netty5.handler.codec.http.HttpObjectDecoder.HeaderParser.newException.

Annotations
@Override
parseback to summary
public AppendableCharSequence parse(Buffer buffer)

Overrides io.netty5.handler.codec.http.HttpObjectDecoder.HeaderParser.parse.

Annotations
@Override
processback to summary
public boolean process(byte value)

Overrides io.netty5.handler.codec.http.HttpObjectDecoder.HeaderParser.process.

Implements io.netty5.util.ByteProcessor.process.

Returns:boolean

Doc from io.netty5.util.ByteProcessor.process.

true if the processor wants to continue the loop and handle the next byte in the buffer. false if the processor wants to stop handling bytes and abort the loop.

Annotations
@Override
io.netty5.handler.codec.http back to summary

private final Enum HttpObjectDecoder.State

extends Enum<HttpObjectDecoder.State>
Class Inheritance

The internal state of HttpObjectDecoder. Internal use only.

Field Summary

Modifier and TypeField and Description
public static final HttpObjectDecoder.State
public static final HttpObjectDecoder.State
public static final HttpObjectDecoder.State
public static final HttpObjectDecoder.State
public static final HttpObjectDecoder.State
public static final HttpObjectDecoder.State
public static final HttpObjectDecoder.State
public static final HttpObjectDecoder.State
public static final HttpObjectDecoder.State
public static final HttpObjectDecoder.State
public static final HttpObjectDecoder.State

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static HttpObjectDecoder.State
public static HttpObjectDecoder.State[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

BAD_MESSAGEback to summary
public static final HttpObjectDecoder.State BAD_MESSAGE
READ_CHUNK_DELIMITERback to summary
public static final HttpObjectDecoder.State READ_CHUNK_DELIMITER
READ_CHUNK_SIZEback to summary
public static final HttpObjectDecoder.State READ_CHUNK_SIZE
READ_CHUNKED_CONTENTback to summary
public static final HttpObjectDecoder.State READ_CHUNKED_CONTENT
READ_FIXED_LENGTH_CONTENTback to summary
public static final HttpObjectDecoder.State READ_FIXED_LENGTH_CONTENT
READ_HEADERback to summary
public static final HttpObjectDecoder.State READ_HEADER
READ_INITIALback to summary
public static final HttpObjectDecoder.State READ_INITIAL
READ_VARIABLE_LENGTH_CONTENTback to summary
public static final HttpObjectDecoder.State READ_VARIABLE_LENGTH_CONTENT
SKIP_CONTROL_CHARSback to summary
public static final HttpObjectDecoder.State SKIP_CONTROL_CHARS
UPGRADEDback to summary
public static final HttpObjectDecoder.State UPGRADED

Constructor Detail

Stateback to summary
private State()

Method Detail

valueOfback to summary
public static HttpObjectDecoder.State valueOf(String name)
valuesback to summary
public static HttpObjectDecoder.State[] values()