Search in sources :

Example 61 with HttpObjectAggregator

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpObjectAggregator in project duangframework by tcrct.

the class HttpChannelInitializer method initChannel.

@Override
protected void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline p = ch.pipeline();
    if (bootStrap.isSslEnabled()) {
        sslContext = bootStrap.getSslContext();
    }
    // HttpServerCodec包含了默认的HttpRequestDecoder(请求消息解码器)和HttpResponseEncoder(响应解码器)
    p.addLast(new HttpServerCodec());
    // 为http响应内容添加gizp压缩器
    p.addLast(new HttpContentCompressor());
    // 目的是将多个消息转换为单一的request或者response对象
    p.addLast(new HttpObjectAggregator(1048576));
    // 目的是支持异步大文件传输
    p.addLast(new ChunkedWriteHandler());
    // 真正处理HTTP业务逻辑的地方,针对每个TCP连接创建一个新的ChannelHandler实例
    p.addLast(new HttpBaseHandler(bootStrap));
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) HttpContentCompressor(io.netty.handler.codec.http.HttpContentCompressor) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) ChannelPipeline(io.netty.channel.ChannelPipeline) HttpBaseHandler(com.duangframework.server.netty.handler.HttpBaseHandler)

Example 62 with HttpObjectAggregator

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpObjectAggregator in project java by wavefrontHQ.

the class PlainTextOrHttpFrameDecoder method decode.

/**
 * Dynamically adds the appropriate encoder/decoder(s) to the pipeline based on the detected
 * protocol.
 */
@Override
protected void decode(final ChannelHandlerContext ctx, final ByteBuf buffer, List<Object> out) throws Exception {
    // read the first 2 bytes to use for protocol detection
    if (buffer.readableBytes() < 2) {
        logger.warning("buffer has less that 2 readable bytes");
        return;
    }
    final int firstByte = buffer.getUnsignedByte(buffer.readerIndex());
    final int secondByte = buffer.getUnsignedByte(buffer.readerIndex() + 1);
    // determine the protocol and add the encoder/decoder
    final ChannelPipeline pipeline = ctx.pipeline();
    if (isHttp(firstByte, secondByte)) {
        pipeline.addLast("decoder", new HttpRequestDecoder());
        pipeline.addLast("encoder", new HttpResponseEncoder());
        pipeline.addLast("aggregator", new HttpObjectAggregator(16 * 1024 * 1024));
        pipeline.addLast("deflate", new HttpContentCompressor());
        pipeline.addLast("handler", this.handler);
    } else {
        pipeline.addLast("line", new LineBasedFrameDecoder(4096));
        pipeline.addLast("decoder", STRING_DECODER);
        pipeline.addLast("encoder", STRING_ENCODER);
        pipeline.addLast("handler", this.handler);
    }
    pipeline.remove(this);
}
Also used : HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) HttpContentCompressor(io.netty.handler.codec.http.HttpContentCompressor) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 63 with HttpObjectAggregator

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpObjectAggregator in project summer by foxsugar.

the class WebSocketServerInitializer method initChannel.

@Override
protected void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    // 处理日志
    // pipeline.addLast(new LoggingHandler(LogLevel.INFO));
    // 处理心跳
    // pipeline.addLast(new IdleStateHandler(15, 0, 0, TimeUnit.SECONDS));
    // pipeline.addLast(new ChatHeartbeatHandler());
    // // 获取职责链
    // pipeline.addLast("http-codec", new HttpServerCodec());
    // pipeline.addLast("aggregator", new HttpObjectAggregator(64*1024));
    // pipeline.addLast("http-chunked", new ChunkedWriteHandler());
    // //        pipeline.addLast(new WebSocketServerCompressionHandler());
    // 
    // //        pipeline.addLast("encoder", new WsEncoder());
    // //        pipeline.addLast("decoder", new WsDecoder());
    // pipeline.addLast("handshake",new WebSocketServerProtocolHandler("/ws"));
    // pipeline.addLast(new WsCodec());
    // //        pipeline.addLast(new WsGameMsgHandler());
    // pipeline.addLast(new WsHandler());
    // //        pipeline.addLast("codec", new WsEncoder());
    // //websocket定义了传递数据的6中frame类型
    // //        pipeline.addLast(new GameMsgHandler());
    // 
    pipeline.addLast(new HttpServerCodec());
    pipeline.addLast(new HttpObjectAggregator(64 * 1024));
    pipeline.addLast(new WebSocketServerProtocolHandler("/websocket", null, true));
    pipeline.addLast(new WsCodec());
    pipeline.addLast(new GameMsgHandler());
