Search in sources :

Example 1 with HttpServerCodec

use of io.netty.handler.codec.http.HttpServerCodec in project jersey by jersey.

the class JerseyServerInitializer method initChannel.

@Override
public void initChannel(SocketChannel ch) {
    if (http2) {
        if (sslCtx != null) {
            configureSsl(ch);
        } else {
            configureClearText(ch);
        }
    } else {
        ChannelPipeline p = ch.pipeline();
        if (sslCtx != null) {
            p.addLast(sslCtx.newHandler(ch.alloc()));
        }
        p.addLast(new HttpServerCodec());
        p.addLast(new ChunkedWriteHandler());
        p.addLast(new JerseyServerHandler(baseUri, container));
    }
}
Also used : ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 2 with HttpServerCodec

use of io.netty.handler.codec.http.HttpServerCodec in project apn-proxy by apn-proxy.

the class ApnProxyServerChannelInitializer method initChannel.

@Override
public void initChannel(SocketChannel channel) throws Exception {
    ChannelPipeline pipeline = channel.pipeline();
    pipeline.addLast("idlestate", new IdleStateHandler(0, 0, 3, TimeUnit.MINUTES));
    pipeline.addLast("idlehandler", new ApnProxyIdleHandler());
    pipeline.addLast("datalog", new LoggingHandler("PRE_BYTE_LOGGER", LogLevel.DEBUG));
    if (ApnProxyConfig.getConfig().getListenType() == ApnProxyListenType.SSL) {
        SSLEngine engine = ApnProxySSLContextFactory.createServerSSLSSLEngine();
        pipeline.addLast("apnproxy.encrypt", new SslHandler(engine));
    } else if (ApnProxyConfig.getConfig().getListenType() == ApnProxyListenType.AES) {
        byte[] key = ApnProxyConfig.getConfig().getKey();
        byte[] iv = ApnProxyConfig.getConfig().getIv();
        pipeline.addLast("apnproxy.encrypt", new ApnProxyAESEncoder(key, iv));
        pipeline.addLast("apnproxy.decrypt", new ApnProxyAESDecoder(key, iv));
    }
    pipeline.addLast("log", new LoggingHandler("BYTE_LOGGER", LogLevel.INFO));
    pipeline.addLast("codec", new HttpServerCodec());
    pipeline.addLast(ApnProxyPreHandler.HANDLER_NAME, new ApnProxyPreHandler());
    pipeline.addLast(ApnProxySchemaHandler.HANDLER_NAME, new ApnProxySchemaHandler());
}
Also used : LoggingHandler(io.netty.handler.logging.LoggingHandler) SSLEngine(javax.net.ssl.SSLEngine) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) ChannelPipeline(io.netty.channel.ChannelPipeline) SslHandler(io.netty.handler.ssl.SslHandler)

Example 3 with HttpServerCodec

use of io.netty.handler.codec.http.HttpServerCodec in project apn-proxy by apn-proxy.

the class HttpServerChannelInitializer method initChannel.

@Override
protected void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    pipeline.addLast("codec", new HttpServerCodec());
    pipeline.addLast("biz", new HttpServerHandler());
}
Also used : HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 4 with HttpServerCodec

use of io.netty.handler.codec.http.HttpServerCodec in project java-in-action by xinghalo.

the class ChatServerInitializer method initChannel.

@Override
protected void initChannel(Channel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    pipeline.addLast(new HttpServerCodec());
    pipeline.addLast(new ChunkedWriteHandler());
    pipeline.addLast(new HttpObjectAggregator(60 * 1024));
    pipeline.addLast(new HttpRequestHandler("/ws"));
    pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));
    pipeline.addLast(new TextWebSocketFrameHandler(group));
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) WebSocketServerProtocolHandler(io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 5 with HttpServerCodec

use of io.netty.handler.codec.http.HttpServerCodec in project swift by luastar.

the class HttpServer method start.

public void start() {
    // 设置线程名称
    ThreadFactoryBuilder threadFactoryBuilder = new ThreadFactoryBuilder();
    EventLoopGroup bossGroup = new NioEventLoopGroup(HttpConstant.SWIFT_BOSS_THREADS, threadFactoryBuilder.setNameFormat("boss-group-%d").build());
    EventLoopGroup workerGroup = new NioEventLoopGroup(HttpConstant.SWIFT_WORKER_THREADS, threadFactoryBuilder.setNameFormat("worker-group-%d").build());
    try {
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline pipeline = ch.pipeline();
                if (ssl) {
                    SelfSignedCertificate ssc = new SelfSignedCertificate();
                    SslContext sslContext = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
                    pipeline.addLast(sslContext.newHandler(ch.alloc()));
                }
                // 超时处理
                pipeline.addLast(new IdleStateHandler(0, 0, HttpConstant.SWIFT_TIMEOUT));
                // http request decode and response encode
                pipeline.addLast(new HttpServerCodec());
                // 将消息头和体聚合成FullHttpRequest和FullHttpResponse
                pipeline.addLast(new HttpObjectAggregator(HttpConstant.SWIFT_MAX_CONTENT_LENGTH));
                // 压缩处理
                pipeline.addLast(new HttpContentCompressor(HttpConstant.SWIFT_COMPRESSION_LEVEL));
                // 自定义http服务
                pipeline.addLast(new HttpChannelHandler(handlerMapping));
            }
        });
        Channel channel = bootstrap.bind(port).sync().channel();
        channel.closeFuture().sync();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) HttpContentCompressor(io.netty.handler.codec.http.HttpContentCompressor) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext)

Aggregations

HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)62 ChannelPipeline (io.netty.channel.ChannelPipeline)40 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)34 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)12 ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)12 HttpServerUpgradeHandler (io.netty.handler.codec.http.HttpServerUpgradeHandler)11 Test (org.junit.Test)11 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)10 ChannelHandler (io.netty.channel.ChannelHandler)10 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)9 Channel (io.netty.channel.Channel)8 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)8 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)8 WebSocketServerProtocolHandler (io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler)7 ChannelDuplexHandler (io.netty.channel.ChannelDuplexHandler)6 ChannelHandlerAdapter (io.netty.channel.ChannelHandlerAdapter)6 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)6 LineBasedFrameDecoder (io.netty.handler.codec.LineBasedFrameDecoder)6 SocketChannel (io.netty.channel.socket.SocketChannel)5 HttpContentCompressor (io.netty.handler.codec.http.HttpContentCompressor)5