<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    皮杜妮

      BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
      887 Posts :: 3 Stories :: 7 Comments :: 0 Trackbacks
    netty是一套高性能的通訊架構(gòu),這里我用netty實(shí)現(xiàn)http服務(wù)器實(shí)現(xiàn)信息采集功能。主要是利用他現(xiàn)有的hander處理器,解析出request頭,做信息采集使用,重寫了他自己的hander.


    package io.netty.example.http.snoop;

    import static io.netty.handler.codec.http.HttpHeaders.getHost;
    import static io.netty.handler.codec.http.HttpHeaders.isKeepAlive;
    import static io.netty.handler.codec.http.HttpHeaders.Names.CONNECTION;
    import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_LENGTH;
    import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE;
    import static io.netty.handler.codec.http.HttpHeaders.Names.COOKIE;
    import static io.netty.handler.codec.http.HttpHeaders.Names.SET_COOKIE;
    import static io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST;
    import static io.netty.handler.codec.http.HttpResponseStatus.OK;
    import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;

    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    import java.util.Map.Entry;

    import io.netty.buffer.ByteBuf;
    import io.netty.buffer.Unpooled;
    import io.netty.channel.ChannelHandlerContext;
    import io.netty.channel.SimpleChannelInboundHandler;
    import io.netty.handler.codec.http.Cookie;
    import io.netty.handler.codec.http.CookieDecoder;
    import io.netty.handler.codec.http.DefaultFullHttpResponse;
    import io.netty.handler.codec.http.FullHttpResponse;
    import io.netty.handler.codec.http.HttpContent;
    import io.netty.handler.codec.http.HttpHeaders;
    import io.netty.handler.codec.http.HttpObject;
    import io.netty.handler.codec.http.HttpRequest;
    import io.netty.handler.codec.http.LastHttpContent;
    import io.netty.handler.codec.http.QueryStringDecoder;
    import io.netty.handler.codec.http.ServerCookieEncoder;
    import io.netty.util.CharsetUtil;

    public class HttpSnoopServiceTxt extends SimpleChannelInboundHandler<Object> {

        private HttpRequest request;
        /** Buffer that stores the response content */
        private final StringBuilder buf = new StringBuilder();

        @Override
        protected void channelRead0(ChannelHandlerContext ctx, Object msg)
                throws Exception {
            // TODO Auto-generated method stub
            if (msg instanceof HttpRequest) {
                HttpRequest request = this.request = (HttpRequest) msg;
                buf.setLength(0);
                // hostname
                buf.append("HOSTNAME:").append(getHost(request, "unknown"));
                // url
                buf.append("REQUEST_URI:").append(request.getUri());
                // parm
                QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.getUri());
                Map<String, List<String>> params = queryStringDecoder.parameters();
                if (!params.isEmpty()) {
                    for (Entry<String, List<String>> p : params.entrySet()) {
                        String key = p.getKey();
                        List<String> vals = p.getValue();
                        for (String val : vals) {
                            buf.append("PARAM:").append(key).append("=")
                                    .append(val);
                        }
                    }
                }
                //cookie
                
            }
            if (msg instanceof HttpContent) {
                if (msg instanceof LastHttpContent) {
                    LastHttpContent trailer = (LastHttpContent) msg;
                    writeResponse(trailer, ctx);
                    WriterFile.printtxt(buf.toString());
                }
            }
        }

        @Override
        public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
            ctx.flush();
        }

        private boolean writeResponse(HttpObject currentObj,ChannelHandlerContext ctx) {
            boolean keepAlive = isKeepAlive(request);
            FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1,
                    currentObj.getDecoderResult().isSuccess() ? OK : BAD_REQUEST,
                    Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8));
            response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
            if (keepAlive) {
                response.headers().set(CONTENT_LENGTH,
                        response.content().readableBytes());
                response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
            }
            ctx.write(response);
            return keepAlive;
        }

    }


    /*
     * Copyright 2012 The Netty Project
     *
     * The Netty Project licenses this file to you under the Apache License,
     * version 2.0 (the "License"); you may not use this file except in compliance
     * with the License. You may obtain a copy of the License at:
     *
     *   http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     * License for the specific language governing permissions and limitations
     * under the License.
     */
    package io.netty.example.http.snoop;

    import io.netty.bootstrap.ServerBootstrap;
    import io.netty.channel.Channel;
    import io.netty.channel.EventLoopGroup;
    import io.netty.channel.nio.NioEventLoopGroup;
    import io.netty.channel.socket.nio.NioServerSocketChannel;

    /**
     * An HTTP server that sends back the content of the received HTTP request
     * in a pretty plaintext form.
     */
    public class HttpSnoopServer {

        private final int port;

        public HttpSnoopServer(int port) {
            this.port = port;
        }

        public void run() throws Exception {
            // Configure the server.
            EventLoopGroup bossGroup = new NioEventLoopGroup();
            EventLoopGroup workerGroup = new NioEventLoopGroup();
            try {
                ServerBootstrap b = new ServerBootstrap();
                b.group(bossGroup, workerGroup)
                 .channel(NioServerSocketChannel.class)
                 .childHandler(new HttpSnoopServerInitializer());

                Channel ch = b.bind(port).sync().channel();
                ch.closeFuture().sync();
            } finally {
                bossGroup.shutdownGracefully();
                workerGroup.shutdownGracefully();
            }
        }

        public static void main(String[] args) throws Exception {
            int port;
            if (args.length > 0) {
                port = 8080;
            } else {
                port = 8080;
            }
            new HttpSnoopServer(port).run();
        }
    }
    /*
     * Copyright 2012 The Netty Project
     *
     * The Netty Project licenses this file to you under the Apache License,
     * version 2.0 (the "License"); you may not use this file except in compliance
     * with the License. You may obtain a copy of the License at:
     *
     *   http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     * License for the specific language governing permissions and limitations
     * under the License.
     */
    package io.netty.example.http.snoop;

    import io.netty.channel.ChannelInitializer;
    import io.netty.channel.ChannelPipeline;
    import io.netty.channel.socket.SocketChannel;
    import io.netty.handler.codec.http.HttpRequestDecoder;
    import io.netty.handler.codec.http.HttpResponseEncoder;

    public class HttpSnoopServerInitializer extends ChannelInitializer<SocketChannel> {
        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            // Create a default pipeline implementation.
            ChannelPipeline p = ch.pipeline();

            // Uncomment the following line if you want HTTPS
            //SSLEngine engine = SecureChatSslContextFactory.getServerContext().createSSLEngine();
            //engine.setUseClientMode(false);
            //p.addLast("ssl", new SslHandler(engine));

            p.addLast("decoder", new HttpRequestDecoder());
            // Uncomment the following line if you don't want to handle HttpChunks.
            //p.addLast("aggregator", new HttpObjectAggregator(1048576));
            p.addLast("encoder", new HttpResponseEncoder());
            // Remove the following line if you don't want automatic content compression.
            //p.addLast("deflater", new HttpContentCompressor());
            
            p.addLast("handler", new HttpSnoopServiceTxt());
            //p.addLast("handler", new HttpSnoopServerHandler());
        }
    }
    posted on 2013-11-23 15:54 豬眼睛 閱讀(1689) 評(píng)論(0)  編輯  收藏 所屬分類: netty
    主站蜘蛛池模板: 成人免费无码大片A毛片抽搐 | 2022久久国产精品免费热麻豆| 97国产在线公开免费观看| 日韩毛片免费无码无毒视频观看 | 国产亚洲精AA在线观看SEE| 亚洲人成777在线播放| 豆国产96在线|亚洲| 毛片免费在线观看| 青青青青青青久久久免费观看| 亚洲天堂免费在线视频| 亚洲一区二区三区四区视频 | 精品一区二区三区无码免费视频 | 一个人免费日韩不卡视频| 国产免费av一区二区三区| 91亚洲一区二区在线观看不卡| 亚洲爆乳精品无码一区二区| 亚欧日韩毛片在线看免费网站| 亚洲AV综合色区无码一区爱AV| 小说区亚洲自拍另类| 又粗又黄又猛又爽大片免费| 亚洲黄色网址大全| 成人免费观看一区二区| 亚洲韩国精品无码一区二区三区 | 亚洲综合久久精品无码色欲| 久久er国产精品免费观看2| 久久亚洲sm情趣捆绑调教| 69堂人成无码免费视频果冻传媒| 亚洲综合小说另类图片动图| 亚洲视频免费一区| 亚洲人成欧美中文字幕| 久久经典免费视频| 亚洲日本香蕉视频| 久久久精品2019免费观看 | 精精国产www视频在线观看免费| 美女黄网站人色视频免费国产 | 中国人免费观看高清在线观看二区 | 亚洲最大的视频网站| 日韩a级毛片免费视频| 亚洲综合色丁香婷婷六月图片| 国产一精品一aⅴ一免费| 成人免费乱码大片A毛片|