From a7f83aa23ebe14669119aadb2bbd3012f94fdf64 Mon Sep 17 00:00:00 2001 From: Scott Mitchell Date: Tue, 23 Jun 2015 09:25:03 -0700 Subject: [PATCH] HTTP/2 Example Needs FullHttpRequest Motivation: The HTTP/2 hello world example server should be expecting a FullHttpRequest when falling back to HTTP/1.x mode. Modifications: - HelloWorldHttp1Handler should process FullHttpRequestObjects - Http2ServerInitializer should insert an HttpObjectAggregator into the pipeline if no upgrade was attempted Result: Responses from the HelloWorldHttp1Handler should only come after full HTTP requests are received. --- .../server/HelloWorldHttp1Handler.java | 29 ++++++++++--------- .../server/Http2ServerInitializer.java | 20 ++++++++++--- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/example/src/main/java/io/netty/example/http2/helloworld/server/HelloWorldHttp1Handler.java b/example/src/main/java/io/netty/example/http2/helloworld/server/HelloWorldHttp1Handler.java index c790399f0f..f00932a837 100644 --- a/example/src/main/java/io/netty/example/http2/helloworld/server/HelloWorldHttp1Handler.java +++ b/example/src/main/java/io/netty/example/http2/helloworld/server/HelloWorldHttp1Handler.java @@ -15,28 +15,29 @@ */ package io.netty.example.http2.helloworld.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; -import io.netty.handler.codec.http.DefaultFullHttpResponse; -import io.netty.handler.codec.http.FullHttpResponse; -import io.netty.handler.codec.http.HttpHeaderUtil; -import io.netty.handler.codec.http.HttpHeaderValues; -import io.netty.handler.codec.http.HttpRequest; - import static io.netty.handler.codec.http.HttpHeaderNames.CONNECTION; import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH; 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; +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; +import io.netty.handler.codec.http.DefaultFullHttpResponse; +import io.netty.handler.codec.http.FullHttpRequest; +import io.netty.handler.codec.http.FullHttpResponse; +import io.netty.handler.codec.http.HttpHeaderUtil; +import io.netty.handler.codec.http.HttpHeaderValues; + /** * HTTP handler that responds with a "Hello World" */ -public class HelloWorldHttp1Handler extends SimpleChannelInboundHandler { +public class HelloWorldHttp1Handler extends SimpleChannelInboundHandler { private final String establishApproach; public HelloWorldHttp1Handler(String establishApproach) { @@ -44,7 +45,7 @@ public class HelloWorldHttp1Handler extends SimpleChannelInboundHandler { }; private final SslContext sslCtx; + private final int maxHttpContentLength; public Http2ServerInitializer(SslContext sslCtx) { + this(sslCtx, 16 * 1024); + } + + public Http2ServerInitializer(SslContext sslCtx, int maxHttpContentLength) { + if (maxHttpContentLength < 0) { + throw new IllegalArgumentException("maxHttpContentLength (expected >= 0): " + maxHttpContentLength); + } this.sslCtx = sslCtx; + this.maxHttpContentLength = maxHttpContentLength; } @Override @@ -71,9 +81,9 @@ public class Http2ServerInitializer extends ChannelInitializer { } /** - * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2. + * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.0 */ - private static void configureClearText(SocketChannel ch) { + private void configureClearText(SocketChannel ch) { final ChannelPipeline p = ch.pipeline(); final HttpServerCodec sourceCodec = new HttpServerCodec(); @@ -84,8 +94,10 @@ public class Http2ServerInitializer extends ChannelInitializer { protected void channelRead0(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.")); + ChannelPipeline pipeline = ctx.pipeline(); + ChannelHandlerContext thisCtx = pipeline.context(this); + pipeline.addAfter(thisCtx.name(), null, new HelloWorldHttp1Handler("Direct. No Upgrade Attempted.")); + pipeline.replace(this, null, new HttpObjectAggregator(maxHttpContentLength)); ctx.fireChannelRead(msg); } });