Top Description Inners Fields Constructors Methods
io.netty.contrib.handler.codec.haproxy

public Class HAProxyMessageDecoder

extends ByteToMessageDecoder
Class Inheritance
Imports
io.netty5.buffer.Buffer, io.netty5.channel.ChannelHandlerContext, io.netty5.handler.codec.ByteToMessageDecoder, .ProtocolDetectionResult, java.nio.charset.StandardCharsets

Decodes an HAProxy proxy protocol header
See Also
Proxy Protocol Specification

Nested and Inner Type Summary

Modifier and TypeClass and Description
private abstract class
HAProxyMessageDecoder.HeaderExtractor

HeaderExtractor create a header frame out of the Buffer.

private class
private class

Field Summary

Modifier and TypeField and Description
private static final int
BINARY_PREFIX_LENGTH

Binary header prefix length

private static final ProtocolDetectionResult<HAProxyProtocolVersion>
private static final ProtocolDetectionResult<HAProxyProtocolVersion>
private int
discardedBytes

Number of discarded bytes

private boolean
discarding

true if we're discarding input because we're already over maxLength

private final boolean
failFast

Whether or not to throw an exception as soon as we exceed maxLength.

private boolean
finished

true if we're finished decoding the proxy protocol header

private HAProxyMessageDecoder.HeaderExtractor
headerExtractor

Used to extract a header frame out of the Buffer and return it.

private static final int
V1_MAX_LENGTH

Maximum possible length of a v1 proxy protocol header per spec

private static final int
V2_MAX_LENGTH

Maximum possible length of a v2 proxy protocol header (fixed 16 bytes + max unsigned short)

private static final int
V2_MAX_TLV

Maximum possible length for v2 additional TLV data (max unsigned short - max v2 address info space)

private static final int
V2_MIN_LENGTH

Minimum possible length of a fully functioning v2 proxy protocol header (fixed 16 bytes + v2 address info space)

private final int
v2MaxHeaderSize

The latest v2 spec (2014/05/18) allows for additional data to be sent in the proxy protocol header beyond the address information block so now we need a configurable max header size

private int
version

Protocol specification version

Inherited from io.netty5.handler.codec.ByteToMessageDecoder:
COMPOSITE_CUMULATORMERGE_CUMULATOR

Constructor Summary

AccessConstructor and Description
public
HAProxyMessageDecoder()

Creates a new decoder with no additional data (TLV) restrictions, and should throw an exception as soon as we exceed maxLength.

public
HAProxyMessageDecoder(boolean
Whether or not to throw an exception as soon as we exceed maxLength
failFast
)

Creates a new decoder with no additional data (TLV) restrictions, whether or not to throw an exception as soon as we exceed maxLength.

public
HAProxyMessageDecoder(int
maximum number of bytes allowed for additional data (Type-Length-Value vectors) in a v2 header
maxTlvSize
)

Creates a new decoder with restricted additional data (TLV) size, and should throw an exception as soon as we exceed maxLength.

public
HAProxyMessageDecoder(int
maximum number of bytes allowed for additional data (Type-Length-Value vectors) in a v2 header
maxTlvSize
,
boolean
Whether or not to throw an exception as soon as we exceed maxLength
failFast
)

Creates a new decoder with restricted additional data (TLV) size, whether or not to throw an exception as soon as we exceed maxLength.

Method Summary

Modifier and TypeMethod and Description
public void
channelExceptionCaught(ChannelHandlerContext ctx, Throwable cause)

Overrides default io.netty5.channel.ChannelHandler.channelExceptionCaught.

Gets called if a Throwable was thrown when handling inbound events.
public void
channelRead(ChannelHandlerContext ctx, Object msg)

Overrides io.netty5.handler.codec.ByteToMessageDecoder.channelRead.

Overrides default io.netty5.channel.ChannelHandler.channelRead.

