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
public class HAProxyMessageDecoder extends ByteToMessageDecoder
Decodes an HAProxy proxy protocol header- See Also:
- Proxy Protocol Specification
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
HAProxyMessageDecoder.HeaderExtractor
HeaderExtractor create a header frame out of theByteBuf
.private class
HAProxyMessageDecoder.LineHeaderExtractor
private class
HAProxyMessageDecoder.StructHeaderExtractor
-
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
Fields Modifier and Type Field Description private static int
BINARY_PREFIX_LENGTH
Binary header prefix lengthprivate static ProtocolDetectionResult<HAProxyProtocolVersion>
DETECTION_RESULT_V1
private static ProtocolDetectionResult<HAProxyProtocolVersion>
DETECTION_RESULT_V2
private int
discardedBytes
Number of discarded bytesprivate boolean
discarding
true
if we're discarding input because we're already over maxLengthprivate 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 headerprivate HAProxyMessageDecoder.HeaderExtractor
headerExtractor
Used to extract a header frame out of theByteBuf
and return it.private static int
V1_MAX_LENGTH
Maximum possible length of a v1 proxy protocol header per specprivate static int
V2_MAX_LENGTH
Maximum possible length of a v2 proxy protocol header (fixed 16 bytes + max unsigned short)private static int
V2_MAX_TLV
Maximum possible length for v2 additional TLV data (max unsigned short - max v2 address info space)private static int
V2_MIN_LENGTH
Minimum possible length of a fully functioning v2 proxy protocol header (fixed 16 bytes + v2 address info space)private 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 sizeprivate int
version
Protocol specification version-
Fields inherited from class io.netty.handler.codec.ByteToMessageDecoder
COMPOSITE_CUMULATOR, MERGE_CUMULATOR
-
-
Constructor Summary
Constructors Constructor Description HAProxyMessageDecoder()
Creates 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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
channelRead(ChannelHandlerContext ctx, java.lang.Object msg)
CallsChannelHandlerContext.fireChannelRead(Object)
to forward to the nextChannelInboundHandler
in theChannelPipeline
.protected void
decode(ChannelHandlerContext ctx, ByteBuf in, java.util.List<java.lang.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.static ProtocolDetectionResult<HAProxyProtocolVersion>
detectProtocol(ByteBuf buffer)
Returns theProtocolDetectionResult
for the givenByteBuf
.private void
fail(ChannelHandlerContext ctx, java.lang.String errMsg, java.lang.Exception e)
private void
failOverLimit(ChannelHandlerContext ctx, int length)
private void
failOverLimit(ChannelHandlerContext ctx, java.lang.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
isSingleDecode()
Iftrue
then only one message is decoded on eachByteToMessageDecoder.channelRead(ChannelHandlerContext, Object)
call.private static boolean
match(byte[] prefix, ByteBuf buffer, int idx)
-
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 Detail
-
V1_MAX_LENGTH
private static final int V1_MAX_LENGTH
Maximum possible length of a v1 proxy protocol header per spec- See Also:
- Constant Field Values
-
V2_MAX_LENGTH
private static final int V2_MAX_LENGTH
Maximum possible length of a v2 proxy protocol header (fixed 16 bytes + max unsigned short)- See Also:
- Constant Field Values
-
V2_MIN_LENGTH
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)- See Also:
- Constant Field Values
-
V2_MAX_TLV
private static final int V2_MAX_TLV
Maximum possible length for v2 additional TLV data (max unsigned short - max v2 address info space)- See Also:
- Constant Field Values
-
BINARY_PREFIX_LENGTH
private static final int BINARY_PREFIX_LENGTH
Binary header prefix length
-
DETECTION_RESULT_V1
private static final ProtocolDetectionResult<HAProxyProtocolVersion> DETECTION_RESULT_V1
-
DETECTION_RESULT_V2
private static final ProtocolDetectionResult<HAProxyProtocolVersion> DETECTION_RESULT_V2
-
headerExtractor
private HAProxyMessageDecoder.HeaderExtractor headerExtractor
Used to extract a header frame out of theByteBuf
and return it.
-
discarding
private boolean discarding
true
if we're discarding input because we're already over maxLength
-
discardedBytes
private int discardedBytes
Number of discarded bytes
-
failFast
private final boolean failFast
Whether or not to throw an exception as soon as we exceed maxLength.
-
finished
private boolean finished
true
if we're finished decoding the proxy protocol header
-
version
private int version
Protocol specification version
-
v2MaxHeaderSize
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
-
-
Constructor Detail
-
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 Detail
-
findVersion
private static int findVersion(ByteBuf 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.
-
findEndOfHeader
private static int findEndOfHeader(ByteBuf 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.
-
findEndOfLine
private static int findEndOfLine(ByteBuf 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.
-
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
public void channelRead(ChannelHandlerContext ctx, java.lang.Object msg) throws java.lang.Exception
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:
java.lang.Exception
-
decode
protected final void decode(ChannelHandlerContext ctx, ByteBuf in, java.util.List<java.lang.Object> out) throws java.lang.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:
java.lang.Exception
- is thrown if an error occurs
-
decodeStruct
private ByteBuf decodeStruct(ChannelHandlerContext ctx, ByteBuf buffer) throws java.lang.Exception
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:
java.lang.Exception
-
decodeLine
private ByteBuf decodeLine(ChannelHandlerContext ctx, ByteBuf buffer) throws java.lang.Exception
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:
java.lang.Exception
-
failOverLimit
private void failOverLimit(ChannelHandlerContext ctx, int length)
-
failOverLimit
private void failOverLimit(ChannelHandlerContext ctx, java.lang.String length)
-
fail
private void fail(ChannelHandlerContext ctx, java.lang.String errMsg, java.lang.Exception e)
-
detectProtocol
public static ProtocolDetectionResult<HAProxyProtocolVersion> detectProtocol(ByteBuf buffer)
Returns theProtocolDetectionResult
for the givenByteBuf
.
-
match
private static boolean match(byte[] prefix, ByteBuf buffer, int idx)
-
-