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 classHAProxyMessageDecoder.HeaderExtractorHeaderExtractor create a header frame out of theByteBuf.private classHAProxyMessageDecoder.LineHeaderExtractorprivate classHAProxyMessageDecoder.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 intBINARY_PREFIX_LENGTHBinary header prefix lengthprivate static ProtocolDetectionResult<HAProxyProtocolVersion>DETECTION_RESULT_V1private static ProtocolDetectionResult<HAProxyProtocolVersion>DETECTION_RESULT_V2private intdiscardedBytesNumber of discarded bytesprivate booleandiscardingtrueif we're discarding input because we're already over maxLengthprivate booleanfailFastWhether or not to throw an exception as soon as we exceed maxLength.private booleanfinishedtrueif we're finished decoding the proxy protocol headerprivate HAProxyMessageDecoder.HeaderExtractorheaderExtractorUsed to extract a header frame out of theByteBufand return it.private static intV1_MAX_LENGTHMaximum possible length of a v1 proxy protocol header per specprivate static intV2_MAX_LENGTHMaximum possible length of a v2 proxy protocol header (fixed 16 bytes + max unsigned short)private static intV2_MAX_TLVMaximum possible length for v2 additional TLV data (max unsigned short - max v2 address info space)private static intV2_MIN_LENGTHMinimum possible length of a fully functioning v2 proxy protocol header (fixed 16 bytes + v2 address info space)private intv2MaxHeaderSizeThe 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 intversionProtocol 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 voidchannelRead(ChannelHandlerContext ctx, java.lang.Object msg)CallsChannelHandlerContext.fireChannelRead(Object)to forward to the nextChannelInboundHandlerin theChannelPipeline.protected voiddecode(ChannelHandlerContext ctx, ByteBuf in, java.util.List<java.lang.Object> out)Decode the from oneByteBufto an other.private ByteBufdecodeLine(ChannelHandlerContext ctx, ByteBuf buffer)Create a frame out of theByteBufand return it.private ByteBufdecodeStruct(ChannelHandlerContext ctx, ByteBuf buffer)Create a frame out of theByteBufand return it.static ProtocolDetectionResult<HAProxyProtocolVersion>detectProtocol(ByteBuf buffer)Returns theProtocolDetectionResultfor the givenByteBuf.private voidfail(ChannelHandlerContext ctx, java.lang.String errMsg, java.lang.Exception e)private voidfailOverLimit(ChannelHandlerContext ctx, int length)private voidfailOverLimit(ChannelHandlerContext ctx, java.lang.String length)private static intfindEndOfHeader(ByteBuf buffer)Returns the index in the buffer of the end of header if found.private static intfindEndOfLine(ByteBuf buffer)Returns the index in the buffer of the end of line found.private static intfindVersion(ByteBuf buffer)Returns the proxy protocol specification version in the buffer if the version is found.booleanisSingleDecode()Iftruethen only one message is decoded on eachByteToMessageDecoder.channelRead(ChannelHandlerContext, Object)call.private static booleanmatch(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 theByteBufand return it.
-
discarding
private boolean discarding
trueif 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
trueif 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:ByteToMessageDecoderIftruethen only one message is decoded on eachByteToMessageDecoder.channelRead(ChannelHandlerContext, Object)call. Default isfalseas this has performance impacts.- Overrides:
isSingleDecodein classByteToMessageDecoder
-
channelRead
public void channelRead(ChannelHandlerContext ctx, java.lang.Object msg) throws java.lang.Exception
Description copied from class:ChannelInboundHandlerAdapterCallsChannelHandlerContext.fireChannelRead(Object)to forward to the nextChannelInboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
channelReadin interfaceChannelInboundHandler- Overrides:
channelReadin 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:ByteToMessageDecoderDecode the from oneByteBufto an other. This method will be called till either the inputByteBufhas nothing to read when return from this method or till nothing was read from the inputByteBuf.- Specified by:
decodein classByteToMessageDecoder- Parameters:
ctx- theChannelHandlerContextwhich thisByteToMessageDecoderbelongs toin- theByteBuffrom which to read dataout- theListto 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 theByteBufand return it.- Parameters:
ctx- theChannelHandlerContextwhich thisHAProxyMessageDecoderbelongs tobuffer- theByteBuffrom which to read data- Returns:
- frame the
ByteBufwhich represent the frame ornullif 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 theByteBufand return it.- Parameters:
ctx- theChannelHandlerContextwhich thisHAProxyMessageDecoderbelongs tobuffer- theByteBuffrom which to read data- Returns:
- frame the
ByteBufwhich represent the frame ornullif 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 theProtocolDetectionResultfor the givenByteBuf.
-
match
private static boolean match(byte[] prefix, ByteBuf buffer, int idx)
-
-