From 972b4c6e483fd5e7e0660df97130be7c1a36de55 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 | 15 ++++++++------ .../server/Http2ServerInitializer.java | 20 +++++++++++++++---- 2 files changed, 25 insertions(+), 10 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 fa652d671a..d933a60e35 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,6 +15,12 @@ */ package io.netty.example.http2.helloworld.server; +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; @@ -22,18 +28,15 @@ 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; -import io.netty.handler.codec.http.HttpRequest; -import static io.netty.handler.codec.http.HttpHeaderNames.*; -import static io.netty.handler.codec.http.HttpResponseStatus.*; -import static io.netty.handler.codec.http.HttpVersion.*; /** * 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) { @@ -41,7 +44,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 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.")); + 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); } });