下面为HttpObjectEncoder关于encode部分的源码,其中定义了一个变量buf,将编码后的数据存储到buf中,有关协议和消息头的部分没有问题,关于定长消息内容的处理如下:
case ST_CONTENT_NON_CHUNK:
final long contentLength = contentLength(msg);
if (contentLength > 0) {
if (buf != null && buf.writableBytes() >= contentLength && msg instanceof HttpContent) {
// merge into other buffer for performance reasons
buf.writeBytes(((HttpContent) msg).content());
out.add(buf);
} else {
if (buf != null) {
out.add(buf);
}
out.add(encodeAndRetain(msg));
}
if (msg instanceof LastHttpContent) {
state = ST_INIT;
}
break;
}
在这里他先判断了buf的可填充部分是否能容纳下这么长的消息内容,如果当前可写部分小于contentLength的话就直接舍弃了消息内容的处理,所以会引起请求内容的丢失,encode后就只剩下了请求协议与请求头,这是netty的一个bug,还是另有其他设置的地方?
ps:
我是将http请求通过下面的方式重新进行编码,后面读取数据时,发现丢失了消息体部分。查看源码部分后发现了上面的问题
EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestEncoder());
channel.writeOutbound(request);
encoded = channel.readOutbound();
channel.close();
我通过修改源码部分中,对于buf的可写部分的判断可以解决遇到的问题,但是netty这么成熟的框架应该不会有这种错误
case ST_CONTENT_NON_CHUNK:
long contentLength = contentLength(msg);
if (contentLength > 0) {
// 取消buf.writableBytes() >= contentLength判断条件
// if (buf != null && buf.writableBytes() >= contentLength && msg instanceof HttpContent) {
if (buf != null && msg instanceof HttpContent) {
// merge into other buffer for performance reasons
buf.writeBytes(((HttpContent) msg).content());
out.add(buf);
} else {
if (buf != null) {
out.add(buf);
}
out.add(encodeAndRetain(msg));
}
if (msg instanceof LastHttpContent) {
state = ST_INIT;
}
break;
}
HttpObjectEncoder 当数据过长时丢失,什么方式提交数据呢?
我是将http请求通过下面的方式重新进行编码,后面读取数据时,发现丢失了消息体部分。查看源码部分后发现了上面的问题 EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestEncoder()); channel.writeOutbound(request); encoded = channel.readOutbound(); channel.close();
我通过修改源码部分中,对于buf的可写部分的判断可以解决遇到的问题,但是netty这么成熟的框架应该不会有这种错误,是否我哪里设置有问题?
case ST_CONTENT_NON_CHUNK: long contentLength = contentLength(msg); if (contentLength > 0) { // 取消buf.writableBytes() >= contentLength判断条件 // if (buf != null && buf.writableBytes() >= contentLength && msg instanceof HttpContent) { if (buf != null && msg instanceof HttpContent) { // merge into other buffer for performance reasons buf.writeBytes(((HttpContent) msg).content()); out.add(buf); } else { if (buf != null) { out.add(buf); } out.add(encodeAndRetain(msg)); } if (msg instanceof LastHttpContent) { state = ST_INIT; } break; }