View Javadoc
1   /*
2    * Copyright 2015 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package io.netty.example.http2.tiles;
18  
19  import static io.netty.buffer.Unpooled.copiedBuffer;
20  import static io.netty.buffer.Unpooled.unreleasableBuffer;
21  import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH;
22  import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE;
23  import static io.netty.handler.codec.http.HttpResponseStatus.CONTINUE;
24  import static io.netty.handler.codec.http.HttpResponseStatus.OK;
25  import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
26  import static io.netty.util.CharsetUtil.UTF_8;
27  import io.netty.buffer.ByteBuf;
28  import io.netty.channel.ChannelFutureListener;
29  import io.netty.channel.ChannelHandlerContext;
30  import io.netty.channel.SimpleChannelInboundHandler;
31  import io.netty.handler.codec.http.DefaultFullHttpResponse;
32  import io.netty.handler.codec.http.FullHttpResponse;
33  import io.netty.handler.codec.http.HttpUtil;
34  import io.netty.handler.codec.http.HttpRequest;
35  import io.netty.handler.codec.http2.Http2CodecUtil;
36  
37  /**
38   * Handles the exceptional case where HTTP 1.x was negotiated under TLS.
39   */
40  public final class FallbackRequestHandler extends SimpleChannelInboundHandler<HttpRequest> {
41  
42      private static final ByteBuf response = unreleasableBuffer(copiedBuffer("<!DOCTYPE html>"
43              + "<html><body><h2>To view the example you need a browser that supports HTTP/2 ("
44              + Http2CodecUtil.TLS_UPGRADE_PROTOCOL_NAME
45              + ")</h2></body></html>", UTF_8)).asReadOnly();
46  
47      @Override
48      protected void channelRead0(ChannelHandlerContext ctx, HttpRequest req) throws Exception {
49          if (HttpUtil.is100ContinueExpected(req)) {
50              ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
51          }
52  
53          ByteBuf content = ctx.alloc().buffer();
54          content.writeBytes(response.duplicate());
55  
56          FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content);
57          response.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8");
58          response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes());
59  
60          ctx.write(response).addListener(ChannelFutureListener.CLOSE);
61      }
62  
63      @Override
64      public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
65          ctx.flush();
66      }
67  
68      @Override
69      public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
70          cause.printStackTrace();
71          ctx.close();
72      }
73  }