Changed willMessage and password in CONNECT message from String to byte[] (fix issue #94 and #95)

This commit is contained in:
andsel
2015-09-16 23:25:15 +02:00
parent 9a7ec7fd62
commit e08cf95135
18 changed files with 58 additions and 49 deletions
@@ -173,7 +173,7 @@ public class ConnectDecoder extends DemuxDecoder {
//Decode willMessage
if (willFlag) {
String willMessage = Utils.decodeString(in);
byte[] willMessage = Utils.readFixedLengthContent(in);
if (willMessage == null) {
in.resetReaderIndex();
return;
@@ -207,7 +207,7 @@ public class ConnectDecoder extends DemuxDecoder {
//Decode password
if (passwordFlag) {
String password = Utils.decodeString(in);
byte[] password = Utils.readFixedLengthContent(in);
if (password == null) {
in.resetReaderIndex();
return;
@@ -65,12 +65,12 @@ public class ConnectEncoder extends DemuxEncoder<ConnectMessage> {
variableHeaderBuff.writeBytes(Utils.encodeString(message.getClientID()));
if (message.isWillFlag()) {
variableHeaderBuff.writeBytes(Utils.encodeString(message.getWillTopic()));
variableHeaderBuff.writeBytes(Utils.encodeString(message.getWillMessage()));
variableHeaderBuff.writeBytes(Utils.encodeFixedLengthContent(message.getWillMessage()));
}
if (message.isUserFlag() && message.getUsername() != null) {
variableHeaderBuff.writeBytes(Utils.encodeString(message.getUsername()));
if (message.isPasswordFlag() && message.getPassword() != null) {
variableHeaderBuff.writeBytes(Utils.encodeString(message.getPassword()));
variableHeaderBuff.writeBytes(Utils.encodeFixedLengthContent(message.getPassword()));
}
}
}
@@ -117,10 +117,16 @@ public class Utils {
* @return the decoded string or null if NEED_DATA
*/
static String decodeString(ByteBuf in) throws UnsupportedEncodingException {
return new String(readFixedLengthContent(in), "UTF-8");
}
/**
* Read a byte array from the buffer, use two bytes as length information followed by length bytes.
* */
static byte[] readFixedLengthContent(ByteBuf in) throws UnsupportedEncodingException {
if (in.readableBytes() < 2) {
return null;
}
//int strLen = Utils.readWord(in);
int strLen = in.readUnsignedShort();
if (in.readableBytes() < strLen) {
return null;
@@ -128,16 +134,14 @@ public class Utils {
byte[] strRaw = new byte[strLen];
in.readBytes(strRaw);
return new String(strRaw, "UTF-8");
return strRaw;
}
/**
* Return the IoBuffer with string encoded as MSB, LSB and UTF-8 encoded
* string content.
*/
public static ByteBuf encodeString(String str) {
ByteBuf out = Unpooled.buffer(2);
byte[] raw;
try {
raw = str.getBytes("UTF-8");
@@ -147,12 +151,19 @@ public class Utils {
LoggerFactory.getLogger(Utils.class).error(null, ex);
return null;
}
//Utils.writeWord(out, raw.length);
out.writeShort(raw.length);
out.writeBytes(raw);
return encodeFixedLengthContent(raw);
}
/**
* Return the IoBuffer with string encoded as MSB, LSB and bytes array content.
*/
public static ByteBuf encodeFixedLengthContent(byte[] content) {
ByteBuf out = Unpooled.buffer(2);
out.writeShort(content.length);
out.writeBytes(content);
return out;
}
/**
* Return the number of bytes to encode the given remaining length value
*/