Invoked when the current Channel has read a message from the peer.
protected final void
decode(ChannelHandlerContext ctx, Buffer
the Buffer from which to read data
in
)

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

Decode the from one Buffer to another.
private Buffer

Returns:

frame the Buffer which represent the frame or null if no frame could be created
decodeLine
(ChannelHandlerContext ctx, Buffer
the Buffer from which to read data
buffer
)

Create a frame out of the Buffer and return it.

private Buffer

Returns:

frame the Buffer which represent the frame or null if no frame could be created
decodeStruct
(ChannelHandlerContext ctx, Buffer
the Buffer from which to read data
buffer
)

Create a frame out of the Buffer and return it.

public static ProtocolDetectionResult<HAProxyProtocolVersion>
detectProtocol(Buffer buffer)

Returns the ProtocolDetectionResult for the given Buffer.

private void
private void
failOverLimit(final ChannelHandlerContext ctx, int length)

private void
private static int
findEndOfHeader(final Buffer buffer)

Returns the index in the buffer of the end of header if found.

private static int
findEndOfLine(final Buffer buffer)

Returns the index in the buffer of the end of line found.

private static int
findVersion(final Buffer buffer)

Returns the proxy protocol specification version in the buffer if the version is found.

public boolean
private static boolean
match(byte[] prefix, Buffer buffer, int idx)

Inherited from io.netty5.handler.codec.ByteToMessageDecoder:
actualReadableByteschannelInactivechannelReadCompletechannelShutdowndecodeLastdiscardSomeReadByteshandlerAddedhandlerAdded0handlerRemovedhandlerRemoved0internalBufferisSharablesetSingleDecode

Field Detail

BINARY_PREFIX_LENGTHback to summary
private static final int BINARY_PREFIX_LENGTH

Binary header prefix length

DETECTION_RESULT_V1back to summary
private static final ProtocolDetectionResult<HAProxyProtocolVersion> DETECTION_RESULT_V1

ProtocolDetectionResult for HAProxyProtocolVersion#V1.

DETECTION_RESULT_V2back to summary
private static final ProtocolDetectionResult<HAProxyProtocolVersion> DETECTION_RESULT_V2

ProtocolDetectionResult for HAProxyProtocolVersion#V2.

discardedBytesback to summary
private int discardedBytes

Number of discarded bytes

discardingback to summary
private boolean discarding

true if we're discarding input because we're already over maxLength

failFastback to summary
private final boolean failFast

Whether or not to throw an exception as soon as we exceed maxLength.

finishedback to summary
private boolean finished

true if we're finished decoding the proxy protocol header

headerExtractorback to summary
private HAProxyMessageDecoder.HeaderExtractor headerExtractor

Used to extract a header frame out of the Buffer and return it.

V1_MAX_LENGTHback to summary
private static final int V1_MAX_LENGTH

Maximum possible length of a v1 proxy protocol header per spec

V2_MAX_LENGTHback to summary
private static final int V2_MAX_LENGTH

Maximum possible length of a v2 proxy protocol header (fixed 16 bytes + max unsigned short)

V2_MAX_TLVback to summary
private static final int V2_MAX_TLV

Maximum possible length for v2 additional TLV data (max unsigned short - max v2 address info space)

V2_MIN_LENGTHback to summary
private static final int V2_MIN_LENGTH

Minimum possible length of a fully functioning v2 proxy protocol header (fixed 16 bytes + v2 address info space)

v2MaxHeaderSizeback to summary
private final int v2MaxHeaderSize

The latest v2 spec (2014/05/18) allows for additional data to be sent in the proxy protocol header beyond the address information block so now we need a configurable max header size

versionback to summary
private int version

Protocol specification version

Constructor Detail

HAProxyMessageDecoderback to summary
public HAProxyMessageDecoder()

Creates a new decoder with no additional data (TLV) restrictions, and should throw an exception as soon as we exceed maxLength.

HAProxyMessageDecoderback to summary
public HAProxyMessageDecoder(boolean failFast)

