UnicodeUtil.UTF8toUTF16(key, 0, key.length, spare);
// the remainder of the message -- that is, totalLength - (keyLength + extraLength) should be the payload
int size = totalBodyLength - keyLength - extraLength;
request = new MemcachedRestRequest(RestRequest.Method.POST, new String(spare), key, size, true);
request.setOpaque(opaque);
request.setData(new ChannelBufferBytesReference(buffer.readSlice(size)));
request.setQuiet(opcode == 0x11);
return request;
} else if (opcode == 0x0A || opcode == 0x10) { // NOOP or STATS
// TODO once we support setQ we need to wait for them to flush
ChannelBuffer writeBuffer = ChannelBuffers.dynamicBuffer(24);
writeBuffer.writeByte(0x81); // magic
writeBuffer.writeByte(opcode); // opcode
writeBuffer.writeShort(0); // key length
writeBuffer.writeByte(0); // extra length = flags + expiry
writeBuffer.writeByte(0); // data type unused
writeBuffer.writeShort(0x0000); // OK
writeBuffer.writeInt(0); // data length
writeBuffer.writeInt(opaque); // opaque
writeBuffer.writeLong(0); // cas
channel.write(writeBuffer);
return MemcachedDispatcher.IGNORE_REQUEST;
} else if (opcode == 0x07) { // QUIT
channel.disconnect();
} else {
logger.error("Unsupported opcode [0x{}], ignoring and closing connection", Integer.toHexString(opcode));
channel.disconnect();
return null;
}
} else {
buffer.resetReaderIndex(); // reset to get to the first byte
// need to read a header
boolean done = false;
StringBuffer sb = this.sb;
int readableBytes = buffer.readableBytes();
for (int i = 0; i < readableBytes; i++) {
byte next = buffer.readByte();
if (!ending && next == CR) {
ending = true;
} else if (ending && next == LF) {
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) {
BytesRef bytesRef = new BytesRef(args[2]);
request.setData(new BytesArray(bytesRef));
}
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.CURRENT.toString().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;
}
BytesReference data = new ChannelBufferBytesReference(buffer.readSlice(request.getDataSize()));
byte next = buffer.readByte();
if (next == CR) {
next = buffer.readByte();
if (next == LF) {
request.setData(data);