Search in sources :

Example 6 with HttpContentDecompressor

use of io.netty.handler.codec.http.HttpContentDecompressor in project proxyee-down by monkeyWie.

the class ResponseTextIntercept method afterResponse.

@Override
public void afterResponse(Channel clientChannel, Channel proxyChannel, HttpResponse httpResponse, HttpProxyInterceptPipeline pipeline) throws Exception {
    if (match(httpResponse, pipeline)) {
        isMatch = true;
        // 解压gzip响应
        if ("gzip".equalsIgnoreCase(httpResponse.headers().get(HttpHeaderNames.CONTENT_ENCODING))) {
            isGzip = true;
            pipeline.reset3();
            proxyChannel.pipeline().addAfter("httpCodec", "decompress", new HttpContentDecompressor());
            proxyChannel.pipeline().fireChannelRead(httpResponse);
        } else {
            if (isGzip) {
                httpResponse.headers().set(HttpHeaderNames.CONTENT_ENCODING, HttpHeaderValues.GZIP);
            }
            contentBuf = PooledByteBufAllocator.DEFAULT.buffer();
        }
        httpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html;charset=utf-8");
        // 直接调用默认拦截器,跳过下载拦截器
        pipeline.getDefault().afterResponse(clientChannel, proxyChannel, httpResponse, pipeline);
    } else {
        isMatch = false;
        pipeline.afterResponse(clientChannel, proxyChannel, httpResponse);
    }
}
Also used : HttpContentDecompressor(io.netty.handler.codec.http.HttpContentDecompressor)

Example 7 with HttpContentDecompressor

use of io.netty.handler.codec.http.HttpContentDecompressor in project tesla by linking12.

the class ProxyConnection method aggregateContentForFiltering.

public void aggregateContentForFiltering(ChannelPipeline pipeline, int numberOfBytesToBuffer) {
    pipeline.addLast("inflater", new HttpContentDecompressor());
    pipeline.addLast("aggregator", new HttpObjectAggregator(numberOfBytesToBuffer));
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) HttpContentDecompressor(io.netty.handler.codec.http.HttpContentDecompressor)

Example 8 with HttpContentDecompressor

use of io.netty.handler.codec.http.HttpContentDecompressor in project netty by netty.

the class HttpSnoopClientInitializer method initChannel.

@Override
public void initChannel(SocketChannel ch) {
    ChannelPipeline p = ch.pipeline();
    // Enable HTTPS if necessary.
    if (sslCtx != null) {
        p.addLast(sslCtx.newHandler(ch.alloc()));
    }
    p.addLast(new HttpClientCodec());
    // Remove the following line if you don't want automatic content decompression.
    p.addLast(new HttpContentDecompressor());
    // Uncomment the following line if you don't want to handle HttpContents.
    // p.addLast(new HttpObjectAggregator(1048576));
    p.addLast(new HttpSnoopClientHandler());
}
Also used : HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) ChannelPipeline(io.netty.channel.ChannelPipeline) HttpContentDecompressor(io.netty.handler.codec.http.HttpContentDecompressor)

Example 9 with HttpContentDecompressor

use of io.netty.handler.codec.http.HttpContentDecompressor in project okhttp by square.

the class NettyHttpClient method prepare.

@Override
public void prepare(final Benchmark benchmark) {
    this.concurrencyLevel = benchmark.concurrencyLevel;
    this.targetBacklog = benchmark.targetBacklog;
    ChannelInitializer<SocketChannel> channelInitializer = new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel channel) throws Exception {
            ChannelPipeline pipeline = channel.pipeline();
            if (benchmark.tls) {
                SslClient sslClient = SslClient.localhost();
                SSLEngine engine = sslClient.sslContext.createSSLEngine();
                engine.setUseClientMode(true);
                pipeline.addLast("ssl", new SslHandler(engine));
            }
            pipeline.addLast("codec", new HttpClientCodec());
            pipeline.addLast("inflater", new HttpContentDecompressor());
            pipeline.addLast("handler", new HttpChannel(channel));
        }
    };
    bootstrap = new Bootstrap();
    bootstrap.group(new NioEventLoopGroup(concurrencyLevel)).option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT).channel(NioSocketChannel.class).handler(channelInitializer);
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) SSLEngine(javax.net.ssl.SSLEngine) SslClient(okhttp3.internal.tls.SslClient) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) ChannelPipeline(io.netty.channel.ChannelPipeline) SslHandler(io.netty.handler.ssl.SslHandler) HttpContentDecompressor(io.netty.handler.codec.http.HttpContentDecompressor) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 10 with HttpContentDecompressor

