View Javadoc
1   /*
2    * Copyright 2024 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  
17  package io.netty.testsuite_jpms.main;
18  
19  import io.netty.buffer.Unpooled;
20  import io.netty.channel.ChannelFuture;
21  import io.netty.channel.ChannelFutureListener;
22  import io.netty.channel.ChannelHandlerContext;
23  import io.netty.channel.SimpleChannelInboundHandler;
24  import io.netty.handler.codec.http.HttpObject;
25  import io.netty.handler.codec.http.HttpRequest;
26  import io.netty.handler.codec.http.HttpUtil;
27  import io.netty.handler.codec.http.FullHttpResponse;
28  import io.netty.handler.codec.http.DefaultFullHttpResponse;
29  
30  import java.nio.charset.StandardCharsets;
31  
32  import static io.netty.handler.codec.http.HttpHeaderNames.CONNECTION;
33  import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE;
34  import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH;
35  import static io.netty.handler.codec.http.HttpHeaderValues.KEEP_ALIVE;
36  import static io.netty.handler.codec.http.HttpHeaderValues.CLOSE;
37  import static io.netty.handler.codec.http.HttpHeaderValues.TEXT_PLAIN;
38  import static io.netty.handler.codec.http.HttpResponseStatus.OK;
39  
40  public class HttpHelloWorldServerHandler extends SimpleChannelInboundHandler<HttpObject> {
41  
42      @Override
43      public void channelReadComplete(ChannelHandlerContext ctx) {
44          ctx.flush();
45      }
46  
47      @Override
48      public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
49          if (msg instanceof HttpRequest) {
50              HttpRequest req = (HttpRequest) msg;
51  
52              String hello = HttpHelloWorldServer.content(ctx);
53  
54              boolean keepAlive = HttpUtil.isKeepAlive(req);
55              FullHttpResponse response = new DefaultFullHttpResponse(
56                      req.protocolVersion(), OK,
57                      Unpooled.copiedBuffer(hello, StandardCharsets.UTF_8));
58              response.headers()
59                      .set(CONTENT_TYPE, TEXT_PLAIN)
60                      .setInt(CONTENT_LENGTH, response.content().readableBytes());
61  
62              if (keepAlive) {
63                  if (!req.protocolVersion().isKeepAliveDefault()) {
64                      response.headers().set(CONNECTION, KEEP_ALIVE);
65                  }
66              } else {
67                  // Tell the client we're going to close the connection.
68                  response.headers().set(CONNECTION, CLOSE);
69              }
70  
71              ChannelFuture f = ctx.write(response);
72  
73              if (!keepAlive) {
74                  f.addListener(ChannelFutureListener.CLOSE);
75              }
76          }
77      }
78  
79      @Override
80      public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
81          cause.printStackTrace();
82          ctx.close();
83      }
84  }