Do not use slice() to read content of an HTTP message

This fixes the bug introduced while fixing #412
This commit is contained in:
Trustin Lee 2012-12-19 18:38:03 +09:00
parent f13f3d1ed3
commit f0a5774fed

View File

@ -467,39 +467,19 @@ public abstract class HttpMessageDecoder extends ReplayingDecoder<HttpMessageDec
if (length < contentRead) { if (length < contentRead) {
if (!message.isChunked()) { if (!message.isChunked()) {
message.setChunked(true); message.setChunked(true);
return new Object[] {message, new DefaultHttpChunk(read(buffer, toRead))}; return new Object[] {message, new DefaultHttpChunk(buffer.readBytes(toRead))};
} else { } else {
return new DefaultHttpChunk(read(buffer, toRead)); return new DefaultHttpChunk(buffer.readBytes(toRead));
} }
} }
if (content == null) { if (content == null) {
content = read(buffer, (int) length); content = buffer.readBytes((int) length);
} else { } else {
content.writeBytes(buffer.readBytes((int) length)); content.writeBytes(buffer, (int) length);
} }
return reset(); return reset();
} }
/**
* Try to do an optimized "read" of len from the given {@link ChannelBuffer}.
*
* This is part of #412 to safe byte copies
*
*/
private ChannelBuffer read(ChannelBuffer buffer, int len) {
ChannelBuffer internal = internalBuffer();
if (internal.readableBytes() >= len) {
int index = internal.readerIndex();
ChannelBuffer buf = internal.slice(index, len);
// update the readerindex so an the next read its on the correct position
buffer.readerIndex(index + len);
return buf;
} else {
return buffer.readBytes(len);
}
}
private State readHeaders(ChannelBuffer buffer) throws TooLongFrameException { private State readHeaders(ChannelBuffer buffer) throws TooLongFrameException {
headerSize = 0; headerSize = 0;
final HttpMessage message = this.message; final HttpMessage message = this.message;