ending = false;
done = true;
break;
} else if (ending) {
logger.error("Corrupt stream, expected LF, found [0x{}]", Integer.toHexString(next));
throw new StreamCorruptedException("Expecting LF after CR");
} else {
sb.append((char) next);
}
}
if (!done) {
// let's keep the buffer and bytes read
// buffer.discardReadBytes();
buffer.markReaderIndex();
return null;
}
String[] args = lineSplit.split(sb);
// we read the text, clear it
sb.setLength(0);
String cmd = args[0];
if ("get".equals(cmd)) {
request = new MemcachedRestRequest(RestRequest.Method.GET, args[1], null, -1, false);
if (args.length > 3) {
request.setData(Unicode.fromStringAsBytes(args[2]));
}
return request;
} else if ("delete".equals(cmd)) {
request = new MemcachedRestRequest(RestRequest.Method.DELETE, args[1], null, -1, false);
// if (args.length > 3) {
// request.setData(Unicode.fromStringAsBytes(args[2]));
// }
return request;
} else if ("set".equals(cmd)) {
this.request = new MemcachedRestRequest(RestRequest.Method.POST, args[1], null, Integer.parseInt(args[4]), false);
buffer.markReaderIndex();
} else if ("version".equals(cmd)) { // sent as a noop
byte[] bytes = Version.full().getBytes();
ChannelBuffer writeBuffer = ChannelBuffers.dynamicBuffer(bytes.length);
writeBuffer.writeBytes(bytes);
channel.write(writeBuffer);
return MemcachedDispatcher.IGNORE_REQUEST;
} else if ("quit".equals(cmd)) {
if (channel.isConnected()) { // we maybe in the process of clearing the queued bits
channel.disconnect();
}
} else {
logger.error("Unsupported command [{}], ignoring and closing connection", cmd);
if (channel.isConnected()) { // we maybe in the process of clearing the queued bits
channel.disconnect();
}
return null;
}
}
} else {
if (buffer.readableBytes() < (request.getDataSize() + 2)) {
return null;
}
byte[] data = new byte[request.getDataSize()];
buffer.readBytes(data, 0, data.length);
byte next = buffer.readByte();
if (next == CR) {
next = buffer.readByte();
if (next == LF) {
request.setData(data);
// reset
this.request = null;
return request;
} else {
this.request = null;
throw new StreamCorruptedException("Expecting separator after data block");
}
} else {
this.request = null;
throw new StreamCorruptedException("Expecting separator after data block");
}
}
return null;
}