public class HAProxyMessageDecoder extends ByteToMessageDecoder
ByteToMessageDecoder.Cumulator
ChannelHandler.Sharable
Modifier and Type | Field and Description |
---|---|
private static byte[] |
BINARY_PREFIX
Binary header prefix
|
private static int |
BINARY_PREFIX_LENGTH
Binary header prefix length
|
private static int |
DELIMITER_LENGTH
Version 1 header delimiter is always '\r\n' per spec
|
private static ProtocolDetectionResult<HAProxyProtocolVersion> |
DETECTION_RESULT_V1
|
private static ProtocolDetectionResult<HAProxyProtocolVersion> |
DETECTION_RESULT_V2
|
private int |
discardedBytes
Number of discarded bytes
|
private boolean |
discarding
true if we're discarding input because we're already over maxLength |
private boolean |
finished
true if we're finished decoding the proxy protocol header |
private static byte[] |
TEXT_PREFIX |
private static int |
V1_MAX_LENGTH
Maximum possible length of a v1 proxy protocol header per spec
|
private 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 size
|
private int |
version
Protocol specification version
|
COMPOSITE_CUMULATOR, MERGE_CUMULATOR
Constructor and Description |
---|
HAProxyMessageDecoder()
Creates a new decoder with no additional data (TLV) restrictions
|
HAProxyMessageDecoder(int maxTlvSize)
Creates a new decoder with restricted additional data (TLV) size
|
Modifier and Type | Method and Description |
---|---|
void |
channelRead(ChannelHandlerContext ctx,
java.lang.Object msg)
Calls
ChannelHandlerContext.fireChannelRead(Object) to forward
to the next ChannelInboundHandler in the ChannelPipeline . |
protected void |
decode(ChannelHandlerContext ctx,
ByteBuf in,
java.util.List<java.lang.Object> out)
Decode the from one
ByteBuf to an other. |
private ByteBuf |
decodeLine(ChannelHandlerContext ctx,
ByteBuf buffer)
Create a frame out of the
ByteBuf and return it. |
private ByteBuf |
decodeStruct(ChannelHandlerContext ctx,
ByteBuf buffer)
Create a frame out of the
ByteBuf and return it. |
static ProtocolDetectionResult<HAProxyProtocolVersion> |
detectProtocol(ByteBuf buffer)
Returns the
ProtocolDetectionResult for the given ByteBuf . |
private void |
fail(ChannelHandlerContext ctx,
java.lang.String errMsg,
java.lang.Throwable t) |
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()
If
true then only one message is decoded on each
ByteToMessageDecoder.channelRead(ChannelHandlerContext, Object) call. |
private static boolean |
match(byte[] prefix,
ByteBuf buffer,
int idx) |
actualReadableBytes, callDecode, channelInactive, channelReadComplete, decodeLast, discardSomeReadBytes, handlerRemoved, handlerRemoved0, internalBuffer, setCumulator, setDiscardAfterReads, setSingleDecode, userEventTriggered
channelActive, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught
handlerAdded, isSharable
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
handlerAdded
private static final int V1_MAX_LENGTH
private static final int V2_MAX_LENGTH
private static final int V2_MIN_LENGTH
private static final int V2_MAX_TLV
private static final int DELIMITER_LENGTH
private static final byte[] BINARY_PREFIX
private static final byte[] TEXT_PREFIX
private static final int BINARY_PREFIX_LENGTH
private static final ProtocolDetectionResult<HAProxyProtocolVersion> DETECTION_RESULT_V1
private static final ProtocolDetectionResult<HAProxyProtocolVersion> DETECTION_RESULT_V2
private boolean discarding
true
if we're discarding input because we're already over maxLengthprivate int discardedBytes
private boolean finished
true
if we're finished decoding the proxy protocol headerprivate int version
private final int v2MaxHeaderSize
public HAProxyMessageDecoder()
public HAProxyMessageDecoder(int maxTlvSize)
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
.
maxTlvSize
- maximum number of bytes allowed for additional data (Type-Length-Value vectors) in a v2 headerprivate static int findVersion(ByteBuf buffer)
private static int findEndOfHeader(ByteBuf buffer)
private static int findEndOfLine(ByteBuf buffer)
public boolean isSingleDecode()
ByteToMessageDecoder
true
then only one message is decoded on each
ByteToMessageDecoder.channelRead(ChannelHandlerContext, Object)
call.
Default is false
as this has performance impacts.isSingleDecode
in class ByteToMessageDecoder
public void channelRead(ChannelHandlerContext ctx, java.lang.Object msg) throws java.lang.Exception
ChannelInboundHandlerAdapter
ChannelHandlerContext.fireChannelRead(Object)
to forward
to the next ChannelInboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.channelRead
in interface ChannelInboundHandler
channelRead
in class ByteToMessageDecoder
java.lang.Exception
protected final void decode(ChannelHandlerContext ctx, ByteBuf in, java.util.List<java.lang.Object> out) throws java.lang.Exception
ByteToMessageDecoder
ByteBuf
to an other. This method will be called till either the input
ByteBuf
has nothing to read when return from this method or till nothing was read from the input
ByteBuf
.decode
in class ByteToMessageDecoder
ctx
- the ChannelHandlerContext
which this ByteToMessageDecoder
belongs toin
- the ByteBuf
from which to read dataout
- the List
to which decoded messages should be addedjava.lang.Exception
- is thrown if an error accourprivate ByteBuf decodeStruct(ChannelHandlerContext ctx, ByteBuf buffer) throws java.lang.Exception
ByteBuf
and return it.
Based on code from LineBasedFrameDecoder.decode(ChannelHandlerContext, ByteBuf)
.ctx
- the ChannelHandlerContext
which this HAProxyMessageDecoder
belongs tobuffer
- the ByteBuf
from which to read dataByteBuf
which represent the frame or null
if no frame could
be createdjava.lang.Exception
private ByteBuf decodeLine(ChannelHandlerContext ctx, ByteBuf buffer) throws java.lang.Exception
ByteBuf
and return it.
Based on code from LineBasedFrameDecoder.decode(ChannelHandlerContext, ByteBuf)
.ctx
- the ChannelHandlerContext
which this HAProxyMessageDecoder
belongs tobuffer
- the ByteBuf
from which to read dataByteBuf
which represent the frame or null
if no frame could
be createdjava.lang.Exception
private void failOverLimit(ChannelHandlerContext ctx, int length)
private void failOverLimit(ChannelHandlerContext ctx, java.lang.String length)
private void fail(ChannelHandlerContext ctx, java.lang.String errMsg, java.lang.Throwable t)
public static ProtocolDetectionResult<HAProxyProtocolVersion> detectProtocol(ByteBuf buffer)
ProtocolDetectionResult
for the given ByteBuf
.private static boolean match(byte[] prefix, ByteBuf buffer, int idx)