use of io.netty.handler.codec.http.HttpContentDecompressor in project reactor-netty by reactor.

the class HttpClientTest method gzip.

@Test
public void gzip() {
    String content = "HELLO WORLD";
    NettyContext c = HttpServer.create(opts -> opts.compression(true).port(0)).newHandler((req, res) -> res.sendString(Mono.just(content))).block();
    // verify gzip is negotiated (when no decoder)
    StepVerifier.create(HttpClient.create(c.address().getPort()).get("/", req -> req.followRedirect().addHeader("Accept-Encoding", "gzip").addHeader("Accept-Encoding", "deflate")).flatMap(r -> r.receive().aggregate().asString().zipWith(Mono.just(r.responseHeaders().get("Content-Encoding", ""))).zipWith(Mono.just(r)))).expectNextMatches(tuple -> {
        return !tuple.getT1().getT1().equals(content) && "gzip".equals(tuple.getT1().getT2());
    }).expectComplete().verify();
    // verify decoder does its job and removes the header
    StepVerifier.create(HttpClient.create(c.address().getPort()).get("/", req -> {
        req.context().addHandlerFirst("gzipDecompressor", new HttpContentDecompressor());
        return req.followRedirect().addHeader("Accept-Encoding", "gzip").addHeader("Accept-Encoding", "deflate");
    }).flatMap(r -> r.receive().aggregate().asString().zipWith(Mono.just(r.responseHeaders().get("Content-Encoding", ""))).zipWith(Mono.just(r)))).expectNextMatches(tuple -> {
        return tuple.getT1().getT1().equals(content) && "".equals(tuple.getT1().getT2());
    }).expectComplete().verify();
    c.dispose();
}
Also used : HttpResources(reactor.ipc.netty.http.HttpResources) HttpVersion(io.netty.handler.codec.http.HttpVersion) StepVerifier(reactor.test.StepVerifier) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TimeoutException(java.util.concurrent.TimeoutException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Unpooled(io.netty.buffer.Unpooled) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) Duration(java.time.Duration) CharsetUtil(io.netty.util.CharsetUtil) Path(java.nio.file.Path) HttpServer(reactor.ipc.netty.http.server.HttpServer) DirectProcessor(reactor.core.publisher.DirectProcessor) SslContext(io.netty.handler.ssl.SslContext) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) FutureMono(reactor.ipc.netty.FutureMono) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) CertificateException(java.security.cert.CertificateException) PoolResources(reactor.ipc.netty.resources.PoolResources) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) SSLException(javax.net.ssl.SSLException) Ignore(org.junit.Ignore) Paths(java.nio.file.Paths) TcpServer(reactor.ipc.netty.tcp.TcpServer) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) NettyContext(reactor.ipc.netty.NettyContext) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) HttpContentDecompressor(io.netty.handler.codec.http.HttpContentDecompressor) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) AbortedException(reactor.ipc.netty.channel.AbortedException) Assert(org.junit.Assert) InputStream(java.io.InputStream) Proxy(reactor.ipc.netty.options.ClientProxyOptions.Proxy) NettyContext(reactor.ipc.netty.NettyContext) HttpContentDecompressor(io.netty.handler.codec.http.HttpContentDecompressor) Test(org.junit.Test)

Aggregations

HttpContentDecompressor (io.netty.handler.codec.http.HttpContentDecompressor)13 ChannelPipeline (io.netty.channel.ChannelPipeline)7 HttpClientCodec (io.netty.handler.codec.http.HttpClientCodec)7 ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)4 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)3 Bootstrap (io.netty.bootstrap.Bootstrap)2 ChannelHandler (io.netty.channel.ChannelHandler)2 SocketChannel (io.netty.channel.socket.SocketChannel)2 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)2 HttpRequestDecoder (io.netty.handler.codec.http.HttpRequestDecoder)2 HttpResponseEncoder (io.netty.handler.codec.http.HttpResponseEncoder)2 LoggingHandler (io.netty.handler.logging.LoggingHandler)2 InetSocketAddress (java.net.InetSocketAddress)2 Unpooled (io.netty.buffer.Unpooled)1 Channel (io.netty.channel.Channel)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelInitializer (io.netty.channel.ChannelInitializer)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)1 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)1