Search in sources :

Example 11 with ChunkedWriteHandler

use of io.netty.handler.stream.ChunkedWriteHandler in project cxf by apache.

the class NettyHttpClientPipelineFactory method initChannel.

@Override
protected void initChannel(Channel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    SslHandler sslHandler = configureClientSSLOnDemand();
    if (sslHandler != null) {
        LOG.log(Level.FINE, "Server SSL handler configured and added as an interceptor against the ChannelPipeline: {}", sslHandler);
        pipeline.addLast("ssl", sslHandler);
    }
    pipeline.addLast("decoder", new HttpResponseDecoder());
    // TODO need to configure the aggregator size
    pipeline.addLast("aggregator", new HttpObjectAggregator(1048576));
    pipeline.addLast("encoder", new HttpRequestEncoder());
    pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
    pipeline.addLast("client", new NettyHttpClientHandler());
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) HttpRequestEncoder(io.netty.handler.codec.http.HttpRequestEncoder) HttpResponseDecoder(io.netty.handler.codec.http.HttpResponseDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline) SslHandler(io.netty.handler.ssl.SslHandler)

Example 12 with ChunkedWriteHandler

use of io.netty.handler.stream.ChunkedWriteHandler in project jersey by jersey.

the class JerseyServerInitializer method configureClearText.

/**
     * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.
     */
private void configureClearText(SocketChannel ch) {
    final ChannelPipeline p = ch.pipeline();
    final HttpServerCodec sourceCodec = new HttpServerCodec();
    p.addLast(sourceCodec);
    p.addLast(new HttpServerUpgradeHandler(sourceCodec, new HttpServerUpgradeHandler.UpgradeCodecFactory() {

        @Override
        public HttpServerUpgradeHandler.UpgradeCodec newUpgradeCodec(CharSequence protocol) {
            if (AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) {
                return new Http2ServerUpgradeCodec(new Http2Codec(true, new JerseyHttp2ServerHandler(baseUri, container)));
            } else {
                return null;
            }
        }
    }));
    p.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.
            // "Directly talking: " + msg.protocolVersion() + " (no upgrade was attempted)");
            ChannelPipeline pipeline = ctx.pipeline();
            ChannelHandlerContext thisCtx = pipeline.context(this);
            pipeline.addAfter(thisCtx.name(), null, new JerseyServerHandler(baseUri, container));
            pipeline.replace(this, null, new ChunkedWriteHandler());
            ctx.fireChannelRead(msg);
        }
    });
}
Also used : Http2ServerUpgradeCodec(io.netty.handler.codec.http2.Http2ServerUpgradeCodec) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpServerUpgradeHandler(io.netty.handler.codec.http.HttpServerUpgradeHandler) ChannelPipeline(io.netty.channel.ChannelPipeline) Http2Codec(io.netty.handler.codec.http2.Http2Codec) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) HttpMessage(io.netty.handler.codec.http.HttpMessage)

Example 13 with ChunkedWriteHandler

use of io.netty.handler.stream.ChunkedWriteHandler in project vert.x by eclipse.

the class NetServerImpl method initChannel.

@Override
protected void initChannel(ChannelPipeline pipeline) {
    if (sslHelper.isSSL()) {
        SslHandler sslHandler = sslHelper.createSslHandler(vertx);
        pipeline.addLast("ssl", sslHandler);
    }
    if (logEnabled) {
        pipeline.addLast("logging", new LoggingHandler());
    }
    if (sslHelper.isSSL()) {
        // only add ChunkedWriteHandler when SSL is enabled otherwise it is not needed as FileRegion is used.
        // For large file / sendfile support
        pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
    }
    if (options.getIdleTimeout() > 0) {
        pipeline.addLast("idle", new IdleStateHandler(0, 0, options.getIdleTimeout()));
    }
}
Also used : LoggingHandler(io.netty.handler.logging.LoggingHandler) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) SslHandler(io.netty.handler.ssl.SslHandler)

Example 14 with ChunkedWriteHandler

use of io.netty.handler.stream.ChunkedWriteHandler in project netty by netty.

