HAProxyMessageDecoder extends ByteToMessageDecoder { public static ProtocolDetectionResult<HAProxyProtocolVersion> detectProtocol(ByteBuf buffer) { if (buffer.readableBytes() < 12) { return ProtocolDetectionResult.needsMoreData(); } int idx = buffer.readerIndex(); if (match(BINARY_PREFIX, buffer, idx)) { return DETECTION_RESULT_V2; } if (match(TEXT_PREFIX, buffer, idx)) { return DETECTION_RESULT_V1; } return ProtocolDetectionResult.invalid(); } HAProxyMessageDecoder();  HAProxyMessageDecoder(int maxTlvSize); @Override boolean isSingleDecode(); @Override void channelRead(ChannelHandlerContext ctx, Object msg); static ProtocolDetectionResult<HAProxyProtocolVersion> detectProtocol(ByteBuf buffer);  }
@Test public void testDetectProtocol() { final ByteBuf validHeaderV1 = copiedBuffer("PROXY TCP4 192.168.0.1 192.168.0.11 56324 443\r\n", CharsetUtil.US_ASCII); ProtocolDetectionResult<HAProxyProtocolVersion> result = HAProxyMessageDecoder.detectProtocol(validHeaderV1); assertEquals(ProtocolDetectionState.DETECTED, result.state()); assertEquals(HAProxyProtocolVersion.V1, result.detectedProtocol()); validHeaderV1.release(); final ByteBuf invalidHeader = copiedBuffer("Invalid header", CharsetUtil.US_ASCII); result = HAProxyMessageDecoder.detectProtocol(invalidHeader); assertEquals(ProtocolDetectionState.INVALID, result.state()); assertNull(result.detectedProtocol()); invalidHeader.release(); final ByteBuf validHeaderV2 = buffer(); validHeaderV2.writeByte(0x0D); validHeaderV2.writeByte(0x0A); validHeaderV2.writeByte(0x0D); validHeaderV2.writeByte(0x0A); validHeaderV2.writeByte(0x00); validHeaderV2.writeByte(0x0D); validHeaderV2.writeByte(0x0A); validHeaderV2.writeByte(0x51); validHeaderV2.writeByte(0x55); validHeaderV2.writeByte(0x49); validHeaderV2.writeByte(0x54); validHeaderV2.writeByte(0x0A); result = HAProxyMessageDecoder.detectProtocol(validHeaderV2); assertEquals(ProtocolDetectionState.DETECTED, result.state()); assertEquals(HAProxyProtocolVersion.V2, result.detectedProtocol()); validHeaderV2.release(); final ByteBuf incompleteHeader = buffer(); incompleteHeader.writeByte(0x0D); incompleteHeader.writeByte(0x0A); incompleteHeader.writeByte(0x0D); incompleteHeader.writeByte(0x0A); incompleteHeader.writeByte(0x00); incompleteHeader.writeByte(0x0D); incompleteHeader.writeByte(0x0A); result = HAProxyMessageDecoder.detectProtocol(incompleteHeader); assertEquals(ProtocolDetectionState.NEEDS_MORE_DATA, result.state()); assertNull(result.detectedProtocol()); incompleteHeader.release(); }