Search in sources :

Example 36 with HttpServerCodec

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpServerCodec in project benchmark by seelunzi.

the class ChildChannelHandler method initChannel.

@Override
protected void initChannel(SocketChannel e) throws Exception {
    // 设置30秒没有读到数据,则触发一个READER_IDLE事件。
    // pipeline.addLast(new IdleStateHandler(30, 0, 0));
    // HttpServerCodec:将请求和应答消息解码为HTTP消息
    e.pipeline().addLast("http-codec", new HttpServerCodec());
    // HttpObjectAggregator:将HTTP消息的多个部分合成一条完整的HTTP消息
    e.pipeline().addLast("aggregator", new HttpObjectAggregator(65536));
    // ChunkedWriteHandler:向客户端发送HTML5文件
    e.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
    // 在管道中添加我们自己的接收数据实现方法
    e.pipeline().addLast("handler", new MyWebSocketServerHandler());
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec)

Example 37 with HttpServerCodec

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpServerCodec in project benchmark by seelunzi.

the class WebsocketChatServerInitializer method initChannel.

@Override
public void initChannel(SocketChannel ch) throws Exception {
    // 2
    ChannelPipeline pipeline = ch.pipeline();
    pipeline.addLast(new HttpServerCodec());
    pipeline.addLast(new HttpObjectAggregator(64 * 1024));
    pipeline.addLast(new ChunkedWriteHandler());
    pipeline.addLast(new HttpRequestHandler("/ws"));
    pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));
    pipeline.addLast(new TextWebSocketFrameHandler());
}
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 38 with HttpServerCodec

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

the class GatewayHttpChannelInitializer method initChannel.

@Override
protected void initChannel(Channel channel) {
    ChannelPipeline pipeline = channel.pipeline();
    // contexs contexts contexs
    channel.pipeline().addLast(channelContextHandler);
    // set ssl if present
    if (config.getSslContext() != null) {
        SSLEngine sslEngine = config.getSslContext().createSSLEngine();
        sslEngine.setUseClientMode(false);
        pipeline.addLast(new SslHandler(sslEngine));
    }
    // add http codecs
    pipeline.addLast(new HttpServerCodec());
    pipeline.addLast(new HttpObjectAggregator(config.getMaxFrameLength(), true));
    // add CORS handler
    if (config.isCorsEnabled()) {
        pipeline.addLast(corsHeadersHandler);
    }
    // message acceptor
    pipeline.addLast(messageHandler);
    // at-least-something exception handler
    pipeline.addLast(new ChannelInboundHandlerAdapter() {

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable throwable) {
            // Hint: at this point one can look at throwable, make some exception translation, and via channelContext post
            // ChannelContextError event, and hence give business layer ability to react on low level system error events
            LOGGER.warn("Exception caught for channel {}, {}", ctx.channel(), throwable);
        }
    });
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) SSLEngine(javax.net.ssl.SSLEngine) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPipeline(io.netty.channel.ChannelPipeline) SslHandler(io.netty.handler.ssl.SslHandler) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 39 with HttpServerCodec

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

the class CleartextHttp2ServerUpgradeHandlerTest method setUpServerChannel.

private void setUpServerChannel() {
    frameListener = mock(Http2FrameListener.class);
    http2ConnectionHandler = new Http2ConnectionHandlerBuilder().frameListener(frameListener).build();
    UpgradeCodecFactory upgradeCodecFactory = new UpgradeCodecFactory() {

        @Override
        public UpgradeCodec newUpgradeCodec(CharSequence protocol) {
            return new Http2ServerUpgradeCodec(http2ConnectionHandler);
        }
    };
    userEvents = new ArrayList<Object>();
    HttpServerCodec httpServerCodec = new HttpServerCodec();
    HttpServerUpgradeHandler upgradeHandler = new HttpServerUpgradeHandler(httpServerCodec, upgradeCodecFactory);
    CleartextHttp2ServerUpgradeHandler handler = new CleartextHttp2ServerUpgradeHandler(httpServerCodec, upgradeHandler, http2ConnectionHandler);
    channel = new EmbeddedChannel(handler, new ChannelInboundHandlerAdapter() {

        @Override
        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
            userEvents.add(evt);
        }
    });
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) UpgradeCodecFactory(io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeCodecFactory) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpServerUpgradeHandler(io.netty.handler.codec.http.HttpServerUpgradeHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 40 with HttpServerCodec

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

the class CleartextHttp2ServerUpgradeHandlerTest method usedHttp2MultiplexCodec.

@Test
public void usedHttp2MultiplexCodec() throws Exception {
    final Http2MultiplexCodec http2Codec = new Http2MultiplexCodecBuilder(true, new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
        }
    }).build();
    UpgradeCodecFactory upgradeCodecFactory = new UpgradeCodecFactory() {

        @Override
        public UpgradeCodec newUpgradeCodec(CharSequence protocol) {
            return new Http2ServerUpgradeCodec(http2Codec);
        }
    };
    http2ConnectionHandler = http2Codec;
    userEvents = new ArrayList<Object>();
    HttpServerCodec httpServerCodec = new HttpServerCodec();
    HttpServerUpgradeHandler upgradeHandler = new HttpServerUpgradeHandler(httpServerCodec, upgradeCodecFactory);
    CleartextHttp2ServerUpgradeHandler handler = new CleartextHttp2ServerUpgradeHandler(httpServerCodec, upgradeHandler, http2Codec);
    channel = new EmbeddedChannel(handler, new ChannelInboundHandlerAdapter() {

        @Override
        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
            userEvents.add(evt);
        }
    });
    assertFalse(channel.writeInbound(Http2CodecUtil.connectionPrefaceBuf()));
    ByteBuf settingsFrame = settingsFrameBuf();
    assertTrue(channel.writeInbound(settingsFrame));
    assertEquals(1, userEvents.size());
    assertTrue(userEvents.get(0) instanceof PriorKnowledgeUpgradeEvent);
}
Also used : PriorKnowledgeUpgradeEvent(io.netty.handler.codec.http2.CleartextHttp2ServerUpgradeHandler.PriorKnowledgeUpgradeEvent) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) UpgradeCodecFactory(io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeCodecFactory) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpServerUpgradeHandler(io.netty.handler.codec.http.HttpServerUpgradeHandler) ByteBuf(io.netty.buffer.ByteBuf) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) ChannelInitializer(io.netty.channel.ChannelInitializer) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Aggregations

HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)54 ChannelPipeline (io.netty.channel.ChannelPipeline)35 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)30 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)11 ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)11 HttpServerUpgradeHandler (io.netty.handler.codec.http.HttpServerUpgradeHandler)10 Test (org.junit.Test)9 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)8 Channel (io.netty.channel.Channel)7 ChannelHandler (io.netty.channel.ChannelHandler)7 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)7 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)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 WebSocketServerProtocolHandler (io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler)6 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)6 SocketChannel (io.netty.channel.socket.SocketChannel)4 HttpContentCompressor (io.netty.handler.codec.http.HttpContentCompressor)4