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    *   https://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.netty5.example.http2.helloworld.server;
17  
18  import io.netty5.buffer.api.Buffer;
19  import io.netty5.channel.ChannelFutureListeners;
20  import io.netty5.channel.ChannelHandlerContext;
21  import io.netty5.channel.SimpleChannelInboundHandler;
22  import io.netty5.handler.codec.http.DefaultFullHttpResponse;
23  import io.netty5.handler.codec.http.FullHttpRequest;
24  import io.netty5.handler.codec.http.FullHttpResponse;
25  import io.netty5.handler.codec.http.HttpUtil;
26  
27  import static io.netty5.example.http2.helloworld.server.HelloWorldHttp2Handler.RESPONSE_BYTES;
28  import static io.netty5.handler.codec.http.HttpHeaderNames.CONNECTION;
29  import static io.netty5.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH;
30  import static io.netty5.handler.codec.http.HttpHeaderNames.CONTENT_TYPE;
31  import static io.netty5.handler.codec.http.HttpHeaderValues.CLOSE;
32  import static io.netty5.handler.codec.http.HttpHeaderValues.KEEP_ALIVE;
33  import static io.netty5.handler.codec.http.HttpResponseStatus.CONTINUE;
34  import static io.netty5.handler.codec.http.HttpResponseStatus.OK;
35  import static io.netty5.handler.codec.http.HttpVersion.HTTP_1_0;
36  import static io.netty5.handler.codec.http.HttpVersion.HTTP_1_1;
37  import static java.nio.charset.StandardCharsets.US_ASCII;
38  import static java.util.Objects.requireNonNull;
39  
40  /**
41   * HTTP handler that responds with a "Hello World"
42   */
43  public class HelloWorldHttp1Handler extends SimpleChannelInboundHandler<FullHttpRequest> {
44      private final String establishApproach;
45  
46      public HelloWorldHttp1Handler(String establishApproach) {
47          this.establishApproach = requireNonNull(establishApproach, "establishApproach");
48      }
49  
50      @Override
51      public void messageReceived(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception {
52          if (HttpUtil.is100ContinueExpected(req)) {
53              ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE, ctx.bufferAllocator().allocate(0)));
54          }
55  
56          final byte[] sourceBytes = (" - via " + req.protocolVersion() + " (" + establishApproach + ")")
57                  .getBytes(US_ASCII);
58          final Buffer content = ctx.bufferAllocator().allocate(RESPONSE_BYTES.length + sourceBytes.length)
59                  .writeBytes(RESPONSE_BYTES).writeBytes(sourceBytes);
60  
61          FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content);
62          response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
63          response.headers().setInt(CONTENT_LENGTH, response.payload().readableBytes());
64  
65          boolean keepAlive = HttpUtil.isKeepAlive(req);
66          if (keepAlive) {
67              if (req.protocolVersion().equals(HTTP_1_0)) {
68                  response.headers().set(CONNECTION, KEEP_ALIVE);
69              }
70              ctx.write(response);
71          } else {
72              // Tell the client we're going to close the connection.
73              response.headers().set(CONNECTION, CLOSE);
74              ctx.write(response).addListener(ctx, ChannelFutureListeners.CLOSE);
75          }
76      }
77  
78      @Override
79      public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
80          ctx.flush();
81      }
82  
83      @Override
84      public void channelExceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
85          cause.printStackTrace();
86          ctx.close();
87      }
88  }