View Javadoc
1   /*
2    * Copyright 2017 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.testsuite.http2;
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.HttpHeaderValues;
26  import io.netty5.handler.codec.http.HttpUtil;
27  
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.HttpResponseStatus.CONTINUE;
32  import static io.netty5.handler.codec.http.HttpResponseStatus.OK;
33  import static io.netty5.handler.codec.http.HttpVersion.HTTP_1_1;
34  import static java.nio.charset.StandardCharsets.US_ASCII;
35  import static java.util.Objects.requireNonNull;
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      HelloWorldHttp1Handler(String establishApproach) {
44          this.establishApproach = requireNonNull(establishApproach, "establishApproach");
45      }
46  
47      @Override
48      public void messageReceived(ChannelHandlerContext ctx, FullHttpRequest req) {
49          if (HttpUtil.is100ContinueExpected(req)) {
50              ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE, ctx.bufferAllocator().allocate(0)));
51          }
52          boolean keepAlive = HttpUtil.isKeepAlive(req);
53  
54          FullHttpResponse response;
55          try (Buffer responseBytes = HelloWorldHttp2Handler.RESPONSE_BYTES_SUPPLIER.get()) {
56              final Buffer content = responseBytes.copy()
57                      .writeCharSequence(" - via " + req.protocolVersion() + " (" + establishApproach + ')', US_ASCII);
58  
59              response = new DefaultFullHttpResponse(HTTP_1_1, OK, content);
60          }
61          response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
62          response.headers().setInt(CONTENT_LENGTH, response.payload().readableBytes());
63  
64          if (!keepAlive) {
65              ctx.write(response).addListener(ctx, ChannelFutureListeners.CLOSE);
66          } else {
67              response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE);
68              ctx.write(response);
69          }
70      }
71  
72      @Override
73      public void channelReadComplete(ChannelHandlerContext ctx) {
74          ctx.flush();
75      }
76  
77      @Override
78      public void channelExceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
79          cause.printStackTrace();
80          ctx.close();
81      }
82  }