Package io.netty.handler.codec.haproxy
Class HAProxyMessageDecoder
java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelInboundHandlerAdapter
io.netty.handler.codec.ByteToMessageDecoder
io.netty.handler.codec.haproxy.HAProxyMessageDecoder
- All Implemented Interfaces:
ChannelHandler
,ChannelInboundHandler
Decodes an HAProxy proxy protocol header
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate class
HeaderExtractor create a header frame out of theByteBuf
.private final class
private final class
Nested classes/interfaces inherited from class io.netty.handler.codec.ByteToMessageDecoder
ByteToMessageDecoder.Cumulator
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final int
Binary header prefix lengthprivate static final ProtocolDetectionResult
<HAProxyProtocolVersion> private static final ProtocolDetectionResult
<HAProxyProtocolVersion> private int
Number of discarded bytesprivate boolean
true
if we're discarding input because we're already over maxLengthprivate final boolean
Whether or not to throw an exception as soon as we exceed maxLength.private boolean
true
if we're finished decoding the proxy protocol headerUsed to extract a header frame out of theByteBuf
and return it.private static final int
Maximum possible length of a v1 proxy protocol header per specprivate static final int
Maximum possible length of a v2 proxy protocol header (fixed 16 bytes + max unsigned short)private static final int
Maximum possible length for v2 additional TLV data (max unsigned short - max v2 address info space)private static final int
Minimum possible length of a fully functioning v2 proxy protocol header (fixed 16 bytes + v2 address info space)private final int
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 sizeprivate int
Protocol specification versionFields inherited from class io.netty.handler.codec.ByteToMessageDecoder
COMPOSITE_CUMULATOR, MERGE_CUMULATOR
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new decoder with no additional data (TLV) restrictions, and should throw an exception as soon as we exceed maxLength.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.HAProxyMessageDecoder
(int maxTlvSize) Creates a new decoder with restricted additional data (TLV) size, and should throw an exception as soon as we exceed maxLength.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. -
Method Summary
Modifier and TypeMethodDescriptionvoid
channelRead
(ChannelHandlerContext ctx, Object msg) CallsChannelHandlerContext.fireChannelRead(Object)
to forward to the nextChannelInboundHandler
in theChannelPipeline
.protected final void
decode
(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) Decode the from oneByteBuf
to an other.private ByteBuf
decodeLine
(ChannelHandlerContext ctx, ByteBuf buffer) Create a frame out of theByteBuf
and return it.private ByteBuf
decodeStruct
(ChannelHandlerContext ctx, ByteBuf buffer) Create a frame out of theByteBuf
and return it.detectProtocol
(ByteBuf buffer) Returns theProtocolDetectionResult
for the givenByteBuf
.private void
fail
(ChannelHandlerContext ctx, String errMsg, Exception e) private void
failOverLimit
(ChannelHandlerContext ctx, int length) private void
failOverLimit
(ChannelHandlerContext ctx, String length) private static int
findEndOfHeader
(ByteBuf buffer) Returns the index in the buffer of the end of header if found.private static int
findEndOfLine
(ByteBuf buffer) Returns the index in the buffer of the end of line found.private static int
findVersion
(ByteBuf buffer) Returns the proxy protocol specification version in the buffer if the version is found.boolean
Iftrue
then only one message is decoded on eachByteToMessageDecoder.channelRead(ChannelHandlerContext, Object)
call.private static boolean
Methods inherited from class io.netty.handler.codec.ByteToMessageDecoder
actualReadableBytes, callDecode, channelInactive, channelReadComplete, decodeLast, discardSomeReadBytes, handlerRemoved, handlerRemoved0, internalBuffer, setCumulator, setDiscardAfterReads, setSingleDecode, userEventTriggered
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelActive, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerAdded, 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
handlerAdded
-
Field Details
-
V1_MAX_LENGTH
private static final int V1_MAX_LENGTHMaximum possible length of a v1 proxy protocol header per spec- See Also:
-
V2_MAX_LENGTH
private static final int V2_MAX_LENGTHMaximum possible length of a v2 proxy protocol header (fixed 16 bytes + max unsigned short)- See Also:
-
V2_MIN_LENGTH
private static final int V2_MIN_LENGTHMinimum possible length of a fully functioning v2 proxy protocol header (fixed 16 bytes + v2 address info space)- See Also:
-
V2_MAX_TLV
private static final int V2_MAX_TLVMaximum possible length for v2 additional TLV data (max unsigned short - max v2 address info space)- See Also:
-
BINARY_PREFIX_LENGTH
private static final int BINARY_PREFIX_LENGTHBinary header prefix length -
DETECTION_RESULT_V1
-
DETECTION_RESULT_V2
-
headerExtractor
Used to extract a header frame out of theByteBuf
and return it. -
discarding
private boolean discardingtrue
if we're discarding input because we're already over maxLength -
discardedBytes
private int discardedBytesNumber of discarded bytes -
failFast
private final boolean failFastWhether or not to throw an exception as soon as we exceed maxLength. -
finished
private boolean finishedtrue
if we're finished decoding the proxy protocol header -
version
private int versionProtocol specification version -
v2MaxHeaderSize
private final int v2MaxHeaderSizeThe 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
-
-
Constructor Details
-
HAProxyMessageDecoder
public HAProxyMessageDecoder()Creates a new decoder with no additional data (TLV) restrictions, and should throw an exception as soon as we exceed maxLength. -
HAProxyMessageDecoder
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
- Whether or not to throw an exception as soon as we exceed maxLength
-
HAProxyMessageDecoder
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
- maximum number of bytes allowed for additional data (Type-Length-Value vectors) in a v2 header
-
HAProxyMessageDecoder
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
- maximum number of bytes allowed for additional data (Type-Length-Value vectors) in a v2 headerfailFast
- Whether or not to throw an exception as soon as we exceed maxLength
-
-
Method Details
-
findVersion
Returns the proxy protocol specification version in the buffer if the version is found. Returns -1 if no version was found in the buffer. -
findEndOfHeader
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. -
findEndOfLine
Returns the index in the buffer of the end of line found. Returns -1 if no end of line was found in the buffer. -
isSingleDecode
public boolean isSingleDecode()Description copied from class:ByteToMessageDecoder
Iftrue
then only one message is decoded on eachByteToMessageDecoder.channelRead(ChannelHandlerContext, Object)
call. Default isfalse
as this has performance impacts.- Overrides:
isSingleDecode
in classByteToMessageDecoder
-
channelRead
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireChannelRead(Object)
to forward to the nextChannelInboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
channelRead
in interfaceChannelInboundHandler
- Overrides:
channelRead
in classByteToMessageDecoder
- Throws:
Exception
-
decode
protected final void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception Description copied from class:ByteToMessageDecoder
Decode the from oneByteBuf
to an other. This method will be called till either the inputByteBuf
has nothing to read when return from this method or till nothing was read from the inputByteBuf
.- Specified by:
decode
in classByteToMessageDecoder
- Parameters:
ctx
- theChannelHandlerContext
which thisByteToMessageDecoder
belongs toin
- theByteBuf
from which to read dataout
- theList
to which decoded messages should be added- Throws:
Exception
- is thrown if an error occurs
-
decodeStruct
Create a frame out of theByteBuf
and return it.- Parameters:
ctx
- theChannelHandlerContext
which thisHAProxyMessageDecoder
belongs tobuffer
- theByteBuf
from which to read data- Returns:
- frame the
ByteBuf
which represent the frame ornull
if no frame could be created - Throws:
Exception
-
decodeLine
Create a frame out of theByteBuf
and return it.- Parameters:
ctx
- theChannelHandlerContext
which thisHAProxyMessageDecoder
belongs tobuffer
- theByteBuf
from which to read data- Returns:
- frame the
ByteBuf
which represent the frame ornull
if no frame could be created - Throws:
Exception
-
failOverLimit
-
failOverLimit
-
fail
-
detectProtocol
Returns theProtocolDetectionResult
for the givenByteBuf
. -
match
-