Creates a new decoder with no additional data (TLV) restrictions, whether or not to throw an exception as soon as we exceed maxLength.

Parameters
failFast:boolean

Whether or not to throw an exception as soon as we exceed maxLength

HAProxyMessageDecoderback to summary
public HAProxyMessageDecoder(int maxTlvSize)

Creates a new decoder with restricted additional data (TLV) size, and should throw an exception as soon as we exceed maxLength.

Note

limiting TLV size only affects processing of v2, binary headers. Also, as allowed by the 1.5 spec TLV data is currently ignored. For maximum performance it would be best to configure your upstream proxy host to NOT send TLV data and instantiate with a max TLV size of 0.

Parameters
maxTlvSize:int

maximum number of bytes allowed for additional data (Type-Length-Value vectors) in a v2 header

HAProxyMessageDecoderback to summary
public HAProxyMessageDecoder(int maxTlvSize, boolean failFast)

Creates a new decoder with restricted additional data (TLV) size, whether or not to throw an exception as soon as we exceed maxLength.

Parameters
maxTlvSize:int

maximum number of bytes allowed for additional data (Type-Length-Value vectors) in a v2 header

failFast:boolean

Whether or not to throw an exception as soon as we exceed maxLength

Method Detail

channelExceptionCaughtback to summary
public void channelExceptionCaught(ChannelHandlerContext ctx, Throwable cause)

Overrides default io.netty5.channel.ChannelHandler.channelExceptionCaught.

Doc from io.netty5.channel.ChannelHandler.channelExceptionCaught.

Gets called if a Throwable was thrown when handling inbound events.

Annotations
@Override
channelReadback to summary
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception

Overrides io.netty5.handler.codec.ByteToMessageDecoder.channelRead.

Overrides default io.netty5.channel.ChannelHandler.channelRead.

Doc from io.netty5.channel.ChannelHandler.channelRead.

Invoked when the current Channel has read a message from the peer.

Annotations
@Override
decodeback to summary
protected final void decode(ChannelHandlerContext ctx, Buffer in)

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

in:Buffer

the Buffer from which to read data

Annotations
@Override
decodeLineback to summary
private Buffer decodeLine(ChannelHandlerContext ctx, Buffer buffer)

Create a frame out of the Buffer and return it.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext which this HAProxyMessageDecoder belongs to

buffer:Buffer

the Buffer from which to read data

Returns:Buffer

frame the Buffer which represent the frame or null if no frame could be created

decodeStructback to summary
private Buffer decodeStruct(ChannelHandlerContext ctx, Buffer buffer)

Create a frame out of the Buffer and return it.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext which this HAProxyMessageDecoder belongs to

buffer:Buffer

the Buffer from which to read data

Returns:Buffer

frame the Buffer which represent the frame or null if no frame could be created

detectProtocolback to summary
public static ProtocolDetectionResult<HAProxyProtocolVersion> detectProtocol(Buffer buffer)

Returns the ProtocolDetectionResult for the given Buffer.

failback to summary
private void fail(final ChannelHandlerContext ctx, String errMsg, Exception e)
failOverLimitback to summary
private void failOverLimit(final ChannelHandlerContext ctx, int length)
failOverLimitback to summary
private void failOverLimit(final ChannelHandlerContext ctx, String length)
findEndOfHeaderback to summary
private static int findEndOfHeader(final Buffer buffer)

Returns the index in the buffer of the end of header if found. Returns -1 if no end of header was found in the buffer.

findEndOfLineback to summary
private static int findEndOfLine(final Buffer buffer)

Returns the index in the buffer of the end of line found. Returns -1 if no end of line was found in the buffer.

findVersionback to summary
private static int findVersion(final Buffer buffer)

Returns the proxy protocol specification version in the buffer if the version is found. Returns -1 if no version was found in the buffer.

isSingleDecodeback to summary
public boolean isSingleDecode()

