From 04c0d77287d861940d3cd5ae8b672e93ad49e97a Mon Sep 17 00:00:00 2001 From: Scott Mitchell Date: Fri, 8 May 2015 12:38:58 -0700 Subject: [PATCH] HTTP/2 Server Example No Response for HTTP/1.x Only Clients Motiviation: The HTTP/2 server example just hangs when a client is using only HTTP with no ALPN or upgrade attempts. We should still send some kind of response. Modifications: The HTTP/2 server example has a special handler to detect no upgrade HTTP clients and generate a response. Result: Clients that just use HTTP with no upgrade will no appear hung when interacting with the HTTP/2 server example. --- .../example/http2/server/HelloWorldHttp1Handler.java | 9 ++++++++- .../example/http2/server/HelloWorldHttp2Handler.java | 6 +++++- .../example/http2/server/Http2OrHttpHandler.java | 2 +- .../example/http2/server/Http2ServerInitializer.java | 12 ++++++++++++ 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/example/src/main/java/io/netty/example/http2/server/HelloWorldHttp1Handler.java b/example/src/main/java/io/netty/example/http2/server/HelloWorldHttp1Handler.java index 87b176fc5b..b43f851d6c 100644 --- a/example/src/main/java/io/netty/example/http2/server/HelloWorldHttp1Handler.java +++ b/example/src/main/java/io/netty/example/http2/server/HelloWorldHttp1Handler.java @@ -15,7 +15,9 @@ */ package io.netty.example.http2.server; +import static io.netty.util.internal.ObjectUtil.checkNotNull; import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufUtil; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; @@ -31,11 +33,15 @@ import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE; import static io.netty.handler.codec.http.HttpResponseStatus.CONTINUE; import static io.netty.handler.codec.http.HttpResponseStatus.OK; import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; - /** * HTTP handler that responds with a "Hello World" */ public class HelloWorldHttp1Handler extends SimpleChannelInboundHandler { + private final String establishApproach; + + public HelloWorldHttp1Handler(String establishApproach) { + this.establishApproach = checkNotNull(establishApproach, "establishApproach"); + } @Override public void channelRead0(ChannelHandlerContext ctx, HttpRequest req) throws Exception { @@ -46,6 +52,7 @@ public class HelloWorldHttp1Handler extends SimpleChannelInboundHandler { ch.pipeline().addLast(sourceCodec); ch.pipeline().addLast(upgradeHandler); + ch.pipeline().addLast(new SimpleChannelInboundHandler() { + @Override + protected void messageReceived(ChannelHandlerContext ctx, HttpMessage msg) throws Exception { + // If this handler is hit then no upgrade has been attempted and the client is just talking HTTP. + System.err.println("Directly talking: " + msg.protocolVersion() + " (no upgrade was attempted)"); + ctx.pipeline().replace(this, "http-hello-world", + new HelloWorldHttp1Handler("Direct. No Upgrade Attempted.")); + ctx.fireChannelRead(msg); + } + }); ch.pipeline().addLast(new UserEventLogger()); }