if (request.isQuiet() && response.status().getStatus() < 500) {
// nothing to send and all is well
return;
}
try {
ChannelBuffer writeBuffer = ChannelBuffers.dynamicBuffer(24 + request.getUriBytes().length + response.contentLength() + 12);
writeBuffer.writeByte(0x81); // magic
if (request.method() == RestRequest.Method.GET) {
writeBuffer.writeByte(0x00); // opcode
} else if (request.method() == RestRequest.Method.POST) {
if (request.isQuiet()) {
writeBuffer.writeByte(0x11); // opcode
} else {
writeBuffer.writeByte(0x01); // opcode
}
} else if (request.method() == RestRequest.Method.DELETE) {
writeBuffer.writeByte(0x04); // opcode
}
short keyLength = request.method() == RestRequest.Method.GET ? (short) request.getUriBytes().length : 0;
writeBuffer.writeShort(keyLength);
int extrasLength = request.method() == RestRequest.Method.GET ? 4 : 0;
writeBuffer.writeByte(extrasLength); // extra length = flags + expiry
writeBuffer.writeByte(0); // data type unused
if (response.status().getStatus() >= 500) {
// TODO should we use this?
writeBuffer.writeShort(0x0A); // status code
} else {
writeBuffer.writeShort(0x0000); // OK
}
int dataLength = request.method() == RestRequest.Method.GET ? response.contentLength() : 0;
writeBuffer.writeInt(dataLength + keyLength + extrasLength); // data length
writeBuffer.writeInt(request.getOpaque()); // opaque
writeBuffer.writeLong(0); // cas
if (extrasLength > 0) {
writeBuffer.writeShort(0);
writeBuffer.writeShort(0);
}
if (keyLength > 0) {
writeBuffer.writeBytes(request.getUriBytes());
}
ChannelFutureListener releaseContentListener = null;
if (dataLength > 0) {
if (response instanceof XContentRestResponse) {
// if its a builder based response, and it was created with a CachedStreamOutput, we can release it
// after we write the response, and no need to do an extra copy because its not thread safe
XContentBuilder builder = ((XContentRestResponse) response).builder();
if (builder.payload() instanceof CachedStreamOutput.Entry) {
releaseContentListener = new NettyTransport.CacheFutureListener((CachedStreamOutput.Entry) builder.payload());
ChannelBuffer buf = ChannelBuffers.wrappedBuffer(builder.unsafeBytes(), 0, builder.unsafeBytesLength());
writeBuffer = ChannelBuffers.wrappedBuffer(writeBuffer, buf);
} else if (response.contentThreadSafe()) {
ChannelBuffer buf = ChannelBuffers.wrappedBuffer(builder.unsafeBytes(), 0, builder.unsafeBytesLength());
writeBuffer = ChannelBuffers.wrappedBuffer(writeBuffer, buf);
} else {
writeBuffer.writeBytes(response.content(), 0, response.contentLength());
}
} else {
if (response.contentThreadSafe()) {
ChannelBuffer buf = ChannelBuffers.wrappedBuffer(response.content(), 0, response.contentLength());
writeBuffer = ChannelBuffers.wrappedBuffer(writeBuffer, buf);
} else {
writeBuffer.writeBytes(response.content(), 0, response.contentLength());
}
}
}
ChannelFuture future = channel.write(writeBuffer);
if (releaseContentListener != null) {
future.addListener(releaseContentListener);
}
} catch (Exception e) {
throw new MemcachedTransportException("Failed to write response", e);
}
} else {
if (response.status().getStatus() >= 500) {
channel.write(ERROR.duplicate());
} else {
if (request.method() == RestRequest.Method.POST) {
// TODO this is SET, can we send a payload?
channel.write(STORED.duplicate());
} else if (request.method() == RestRequest.Method.DELETE) {
channel.write(DELETED.duplicate());
} else { // GET
try {
ChannelBuffer writeBuffer = ChannelBuffers.dynamicBuffer(response.contentLength() + 512);
writeBuffer.writeBytes(VALUE.duplicate());
writeBuffer.writeBytes(Unicode.fromStringAsBytes(request.uri()));
writeBuffer.writeByte(' ');
writeBuffer.writeByte('0');
writeBuffer.writeByte(' ');
writeBuffer.writeBytes(Bytes.itoa(response.contentLength()));
writeBuffer.writeByte('\r');
writeBuffer.writeByte('\n');
writeBuffer.writeBytes(response.content(), 0, response.contentLength());
writeBuffer.writeByte('\r');
writeBuffer.writeByte('\n');
writeBuffer.writeBytes(END.duplicate());
channel.write(writeBuffer);
} catch (Exception e) {
throw new MemcachedTransportException("Failed to write 'get' response", e);
}
}