Search in sources :

Example 26 with HttpServerCodec

use of io.netty.handler.codec.http.HttpServerCodec in project riposte by Nike-Inc.

the class HttpChannelInitializerTest method initChannel_adds_HttpServerCodec_with_config_values_coming_from_httpRequestDecoderConfig.

@Test
public void initChannel_adds_HttpServerCodec_with_config_values_coming_from_httpRequestDecoderConfig() {
    // given
    HttpChannelInitializer hci = basicHttpChannelInitializerNoUtilityHandlers();
    HttpRequestDecoderConfig configWithCustomValues = new HttpRequestDecoderConfig() {

        @Override
        public int maxInitialLineLength() {
            return 123;
        }

        @Override
        public int maxHeaderSize() {
            return 456;
        }

        @Override
        public int maxChunkSize() {
            return 789;
        }
    };
    Whitebox.setInternalState(hci, "httpRequestDecoderConfig", configWithCustomValues);
    // when
    hci.initChannel(socketChannelMock);
    // then
    ArgumentCaptor<ChannelHandler> channelHandlerArgumentCaptor = ArgumentCaptor.forClass(ChannelHandler.class);
    verify(channelPipelineMock, atLeastOnce()).addLast(anyString(), channelHandlerArgumentCaptor.capture());
    List<ChannelHandler> handlers = channelHandlerArgumentCaptor.getAllValues();
    Pair<Integer, HttpServerCodec> httpServerCodecHandlerPair = findChannelHandler(handlers, HttpServerCodec.class);
    Assertions.assertThat(httpServerCodecHandlerPair).isNotNull();
    HttpServerCodec httpServerCodecHandler = httpServerCodecHandlerPair.getValue();
    HttpRequestDecoder decoderHandler = (HttpRequestDecoder) Whitebox.getInternalState(httpServerCodecHandler, "inboundHandler");
    int actualMaxInitialLineLength = extractField(extractField(decoderHandler, "lineParser"), "maxLength");
    int actualMaxHeaderSize = extractField(extractField(decoderHandler, "headerParser"), "maxLength");
    int actualMaxChunkSize = extractField(decoderHandler, "maxChunkSize");
    Assertions.assertThat(actualMaxInitialLineLength).isEqualTo(configWithCustomValues.maxInitialLineLength());
    Assertions.assertThat(actualMaxHeaderSize).isEqualTo(configWithCustomValues.maxHeaderSize());
    Assertions.assertThat(actualMaxChunkSize).isEqualTo(configWithCustomValues.maxChunkSize());
}
Also used : HttpRequestDecoderConfig(com.nike.riposte.server.config.ServerConfig.HttpRequestDecoderConfig) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) ChannelHandler(io.netty.channel.ChannelHandler) Endpoint(com.nike.riposte.server.http.Endpoint) Test(org.junit.Test)

Example 27 with HttpServerCodec

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

the class ITNettyHttpTracing method init.

@Override
protected void init() {
    stop();
    bossGroup = new NioEventLoopGroup(1);
    workerGroup = new NioEventLoopGroup();
    ServerBootstrap b = new ServerBootstrap();
    b.option(ChannelOption.SO_BACKLOG, 1024);
    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(final Channel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            p.addLast(new HttpServerCodec());
            p.addLast(NettyHttpTracing.create(httpTracing).serverHandler());
            p.addLast(new TestHandler(httpTracing));
        }
    });
    try {
        Channel ch = b.bind(0).sync().channel();
        port = ((InetSocketAddress) ch.localAddress()).getPort();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new AssertionError(e);
    }
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 28 with HttpServerCodec

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

the class NettyHttpServerBenchmarks method initServer.

