View Javadoc
1   /*
2    * Copyright 2014 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  package io.netty.example.http2.helloworld.server;
17  
18  import static io.netty.handler.codec.http.HttpHeaderNames.CONNECTION;
19  import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH;
20  import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE;
21  import static io.netty.handler.codec.http.HttpResponseStatus.CONTINUE;
22  import static io.netty.handler.codec.http.HttpResponseStatus.OK;
23  import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
24  import static io.netty.util.internal.ObjectUtil.checkNotNull;
25  
26  import io.netty.buffer.ByteBuf;
27  import io.netty.buffer.ByteBufUtil;
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.FullHttpRequest;
33  import io.netty.handler.codec.http.FullHttpResponse;
34  import io.netty.handler.codec.http.HttpUtil;
35  import io.netty.handler.codec.http.HttpHeaderValues;
36  
37  /**
38   * HTTP handler that responds with a "Hello World"
39   */
40  public class HelloWorldHttp1Handler extends SimpleChannelInboundHandler<FullHttpRequest> {
41      private final String establishApproach;
42  
43      public HelloWorldHttp1Handler(String establishApproach) {
44          this.establishApproach = checkNotNull(establishApproach, "establishApproach");
45      }
46  
47      @Override
48      public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception {
49          if (HttpUtil.is100ContinueExpected(req)) {
50              ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
51          }
52          boolean keepAlive = HttpUtil.isKeepAlive(req);
53  
54          ByteBuf content = ctx.alloc().buffer();
55          content.writeBytes(HelloWorldHttp2Handler.RESPONSE_BYTES.duplicate());
56          ByteBufUtil.writeAscii(content, " - via " + req.protocolVersion() + " (" + establishApproach + ")");
57  
58          FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content);
59          response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
60          response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes());
61  
62          if (!keepAlive) {
63              ctx.write(response).addListener(ChannelFutureListener.CLOSE);
64          } else {
65              response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE);
66              ctx.write(response);
67          }
68      }
69  
70      @Override
71      public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
72          ctx.flush();
73      }
74  
75      @Override
76      public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
77          cause.printStackTrace();
78          ctx.close();
79      }
80  }