content: /*
   * Copyright (c) 2019 abc123lzf <abc123lzf@126.com>
   *
   * Permission is hereby granted, free of charge, to any person obtaining a copy
   * of this software and associated documentation files (the "Software"), to deal
   * in the Software without restriction, including without limitation the rights
   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   * copies of the Software, and to permit persons to whom the Software is
   * furnished to do so, subject to the following conditions:
   *
   * The above copyright notice and this permission notice shall be included in all
   * copies or substantial portions of the Software.
   *
   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
   * SOFTWARE.
   */
  package com.lzf.flyingsocks.client.proxy.http;
  
  import io.netty.bootstrap.ServerBootstrap;
  import io.netty.channel.ChannelHandlerContext;
  import io.netty.channel.ChannelInboundHandlerAdapter;
  import io.netty.channel.ChannelInitializer;
  import io.netty.channel.ChannelPipeline;
  import io.netty.channel.nio.NioEventLoopGroup;
  import io.netty.channel.socket.SocketChannel;
  import io.netty.channel.socket.nio.NioServerSocketChannel;
  import io.netty.handler.codec.http.HttpRequest;
  import io.netty.handler.codec.http.HttpServerCodec;
  import io.netty.util.ReferenceCountUtil;
  
  import java.util.concurrent.locks.LockSupport;
  
  /**
   * @author lzf abc123lzf@126.com
   * @since 2021/1/28 19:41
   */
  public class HttpProxyTest {
  
  
      public static void main(String[] args) {
          NioEventLoopGroup loopGroup = new NioEventLoopGroup(1);
          ServerBootstrap bootstrap = new ServerBootstrap();
          bootstrap.group(loopGroup)
                  .channel(NioServerSocketChannel.class)
                  .childHandler(new ChannelInitializer<SocketChannel>() {
                      @Override
                      protected void initChannel(SocketChannel ch) {
                          ChannelPipeline pipeline = ch.pipeline();
                          pipeline.addLast(new HttpServerCodec());
                          //pipeline.addLast(new HttpObjectAggregator(512 * 1024));
                          pipeline.addLast(new Processor());
                      }
                  });
  
          bootstrap.bind(10800);
          LockSupport.park();
      }
  
  
      static class Processor extends ChannelInboundHandlerAdapter {
  
          @Override
          public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
              if (msg instanceof HttpRequest) {
                  HttpRequest request = (HttpRequest) msg;
                  System.out.println(request.method() + " " + request.uri() + " " + request.protocolVersion());
  
                  request.headers().forEach(e -> {
                      System.out.println(e.getKey() + ": " + e.getValue());
                  });
              }
  
              ReferenceCountUtil.release(msg);
          }
      }
  }
  