the class HttpChunkedInputTest method check.

private static void check(ChunkedInput<?>... inputs) {
    EmbeddedChannel ch = new EmbeddedChannel(new ChunkedWriteHandler());
    for (ChunkedInput<?> input : inputs) {
        ch.writeOutbound(input);
    }
    assertTrue(ch.finish());
    int i = 0;
    int read = 0;
    HttpContent lastHttpContent = null;
    for (; ; ) {
        HttpContent httpContent = ch.readOutbound();
        if (httpContent == null) {
            break;
        }
        if (lastHttpContent != null) {
            assertTrue("Chunk must be DefaultHttpContent", lastHttpContent instanceof DefaultHttpContent);
        }
        ByteBuf buffer = httpContent.content();
        while (buffer.isReadable()) {
            assertEquals(BYTES[i++], buffer.readByte());
            read++;
            if (i == BYTES.length) {
                i = 0;
            }
        }
        buffer.release();
        // Save last chunk
        lastHttpContent = httpContent;
    }
    assertEquals(BYTES.length * inputs.length, read);
    assertSame("Last chunk must be LastHttpContent.EMPTY_LAST_CONTENT", LastHttpContent.EMPTY_LAST_CONTENT, lastHttpContent);
}
Also used : ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf)

Example 15 with ChunkedWriteHandler

use of io.netty.handler.stream.ChunkedWriteHandler in project netty by netty.

the class SocketSslEchoTest method testSslEcho.

