Search in sources :

Example 26 with ChunkedWriteHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.stream.ChunkedWriteHandler in project intellij-community by JetBrains.

the class NettyUtil method addHttpServerCodec.

public static void addHttpServerCodec(@NotNull ChannelPipeline pipeline) {
    pipeline.addLast("httpRequestEncoder", new HttpResponseEncoder());
    // https://jetbrains.zendesk.com/agent/tickets/68315
    pipeline.addLast("httpRequestDecoder", new HttpRequestDecoder(16 * 1024, 16 * 1024, 8192));
    pipeline.addLast("httpObjectAggregator", new HttpObjectAggregator(MAX_CONTENT_LENGTH));
    // could be added earlier if HTTPS
    if (pipeline.get(ChunkedWriteHandler.class) == null) {
        pipeline.addLast("chunkedWriteHandler", new ChunkedWriteHandler());
    }
    pipeline.addLast("corsHandler", new CorsHandlerDoNotUseOwnLogger(CorsConfigBuilder.forAnyOrigin().shortCircuit().allowCredentials().allowNullOrigin().allowedRequestMethods(HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT, HttpMethod.DELETE, HttpMethod.HEAD, HttpMethod.PATCH).allowedRequestHeaders("origin", "accept", "authorization", "content-type", "x-ijt").build()));
}
Also used : HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder)

Example 27 with ChunkedWriteHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.stream.ChunkedWriteHandler in project intellij-community by JetBrains.

the class PortUnificationServerHandler method decode.

protected void decode(@NotNull ChannelHandlerContext context, @NotNull ByteBuf buffer) throws Exception {
    ChannelPipeline pipeline = context.pipeline();
    if (detectSsl && SslHandler.isEncrypted(buffer)) {
        SSLEngine engine = SSL_SERVER_CONTEXT.getValue().createSSLEngine();
        engine.setUseClientMode(false);
        pipeline.addLast(new SslHandler(engine), new ChunkedWriteHandler(), new PortUnificationServerHandler(delegatingHttpRequestHandler, false, detectGzip));
    } else {
        int magic1 = buffer.getUnsignedByte(buffer.readerIndex());
        int magic2 = buffer.getUnsignedByte(buffer.readerIndex() + 1);
        if (detectGzip && magic1 == 31 && magic2 == 139) {
            pipeline.addLast(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP), ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP), new PortUnificationServerHandler(delegatingHttpRequestHandler, detectSsl, false));
        } else if (isHttp(magic1, magic2)) {
            NettyUtil.addHttpServerCodec(pipeline);
            pipeline.addLast("delegatingHttpHandler", delegatingHttpRequestHandler);
            final Logger logger = Logger.getInstance(BuiltInServer.class);
            if (logger.isDebugEnabled()) {
                pipeline.addLast(new ChannelOutboundHandlerAdapter() {

                    @Override
                    public void write(ChannelHandlerContext context, Object message, ChannelPromise promise) throws Exception {
                        if (message instanceof HttpResponse) {
                            HttpResponse response = (HttpResponse) message;
                            logger.debug("OUT HTTP: " + response.toString());
                        }
                        super.write(context, message, promise);
                    }
                });
            }
        } else if (magic1 == 'C' && magic2 == 'H') {
            buffer.skipBytes(2);
            pipeline.addLast(new CustomHandlerDelegator());
        } else {
            Logger.getInstance(BuiltInServer.class).warn("unknown request, first two bytes " + magic1 + " " + magic2);
            context.close();
        }
    }
    // must be after new channels handlers addition (netty bug?)
    pipeline.remove(this);
    // Buffer will be automatically released after messageReceived, but we pass it to next handler, and next handler will also release, so, we must retain.
    // We can introduce Decoder.isAutoRelease, but in this case, if error will be thrown while we are executing, buffer will not be released.
    // So, it is robust solution just always release (Decoder does) and just retain (we - client) if autorelease behavior is not suitable.
    buffer.retain();
    // we must fire channel read - new added handler must read buffer
    context.fireChannelRead(buffer);
}
Also used : SSLEngine(javax.net.ssl.SSLEngine) HttpResponse(io.netty.handler.codec.http.HttpResponse) Logger(com.intellij.openapi.diagnostic.Logger) SslHandler(io.netty.handler.ssl.SslHandler) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler)

Example 28 with ChunkedWriteHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.stream.ChunkedWriteHandler in project async-http-client by AsyncHttpClient.

the class HttpStaticFileServerInitializer method initChannel.

@Override
public void initChannel(SocketChannel ch) {
    ChannelPipeline pipeline = ch.pipeline();
    pipeline.addLast(new HttpServerCodec());
    pipeline.addLast(new HttpObjectAggregator(65536));
    pipeline.addLast(new ChunkedWriteHandler());
    pipeline.addLast(new HttpStaticFileServerHandler());
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 29 with ChunkedWriteHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.stream.ChunkedWriteHandler in project ambry by linkedin.

the class NettyServerChannelInitializer 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();
        pipeline.addLast("sslHandler", new SslHandler(sslFactory.createSSLEngine(peerAddress.getHostName(), peerAddress.getPort(), SSLFactory.Mode.SERVER)));
    }
    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()).addLast("processor", new NettyMessageProcessor(nettyMetrics, nettyConfig, 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) ChannelPipeline(io.netty.channel.ChannelPipeline) SslHandler(io.netty.handler.ssl.SslHandler)

Example 30 with ChunkedWriteHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.stream.ChunkedWriteHandler in project ambry by linkedin.

the class NettyPerfClient method start.

/**
 * Starts the NettyPerfClient.
 * @throws InterruptedException
 */
protected void start() throws InterruptedException {
    logger.info("Starting NettyPerfClient");
    reporter.start();
    group = new NioEventLoopGroup(concurrency);
    b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            if (sslFactory != null) {
                ch.pipeline().addLast("sslHandler", new SslHandler(sslFactory.createSSLEngine(host, port, SSLFactory.Mode.CLIENT)));
            }
            ch.pipeline().addLast(new HttpClientCodec()).addLast(new ChunkedWriteHandler()).addLast(new ResponseHandler());
        }
    });
    logger.info("Connecting to {}:{}", host, port);
    b.remoteAddress(host, port);
    perfClientStartTime = System.currentTimeMillis();
    for (int i = 0; i < concurrency; i++) {
        b.connect().addListener(channelConnectListener);
    }
    isRunning = true;
    logger.info("Created {} channel(s)", concurrency);
    logger.info("NettyPerfClient started");
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) SslHandler(io.netty.handler.ssl.SslHandler)

Aggregations

ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)46 ChannelPipeline (io.netty.channel.ChannelPipeline)24 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)16 HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)11 SslHandler (io.netty.handler.ssl.SslHandler)10 HttpRequestDecoder (io.netty.handler.codec.http.HttpRequestDecoder)9 ByteBuf (io.netty.buffer.ByteBuf)8 SocketChannel (io.netty.channel.socket.SocketChannel)8 HttpResponseEncoder (io.netty.handler.codec.http.HttpResponseEncoder)8 LoggingHandler (io.netty.handler.logging.LoggingHandler)8 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)7 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)7 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)6 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)6 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)5 IOException (java.io.IOException)5 ChannelFuture (io.netty.channel.ChannelFuture)4 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)4 HttpClientCodec (io.netty.handler.codec.http.HttpClientCodec)4 HttpContentDecompressor (io.netty.handler.codec.http.HttpContentDecompressor)4