netty-1616: Adding a 'last' check to SpdyHttpEncoder.

This commit is contained in:
Shawn Silverman 2013-07-19 13:03:18 -07:00 committed by Norman Maurer
parent 60529ac3ee
commit 80f92f90cb

View File

@ -18,6 +18,7 @@ package io.netty.handler.codec.spdy;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import io.netty.handler.codec.UnsupportedMessageTypeException;
import io.netty.handler.codec.http.FullHttpMessage;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpHeaders;
@ -141,6 +142,7 @@ public class SpdyHttpEncoder extends MessageToMessageEncoder<HttpObject> {
protected void encode(ChannelHandlerContext ctx, HttpObject msg, List<Object> out) throws Exception {
boolean valid = false;
boolean last = false;
if (msg instanceof HttpRequest) {
@ -148,6 +150,7 @@ public class SpdyHttpEncoder extends MessageToMessageEncoder<HttpObject> {
SpdySynStreamFrame spdySynStreamFrame = createSynStreamFrame(httpRequest);
out.add(spdySynStreamFrame);
last = spdySynStreamFrame.isLast();
valid = true;
}
if (msg instanceof HttpResponse) {
@ -155,15 +158,17 @@ public class SpdyHttpEncoder extends MessageToMessageEncoder<HttpObject> {
HttpResponse httpResponse = (HttpResponse) msg;
if (httpResponse.headers().contains(SpdyHttpHeaders.Names.ASSOCIATED_TO_STREAM_ID)) {
SpdySynStreamFrame spdySynStreamFrame = createSynStreamFrame(httpResponse);
last = spdySynStreamFrame.isLast();
out.add(spdySynStreamFrame);
} else {
SpdySynReplyFrame spdySynReplyFrame = createSynReplyFrame(httpResponse);
last = spdySynReplyFrame.isLast();
out.add(spdySynReplyFrame);
}
valid = true;
}
if (msg instanceof HttpContent) {
if (msg instanceof HttpContent && !last) {
HttpContent chunk = (HttpContent) msg;
@ -255,6 +260,7 @@ public class SpdyHttpEncoder extends MessageToMessageEncoder<HttpObject> {
spdySynStreamFrame.headers().add(entry.getKey(), entry.getValue());
}
currentStreamId = spdySynStreamFrame.getStreamId();
spdySynStreamFrame.setLast(isLast(httpMessage));
return spdySynStreamFrame;
}
@ -284,8 +290,25 @@ public class SpdyHttpEncoder extends MessageToMessageEncoder<HttpObject> {
}
currentStreamId = streamID;
spdySynReplyFrame.setLast(false);
spdySynReplyFrame.setLast(isLast(httpResponse));
return spdySynReplyFrame;
}
/**
* Checks if the given HTTP message should be considered as a last SPDY frame.
*
* @param httpMessage check this HTTP message
* @return whether the given HTTP message should generate a <em>last</em> SPDY frame.
*/
private static boolean isLast(HttpMessage httpMessage) {
if (httpMessage instanceof FullHttpMessage) {
FullHttpMessage fullMessage = (FullHttpMessage) httpMessage;
if (fullMessage.trailingHeaders().isEmpty() && !fullMessage.content().isReadable()) {
return true;
}
}
return false;
}
}