public void testSslEcho(ServerBootstrap sb, Bootstrap cb) throws Throwable {
    final ExecutorService delegatedTaskExecutor = Executors.newCachedThreadPool();
    reset();
    sb.childOption(ChannelOption.AUTO_READ, autoRead);
    cb.option(ChannelOption.AUTO_READ, autoRead);
    sb.childHandler(new ChannelInitializer<Channel>() {

        @Override
        @SuppressWarnings("deprecation")
        public void initChannel(Channel sch) throws Exception {
            serverChannel = sch;
            if (serverUsesDelegatedTaskExecutor) {
                SSLEngine sse = serverCtx.newEngine(sch.alloc());
                serverSslHandler = new SslHandler(sse, delegatedTaskExecutor);
            } else {
                serverSslHandler = serverCtx.newHandler(sch.alloc());
            }
            sch.pipeline().addLast("ssl", serverSslHandler);
            if (useChunkedWriteHandler) {
                sch.pipeline().addLast(new ChunkedWriteHandler());
            }
            sch.pipeline().addLast("handler", serverHandler);
        }
    });
    cb.handler(new ChannelInitializer<Channel>() {

        @Override
        @SuppressWarnings("deprecation")
        public void initChannel(Channel sch) throws Exception {
            clientChannel = sch;
            if (clientUsesDelegatedTaskExecutor) {
                SSLEngine cse = clientCtx.newEngine(sch.alloc());
                clientSslHandler = new SslHandler(cse, delegatedTaskExecutor);
            } else {
                clientSslHandler = clientCtx.newHandler(sch.alloc());
            }
            sch.pipeline().addLast("ssl", clientSslHandler);
            if (useChunkedWriteHandler) {
                sch.pipeline().addLast(new ChunkedWriteHandler());
            }
            sch.pipeline().addLast("handler", clientHandler);
        }
    });
    final Channel sc = sb.bind().sync().channel();
    cb.connect().sync();
    final Future<Channel> clientHandshakeFuture = clientSslHandler.handshakeFuture();
    clientChannel.writeAndFlush(Unpooled.wrappedBuffer(data, 0, FIRST_MESSAGE_SIZE));
    clientSendCounter.set(FIRST_MESSAGE_SIZE);
    clientHandshakeFuture.sync();
    boolean needsRenegotiation = renegotiation.type == RenegotiationType.CLIENT_INITIATED;
    Future<Channel> renegoFuture = null;
    while (clientSendCounter.get() < data.length) {
        int clientSendCounterVal = clientSendCounter.get();
        int length = Math.min(random.nextInt(1024 * 64), data.length - clientSendCounterVal);
        ByteBuf buf = Unpooled.wrappedBuffer(data, clientSendCounterVal, length);
        if (useCompositeByteBuf) {
            buf = Unpooled.compositeBuffer().addComponent(true, buf);
        }
        ChannelFuture future = clientChannel.writeAndFlush(buf);
        clientSendCounter.set(clientSendCounterVal += length);
        future.sync();
        if (needsRenegotiation && clientSendCounterVal >= data.length / 2) {
            needsRenegotiation = false;
            clientSslHandler.engine().setEnabledCipherSuites(new String[] { renegotiation.cipherSuite });
            renegoFuture = clientSslHandler.renegotiate();
            logStats("CLIENT RENEGOTIATES");
            assertThat(renegoFuture, is(not(sameInstance(clientHandshakeFuture))));
        }
    }
    // Ensure all data has been exchanged.
    while (clientRecvCounter.get() < data.length) {
        if (serverException.get() != null) {
            break;
        }
        if (serverException.get() != null) {
            break;
        }
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
        // Ignore.
        }
    }
    while (serverRecvCounter.get() < data.length) {
        if (serverException.get() != null) {
            break;
        }
        if (clientException.get() != null) {
            break;
        }
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
        // Ignore.
        }
    }
    // Wait until renegotiation is done.
    if (renegoFuture != null) {
        renegoFuture.sync();
    }
    if (serverHandler.renegoFuture != null) {
        serverHandler.renegoFuture.sync();
    }
    serverChannel.close().awaitUninterruptibly();
    clientChannel.close().awaitUninterruptibly();
    sc.close().awaitUninterruptibly();
    delegatedTaskExecutor.shutdown();
    if (serverException.get() != null && !(serverException.get() instanceof IOException)) {
        throw serverException.get();
    }
    if (clientException.get() != null && !(clientException.get() instanceof IOException)) {
        throw clientException.get();
    }
    if (serverException.get() != null) {
        throw serverException.get();
    }
    if (clientException.get() != null) {
        throw clientException.get();
    }
    // When renegotiation is done, at least the initiating side should be notified.
    try {
        switch(renegotiation.type) {
            case SERVER_INITIATED:
                assertThat(serverSslHandler.engine().getSession().getCipherSuite(), is(renegotiation.cipherSuite));
                assertThat(serverNegoCounter.get(), is(2));
                assertThat(clientNegoCounter.get(), anyOf(is(1), is(2)));
                break;
            case CLIENT_INITIATED:
                assertThat(serverNegoCounter.get(), anyOf(is(1), is(2)));
                assertThat(clientSslHandler.engine().getSession().getCipherSuite(), is(renegotiation.cipherSuite));
                assertThat(clientNegoCounter.get(), is(2));
                break;
            case NONE:
                assertThat(serverNegoCounter.get(), is(1));
                assertThat(clientNegoCounter.get(), is(1));
        }
    } finally {
        logStats("STATS");
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) SSLEngine(javax.net.ssl.SSLEngine) Channel(io.netty.channel.Channel) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) SslHandler(io.netty.handler.ssl.SslHandler) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) ExecutorService(java.util.concurrent.ExecutorService)

Aggregations

ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)21 ChannelPipeline (io.netty.channel.ChannelPipeline)12 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)6 SslHandler (io.netty.handler.ssl.SslHandler)6 SocketChannel (io.netty.channel.socket.SocketChannel)5 HttpRequestDecoder (io.netty.handler.codec.http.HttpRequestDecoder)5 LoggingHandler (io.netty.handler.logging.LoggingHandler)5 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)4 Channel (io.netty.channel.Channel)4 HttpResponseEncoder (io.netty.handler.codec.http.HttpResponseEncoder)4 HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)4 ByteBuf (io.netty.buffer.ByteBuf)3 ChannelFuture (io.netty.channel.ChannelFuture)3 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)3 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)3 HttpContentDecompressor (io.netty.handler.codec.http.HttpContentDecompressor)3 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)3 Bootstrap (io.netty.bootstrap.Bootstrap)2 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)2 HttpClientCodec (io.netty.handler.codec.http.HttpClientCodec)2