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 classHeaderExtractor create a header frame out of theByteBuf.private final classprivate final classNested classes/interfaces inherited from class ByteToMessageDecoder
ByteToMessageDecoder.CumulatorNested classes/interfaces inherited from interface ChannelHandler
ChannelHandler.Sharable -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intBinary header prefix lengthprivate static final ProtocolDetectionResult<HAProxyProtocolVersion> private static final ProtocolDetectionResult<HAProxyProtocolVersion> private intNumber of discarded bytesprivate booleantrueif we're discarding input because we're already over maxLengthprivate final booleanWhether or not to throw an exception as soon as we exceed maxLength.private booleantrueif we're finished decoding the proxy protocol headerUsed to extract a header frame out of theByteBufand return it.private static final intMaximum possible length of a v1 proxy protocol header per specprivate static final intMaximum possible length of a v2 proxy protocol header (fixed 16 bytes + max unsigned short)private static final intMaximum possible length for v2 additional TLV data (max unsigned short - max v2 address info space)private static final intMinimum possible length of a fully functioning v2 proxy protocol header (fixed 16 bytes + v2 address info space)private final intThe 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 intProtocol specification versionFields inherited from class 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 TypeMethodDescriptionvoidchannelRead(ChannelHandlerContext ctx, Object msg) CallsChannelHandlerContext.fireChannelRead(Object)to forward to the nextChannelInboundHandlerin theChannelPipeline.protected final voiddecode(ChannelHandlerContext ctx, ByteBuf in, List<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.detectProtocol(ByteBuf buffer) Returns theProtocolDetectionResultfor the givenByteBuf.private voidfail(ChannelHandlerContext ctx, String errMsg, Exception e) private voidfailOverLimit(ChannelHandlerContext ctx, int length) private voidfailOverLimit(ChannelHandlerContext ctx, 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.booleanIftruethen only one message is decoded on eachByteToMessageDecoder.channelRead(ChannelHandlerContext, Object)call.private static booleanMethods inherited from class ByteToMessageDecoder
actualReadableBytes, callDecode, channelInactive, channelReadComplete, decodeLast, discardSomeReadBytes, handlerRemoved, handlerRemoved0, internalBuffer, setCumulator, setDiscardAfterReads, setSingleDecode, userEventTriggeredMethods inherited from class ChannelInboundHandlerAdapter
channelActive, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaughtMethods inherited from class ChannelHandlerAdapter
ensureNotSharable, handlerAdded, isSharableMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface 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 theByteBufand return it. -
discarding
private boolean discardingtrueif 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 finishedtrueif 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:ByteToMessageDecoderIftruethen only one message is decoded on eachByteToMessageDecoder.channelRead(ChannelHandlerContext, Object)call. Default isfalseas this has performance impacts.- Overrides:
isSingleDecodein classByteToMessageDecoder
-
channelRead
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:
Exception
-
decode
protected final void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws 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:
Exception- is thrown if an error occurs
-
decodeStruct
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:
Exception
-
decodeLine
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:
Exception
-
failOverLimit
-
failOverLimit
-
fail
-
detectProtocol
Returns theProtocolDetectionResultfor the givenByteBuf. -
match
-