View Javadoc
1   /*
2    * Copyright 2013 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.netty.testsuite.svm;
17  
18  import io.netty.buffer.Unpooled;
19  import io.netty.channel.ChannelFutureListener;
20  import io.netty.channel.ChannelHandlerContext;
21  import io.netty.channel.SimpleChannelInboundHandler;
22  import io.netty.handler.codec.http.DefaultFullHttpResponse;
23  import io.netty.handler.codec.http.FullHttpResponse;
24  import io.netty.handler.codec.http.HttpObject;
25  import io.netty.handler.codec.http.HttpUtil;
26  import io.netty.handler.codec.http.HttpRequest;
27  import io.netty.util.AsciiString;
28  
29  import java.util.concurrent.CompletableFuture;
30  
31  import static io.netty.handler.codec.http.HttpHeaderNames.*;
32  import static io.netty.handler.codec.http.HttpResponseStatus.*;
33  import static io.netty.handler.codec.http.HttpVersion.*;
34  
35  public class HttpNativeServerHandler extends SimpleChannelInboundHandler<HttpObject> {
36      private static final byte[] CONTENT = { 'H', 'e', 'l', 'l', 'o', ' ', 'N', 'a', 't', 'i', 'v', 'e' };
37  
38      private static final AsciiString KEEP_ALIVE = AsciiString.cached("keep-alive");
39  
40      private final CompletableFuture<Void> httpRequestFuture;
41  
42      public HttpNativeServerHandler(CompletableFuture<Void> httpRequestFuture) {
43          this.httpRequestFuture = httpRequestFuture;
44      }
45  
46      @Override
47      public void channelReadComplete(ChannelHandlerContext ctx) {
48          ctx.flush();
49      }
50  
51      @Override
52      public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
53          if (msg instanceof HttpRequest) {
54              HttpRequest req = (HttpRequest) msg;
55  
56              boolean keepAlive = HttpUtil.isKeepAlive(req);
57              FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(CONTENT));
58              response.headers().set(CONTENT_TYPE, "text/plain");
59              response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes());
60              httpRequestFuture.complete(null);
61              if (!keepAlive) {
62                  ctx.write(response).addListener(ChannelFutureListener.CLOSE);
63              } else {
64                  response.headers().set(CONNECTION, KEEP_ALIVE);
65                  ctx.write(response);
66              }
67          }
68      }
69  
70      @Override
71      public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
72          httpRequestFuture.completeExceptionally(cause);
73          cause.printStackTrace();
74          ctx.close();
75      }
76  }