Search in sources :

Example 46 with HttpServerCodec

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

the class Http2ClientTest method createH2CServer.

private ServerBootstrap createH2CServer(BiFunction<Http2ConnectionDecoder, Http2ConnectionEncoder, Http2FrameListener> handler, boolean upgrade) {
    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.channel(NioServerSocketChannel.class);
    bootstrap.group(new NioEventLoopGroup());
    bootstrap.childHandler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            if (upgrade) {
                HttpServerCodec sourceCodec = new HttpServerCodec();
                HttpServerUpgradeHandler.UpgradeCodecFactory upgradeCodecFactory = protocol -> {
                    if (AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) {
                        Http2ConnectionHandler httpConnectionHandler = createHttpConnectionHandler((a, b) -> {
                            return new Http2FrameListenerDecorator(handler.apply(a, b)) {

                                @Override
                                public void onSettingsRead(ChannelHandlerContext ctx, io.netty.handler.codec.http2.Http2Settings settings) throws Http2Exception {
                                    super.onSettingsRead(ctx, settings);
                                    Http2Connection conn = a.connection();
                                    Http2Stream stream = conn.stream(1);
                                    DefaultHttp2Headers blah = new DefaultHttp2Headers();
                                    blah.status("200");
                                    b.frameWriter().writeHeaders(ctx, 1, blah, 0, true, ctx.voidPromise());
                                }
                            };
                        });
                        return new Http2ServerUpgradeCodec(httpConnectionHandler);
                    } else {
                        return null;
                    }
                };
                ch.pipeline().addLast(sourceCodec);
                ch.pipeline().addLast(new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory));
            } else {
                Http2ConnectionHandler clientHandler = createHttpConnectionHandler(handler);
                ch.pipeline().addLast("handler", clientHandler);
            }
        }
    });
    return bootstrap;
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpServerUpgradeHandler(io.netty.handler.codec.http.HttpServerUpgradeHandler) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ConnectException(java.net.ConnectException) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 47 with HttpServerCodec

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpServerCodec 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)

Example 48 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 49 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 50 with HttpServerCodec

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

the class EmbedHttpServer method start.

public void start(GenericFutureListener startedListener) {
    NioEventLoopGroup bossGroup = new NioEventLoopGroup(2);
    NioEventLoopGroup workGroup = new NioEventLoopGroup(2);
    try {
        ServerBootstrap bootstrap = new ServerBootstrap().group(bossGroup, workGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
                ch.pipeline().addLast("httpCodec", new HttpServerCodec());
                ch.pipeline().addLast(new HttpObjectAggregator(4194304));
                ch.pipeline().addLast("serverHandle", new SimpleChannelInboundHandler<FullHttpRequest>() {

                    @Override
                    protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
                        URI uri = new URI(request.uri());
                        FullHttpResponse httpResponse = invoke(uri.getPath(), ctx.channel(), request);
                        if (httpResponse != null) {
                            httpResponse.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
                            httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, httpResponse.content().readableBytes());
                            ch.writeAndFlush(httpResponse);
                        }
                    }

                    @Override
                    public void channelUnregistered(ChannelHandlerContext ctx) {
                        ctx.channel().close();
                    }

                    @Override
                    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
                        LOGGER.error("native request error", cause.getCause() == null ? cause : cause.getCause());
                        Map<String, Object> data = new HashMap<>();
                        data.put("error", cause.getCause().toString());
                        FullHttpResponse httpResponse = HttpHandlerUtil.buildJson(data);
                        httpResponse.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
                        ctx.channel().writeAndFlush(httpResponse);
                    }
                });
            }
        });
        ChannelFuture f = bootstrap.bind("127.0.0.1", port).sync();
        if (startedListener != null) {
            f.addListener(startedListener);
        }
        f.channel().closeFuture().sync();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        bossGroup.shutdownGracefully();
        workGroup.shutdownGracefully();
    }
}
Also used : SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) ChannelFuture(io.netty.channel.ChannelFuture) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HashMap(java.util.HashMap) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) URI(java.net.URI) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

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