Fixed issue: NETTY-359 Missing support for HTTP 'Expect: 100-continue' header.

* Improved the snoop example to support the 100-continue header
* Fixed a problem in HttpClientCodec, HTtpContentDecoder and HttpContentEncoder where 100-continue response causes incorrect behavior
This commit is contained in:
Trustin Lee 2010-10-18 08:19:03 +00:00
parent 04430cd6d3
commit 4fd034ab5d
4 changed files with 26 additions and 4 deletions

View File

@ -22,8 +22,8 @@ import static org.jboss.netty.handler.codec.http.HttpVersion.*;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.Set;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
@ -62,6 +62,11 @@ public class HttpRequestHandler extends SimpleChannelUpstreamHandler {
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
if (!readingChunks) {
HttpRequest request = this.request = (HttpRequest) e.getMessage();
if (is100ContinueExpected(request)) {
send100Continue(e);
}
buf.setLength(0);
buf.append("WELCOME TO THE WILD WILD WEB SERVER\r\n");
buf.append("===================================\r\n");
@ -159,6 +164,11 @@ public class HttpRequestHandler extends SimpleChannelUpstreamHandler {
}
}
private void send100Continue(MessageEvent e) {
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, CONTINUE);
e.getChannel().write(response);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
throws Exception {

View File

@ -116,6 +116,12 @@ public class HttpClientCodec implements ChannelUpstreamHandler,
@Override
protected boolean isContentAlwaysEmpty(HttpMessage msg) {
final int statusCode = ((HttpResponse) msg).getStatus().getCode();
if (statusCode == 100) {
// 100-continue response should be excluded from paired comparison.
return true;
}
// Get the method of the HTTP request that corresponds to the
// current response.
HttpMethod method = queue.poll();
@ -137,7 +143,7 @@ public class HttpClientCodec implements ChannelUpstreamHandler,
break;
case 'C':
// Successful CONNECT request results in a response with empty body.
if (((HttpResponse) msg).getStatus().getCode() == 200) {
if (statusCode == 200) {
if (HttpMethod.CONNECT.equals(method)) {
// Proxy connection established - Not HTTP anymore.
done = true;

View File

@ -60,7 +60,10 @@ public abstract class HttpContentDecoder extends SimpleChannelUpstreamHandler {
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
Object msg = e.getMessage();
if (msg instanceof HttpMessage) {
if (msg instanceof HttpResponse && ((HttpResponse) msg).getStatus().getCode() == 100) {
// 100-continue response must be passed through.
ctx.sendDownstream(e);
} else if (msg instanceof HttpMessage) {
HttpMessage m = (HttpMessage) msg;
decoder = null;

View File

@ -88,7 +88,10 @@ public abstract class HttpContentEncoder extends SimpleChannelHandler {
throws Exception {
Object msg = e.getMessage();
if (msg instanceof HttpMessage) {
if (msg instanceof HttpResponse && ((HttpResponse) msg).getStatus().getCode() == 100) {
// 100-continue response must be passed through.
ctx.sendDownstream(e);
} else if (msg instanceof HttpMessage) {
HttpMessage m = (HttpMessage) msg;
encoder = null;