Overrides io.netty5.handler.codec.ByteToMessageDecoder.isSingleDecode.

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

If true then only one message is decoded on each channelRead(ChannelHandlerContext, Object) call. Default is false as this has performance impacts.

Annotations
@Override
matchback to summary
private static boolean match(byte[] prefix, Buffer buffer, int idx)
io.netty.contrib.handler.codec.haproxy back to summary

private abstract Class HAProxyMessageDecoder.HeaderExtractor

extends Object
Class Inheritance
  • java.lang.Object
  • io.netty.contrib.handler.codec.haproxy.HAProxyMessageDecoder.HeaderExtractor
Known Direct Subclasses
io.netty.contrib.handler.codec.haproxy.HAProxyMessageDecoder.LineHeaderExtractor, io.netty.contrib.handler.codec.haproxy.HAProxyMessageDecoder.StructHeaderExtractor

HeaderExtractor create a header frame out of the Buffer.

Field Summary

Modifier and TypeField and Description
private final int
maxHeaderSize

Header max size

Constructor Summary

AccessConstructor and Description
protected
HeaderExtractor(int maxHeaderSize)

Method Summary

Modifier and TypeMethod and Description
protected abstract int

Returns:

length of the delimiter
delimiterLength
(Buffer
the buffer where delimiter is located
buffer
,
int
index of delimiter
eoh
)

Get the length of the header delimiter.

public Buffer

Returns:

frame the Buffer which represent the frame or null if no frame could be created
extract
(ChannelHandlerContext ctx, Buffer
the Buffer from which to read data
buffer
)

Create a frame out of the Buffer and return it.

protected abstract int

Returns:

-1 if can not find the end, otherwise return the buffer index of end
findEndOfHeader
(Buffer
the buffer to be searched
buffer
)

Find the end of the header from the given Buffer,the end may be a CRLF, or the length given by the header.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

maxHeaderSizeback to summary
private final int maxHeaderSize

Header max size

Constructor Detail

HeaderExtractorback to summary
protected HeaderExtractor(int maxHeaderSize)

Method Detail

delimiterLengthback to summary
protected abstract int delimiterLength(Buffer buffer, int eoh)

Get the length of the header delimiter.

Parameters
buffer:Buffer

the buffer where delimiter is located

eoh:int

index of delimiter

Returns:int

length of the delimiter

extractback to summary
public Buffer extract(ChannelHandlerContext ctx, Buffer buffer)

Create a frame out of the Buffer and return it.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext which this HAProxyMessageDecoder belongs to

buffer:Buffer

the Buffer from which to read data

Returns:Buffer

frame the Buffer which represent the frame or null if no frame could be created

Exceptions
Exception:
if exceed maxLength
findEndOfHeaderback to summary
protected abstract int findEndOfHeader(Buffer buffer)

Find the end of the header from the given Buffer,the end may be a CRLF, or the length given by the header.

Parameters
buffer:Buffer

the buffer to be searched

Returns:int

-1 if can not find the end, otherwise return the buffer index of end

io.netty.contrib.handler.codec.haproxy back to summary

private final Class HAProxyMessageDecoder.LineHeaderExtractor

extends HeaderExtractor
Class Inheritance

Constructor Summary

AccessConstructor and Description
pack-priv
LineHeaderExtractor(int maxHeaderSize)

Method Summary

Modifier and TypeMethod and Description
protected int
delimiterLength(Buffer
the buffer where delimiter is located
buffer
,
int
index of delimiter
eoh
)

Implements abstract io.netty.contrib.handler.codec.haproxy.HAProxyMessageDecoder.HeaderExtractor.delimiterLength.

Get the length of the header delimiter.
protected int
findEndOfHeader(Buffer
the buffer to be searched
buffer
)

Implements abstract io.netty.contrib.handler.codec.haproxy.HAProxyMessageDecoder.HeaderExtractor.findEndOfHeader.