// ChannelPipeline pipeline = ch.pipeline();
// pipeline.addLast(new HttpServerCodec());
// pipeline.addLast(new HttpObjectAggregator(65536));
// //        pipeline.addLast(new WebSocketServerCompressionHandler());
// //        pipeline.addLast(new WebSocketServerProtocolHandler(WEBSOCKET_PATH, null, true));
// //        pipeline.addLast(new WebSocketIndexPageHandler(WEBSOCKET_PATH));
// pipeline.addLast(new WebSocketServerHandler());
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) GameMsgHandler(com.code.server.gate.handle.GameMsgHandler) WsCodec(com.code.server.gate.encoding.WsCodec) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) WebSocketServerProtocolHandler(io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 64 with HttpObjectAggregator

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpObjectAggregator in project dubbo by alibaba.

the class QosProcessHandler method decode.

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (in.readableBytes() < 1) {
        return;
    }
    // read one byte to guess protocol
    final int magic = in.getByte(in.readerIndex());
    ChannelPipeline p = ctx.pipeline();
    p.addLast(new LocalHostPermitHandler(acceptForeignIp));
    if (isHttp(magic)) {
        // no welcome output for http protocol
        if (welcomeFuture != null && welcomeFuture.isCancellable()) {
            welcomeFuture.cancel(false);
        }
        p.addLast(new HttpServerCodec());
        p.addLast(new HttpObjectAggregator(1048576));
        p.addLast(new HttpProcessHandler());
        p.remove(this);
    } else {
        p.addLast(new LineBasedFrameDecoder(2048));
        p.addLast(new StringDecoder(CharsetUtil.UTF_8));
        p.addLast(new StringEncoder(CharsetUtil.UTF_8));
        p.addLast(new IdleStateHandler(0, 0, 5 * 60));
        p.addLast(new TelnetIdleEventHandler());
        p.addLast(new TelnetProcessHandler());
        p.remove(this);
    }
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 65 with HttpObjectAggregator

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpObjectAggregator in project cxf by apache.

the class NettyHttpServletPipelineFactory method getDefaultHttp2ChannelPipeline.

protected ChannelPipeline getDefaultHttp2ChannelPipeline(Channel channel) throws Exception {
    // Create a default pipeline implementation with HTTP/2 support
    ChannelPipeline pipeline = channel.pipeline();
    SslContext sslCtx = configureServerHttp2SSLOnDemand();
    if (sslCtx != null) {
        final SslHandler sslHandler = sslCtx.newHandler(channel.alloc());
        LOG.log(Level.FINE, "Server SSL handler configured and added as an interceptor against the ChannelPipeline: {}", sslHandler);
        pipeline.addLast(sslHandler, new Http2OrHttpHandler());
        return pipeline;
    }
    final UpgradeCodecFactory upgradeCodecFactory = new UpgradeCodecFactory() {

        @Override
        public UpgradeCodec newUpgradeCodec(CharSequence protocol) {
            if (AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) {
                return new Http2ServerUpgradeCodec(Http2FrameCodecBuilder.forServer().build(), new Http2MultiplexHandler(createHttp2ChannelInitializer()));
            } else {
                return null;
            }
        }
    };
    final HttpServerCodec sourceCodec = new HttpServerCodec();
    final HttpServerUpgradeHandler upgradeHandler = new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory);
    final CleartextHttp2ServerUpgradeHandler cleartextUpgradeHandler = new CleartextHttp2ServerUpgradeHandler(sourceCodec, upgradeHandler, createHttp2ChannelInitializerPriorKnowledge());
    pipeline.addLast(cleartextUpgradeHandler);
    pipeline.addLast(new SimpleChannelInboundHandler<HttpMessage>() {

        @Override
        protected void channelRead0(ChannelHandlerContext ctx, HttpMessage msg) throws Exception {
            // If this handler is hit then no upgrade has been attempted and the client is just talking HTTP
            final ChannelPipeline pipeline = ctx.pipeline();
            pipeline.addAfter(applicationExecutor, ctx.name(), "handler", getServletHandler());
            pipeline.replace(this, "aggregator", new HttpObjectAggregator(maxChunkContentSize));
            // Remove the following line if you don't want automatic content compression.
            pipeline.addLast("deflater", new HttpContentCompressor());
            // Set up the idle handler
            pipeline.addLast("idle", new IdleStateHandler(nettyHttpServerEngine.getReadIdleTime(), nettyHttpServerEngine.getWriteIdleTime(), 0));
            ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
        }
    });
    return pipeline;
}
Also used : CleartextHttp2ServerUpgradeHandler(io.netty.handler.codec.http2.CleartextHttp2ServerUpgradeHandler) HttpContentCompressor(io.netty.handler.codec.http.HttpContentCompressor) Http2ServerUpgradeCodec(io.netty.handler.codec.http2.Http2ServerUpgradeCodec) UpgradeCodecFactory(io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeCodecFactory) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpServerUpgradeHandler(io.netty.handler.codec.http.HttpServerUpgradeHandler) ChannelPipeline(io.netty.channel.ChannelPipeline) SslHandler(io.netty.handler.ssl.SslHandler) Http2MultiplexHandler(io.netty.handler.codec.http2.Http2MultiplexHandler) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) HttpMessage(io.netty.handler.codec.http.HttpMessage) SslContext(io.netty.handler.ssl.SslContext)

Aggregations

HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)95 ChannelPipeline (io.netty.channel.ChannelPipeline)60 HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)34 HttpRequestDecoder (io.netty.handler.codec.http.HttpRequestDecoder)29 HttpResponseEncoder (io.netty.handler.codec.http.HttpResponseEncoder)28 HttpClientCodec (io.netty.handler.codec.http.HttpClientCodec)25 SocketChannel (io.netty.channel.socket.SocketChannel)20 ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)18 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)17 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)17 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)16 Bootstrap (io.netty.bootstrap.Bootstrap)13 Channel (io.netty.channel.Channel)12 EventLoopGroup (io.netty.channel.EventLoopGroup)11 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)11 SslHandler (io.netty.handler.ssl.SslHandler)11 HttpContentCompressor (io.netty.handler.codec.http.HttpContentCompressor)10 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)9 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)8 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)8