查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
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.example.http.helloworld;
17  
18  import io.netty.buffer.Unpooled;
19  import io.netty.channel.ChannelFuture;
20  import io.netty.channel.ChannelFutureListener;
21  import io.netty.channel.ChannelHandlerContext;
22  import io.netty.channel.SimpleChannelInboundHandler;
23  import io.netty.handler.codec.http.DefaultFullHttpResponse;
24  import io.netty.handler.codec.http.FullHttpResponse;
25  import io.netty.handler.codec.http.HttpObject;
26  import io.netty.handler.codec.http.HttpRequest;
27  import io.netty.handler.codec.http.HttpUtil;
28  
29  import static io.netty.handler.codec.http.HttpHeaderNames.CONNECTION;
30  import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH;
31  import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE;
32  import static io.netty.handler.codec.http.HttpHeaderValues.CLOSE;
33  import static io.netty.handler.codec.http.HttpHeaderValues.KEEP_ALIVE;
34  import static io.netty.handler.codec.http.HttpHeaderValues.TEXT_PLAIN;
35  import static io.netty.handler.codec.http.HttpResponseStatus.OK;
36  
37  public class HttpHelloWorldServerHandler extends SimpleChannelInboundHandler<HttpObject> {
38      private static final byte[] CONTENT = { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd' };
39  
40      @Override
41      public void channelReadComplete(ChannelHandlerContext ctx) {
42          ctx.flush();
43      }
44  
45      @Override
46      public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
47          if (msg instanceof HttpRequest) {
48              HttpRequest req = (HttpRequest) msg;
49  
50              boolean keepAlive = HttpUtil.isKeepAlive(req);
51              FullHttpResponse response = new DefaultFullHttpResponse(req.protocolVersion(), OK,
52                                                                      Unpooled.wrappedBuffer(CONTENT));
53              response.headers()
54                      .set(CONTENT_TYPE, TEXT_PLAIN)
55                      .setInt(CONTENT_LENGTH, response.content().readableBytes());
56  
57              if (keepAlive) {
58                  if (!req.protocolVersion().isKeepAliveDefault()) {
59                      response.headers().set(CONNECTION, KEEP_ALIVE);
60                  }
61              } else {
62                  // Tell the client we're going to close the connection.
63                  response.headers().set(CONNECTION, CLOSE);
64              }
65  
66              ChannelFuture f = ctx.write(response);
67  
68              if (!keepAlive) {
69                  f.addListener(ChannelFutureListener.CLOSE);
70              }
71          }
72      }
73  
74      @Override
75      public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
76          cause.printStackTrace();
77          ctx.close();
78      }
79  }