Find the end of the header from the given Buffer,the end may be a CRLF, or the length given by the header.
Inherited from io.netty.contrib.handler.codec.haproxy.HAProxyMessageDecoder.HeaderExtractor:
extract

Constructor Detail

LineHeaderExtractorback to summary
pack-priv LineHeaderExtractor(int maxHeaderSize)

Method Detail

delimiterLengthback to summary
protected int delimiterLength(Buffer buffer, int eoh)

Implements abstract io.netty.contrib.handler.codec.haproxy.HAProxyMessageDecoder.HeaderExtractor.delimiterLength.

Doc from io.netty.contrib.handler.codec.haproxy.HAProxyMessageDecoder.HeaderExtractor.delimiterLength.

Get the length of the header delimiter.

Parameters
buffer:Buffer

the buffer where delimiter is located

eoh:int

index of delimiter

Returns:int

length of the delimiter

Annotations
@Override
findEndOfHeaderback to summary
protected int findEndOfHeader(Buffer buffer)

Implements abstract io.netty.contrib.handler.codec.haproxy.HAProxyMessageDecoder.HeaderExtractor.findEndOfHeader.

Doc from io.netty.contrib.handler.codec.haproxy.HAProxyMessageDecoder.HeaderExtractor.findEndOfHeader.

Find the end of the header from the given Buffer,the end may be a CRLF, or the length given by the header.

Parameters
buffer:Buffer

the buffer to be searched

Returns:int

-1 if can not find the end, otherwise return the buffer index of end

Annotations
@Override
io.netty.contrib.handler.codec.haproxy back to summary

private final Class HAProxyMessageDecoder.StructHeaderExtractor

extends HeaderExtractor
Class Inheritance

Constructor Summary

AccessConstructor and Description
pack-priv
StructHeaderExtractor(int maxHeaderSize)

Method Summary

Modifier and TypeMethod and Description
protected int
delimiterLength(Buffer
the buffer where delimiter is located
buffer
,
int
index of delimiter
eoh
)

Implements abstract io.netty.contrib.handler.codec.haproxy.HAProxyMessageDecoder.HeaderExtractor.delimiterLength.

Get the length of the header delimiter.
protected int
findEndOfHeader(Buffer
the buffer to be searched
buffer
)

Implements abstract io.netty.contrib.handler.codec.haproxy.HAProxyMessageDecoder.HeaderExtractor.findEndOfHeader.

Find the end of the header from the given Buffer,the end may be a CRLF, or the length given by the header.
Inherited from io.netty.contrib.handler.codec.haproxy.HAProxyMessageDecoder.HeaderExtractor:
extract

Constructor Detail

StructHeaderExtractorback to summary
pack-priv StructHeaderExtractor(int maxHeaderSize)

Method Detail

delimiterLengthback to summary
protected int delimiterLength(Buffer buffer, int eoh)

Implements abstract io.netty.contrib.handler.codec.haproxy.HAProxyMessageDecoder.HeaderExtractor.delimiterLength.

Doc from io.netty.contrib.handler.codec.haproxy.HAProxyMessageDecoder.HeaderExtractor.delimiterLength.

Get the length of the header delimiter.

Parameters
buffer:Buffer

the buffer where delimiter is located

eoh:int

index of delimiter

Returns:int

length of the delimiter

Annotations
@Override
findEndOfHeaderback to summary
protected int findEndOfHeader(Buffer buffer)

Implements abstract io.netty.contrib.handler.codec.haproxy.HAProxyMessageDecoder.HeaderExtractor.findEndOfHeader.

Doc from io.netty.contrib.handler.codec.haproxy.HAProxyMessageDecoder.HeaderExtractor.findEndOfHeader.

Find the end of the header from the given Buffer,the end may be a CRLF, or the length given by the header.

Parameters
buffer:Buffer

the buffer to be searched

Returns:int

-1 if can not find the end, otherwise return the buffer index of end

Annotations
@Override