@Override
protected int initServer() throws InterruptedException {
    bossGroup = new NioEventLoopGroup(1);
    workerGroup = new NioEventLoopGroup();
    ServerBootstrap b = new ServerBootstrap();
    b.option(ChannelOption.SO_BACKLOG, 1024);
    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(final Channel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            p.addLast(new HttpServerCodec());
            p.addLast(new TracingDispatchHandler());
            p.addLast(new HelloWorldHandler());
        }
    });
    Channel ch = b.bind(0).sync().channel();
    return ((InetSocketAddress) ch.localAddress()).getPort();
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) InetSocketAddress(java.net.InetSocketAddress) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) RunnerException(org.openjdk.jmh.runner.RunnerException) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 29 with HttpServerCodec

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

the class Web3WebSocketServer method start.

@Override
public void start() {
    logger.info("RPC WebSocket enabled");
    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            p.addLast(new HttpServerCodec());
            p.addLast(new HttpObjectAggregator(maxAggregatedFrameSize));
            p.addLast(new WriteTimeoutHandler(serverWriteTimeoutSeconds, TimeUnit.SECONDS));
            p.addLast(new RskWebSocketServerProtocolHandler("/websocket", maxFrameSize));
            p.addLast(new WebSocketFrameAggregator(maxAggregatedFrameSize));
            p.addLast(webSocketJsonRpcHandler);
            p.addLast(web3ServerHandler);
            p.addLast(new Web3ResultWebSocketResponseHandler());
        }
    });
    webSocketChannel = b.bind(host, port);
    try {
        webSocketChannel.sync();
    } catch (InterruptedException e) {
        logger.error("The RPC WebSocket server couldn't be started", e);
        Thread.currentThread().interrupt();
    }
}
Also used : WebSocketFrameAggregator(io.netty.handler.codec.http.websocketx.WebSocketFrameAggregator) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) WriteTimeoutHandler(io.netty.handler.timeout.WriteTimeoutHandler) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec)

Example 30 with HttpServerCodec

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

the class FrontendNettyChannelInitializer method initChannel.

@Override
protected void initChannel(SocketChannel ch) throws Exception {
    // If channel handler implementations are not annotated with @Sharable, Netty creates a new instance of every class
    // in the pipeline for every connection.
    // i.e. if there are a 1000 active connections there will be a 1000 NettyMessageProcessor instances.
    ChannelPipeline pipeline = ch.pipeline();
    // connection stats handler to track connection related metrics
    pipeline.addLast("connectionStatsHandler", connectionStatsHandler);
    // if SSL is enabled, add an SslHandler before the HTTP codec
    if (sslFactory != null) {
        InetSocketAddress peerAddress = ch.remoteAddress();
        String peerHost = peerAddress.getHostName();
        int peerPort = peerAddress.getPort();
        SslHandler sslHandler = new SslHandler(sslFactory.createSSLEngine(peerHost, peerPort, SSLFactory.Mode.SERVER));
        pipeline.addLast("sslHandler", sslHandler);
    }
    pipeline.addLast("codec", new HttpServerCodec(nettyConfig.nettyServerMaxInitialLineLength, nettyConfig.nettyServerMaxHeaderSize, nettyConfig.nettyServerMaxChunkSize)).addLast("healthCheckHandler", new HealthCheckHandler(restServerState, nettyMetrics)).addLast("publicAccessLogHandler", new PublicAccessLogHandler(publicAccessLogger, nettyMetrics)).addLast("idleStateHandler", new IdleStateHandler(0, 0, nettyConfig.nettyServerIdleTimeSeconds)).addLast("chunker", new ChunkedWriteHandler());
    if (addedChannelHandlers != null) {
        pipeline.addLast(addedChannelHandlers.toArray(new ChannelHandler[0]));
    }
    // custom processing class that interfaces with a RestRequestService.
    pipeline.addLast("processor", new NettyMessageProcessor(nettyMetrics, nettyConfig, performanceConfig, requestHandler));
}
Also used : ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) InetSocketAddress(java.net.InetSocketAddress) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) ChannelHandler(io.netty.channel.ChannelHandler) ChannelPipeline(io.netty.channel.ChannelPipeline) SslHandler(io.netty.handler.ssl.SslHandler)

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