Search in sources :

Example 16 with HttpClientCodec

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpClientCodec in project pinpoint by naver.

the class NettyIT method client.

public Bootstrap client(EventLoopGroup workerGroup) {
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(workerGroup).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast(new HttpClientCodec());
            ch.pipeline().addLast(new HttpObjectAggregator(65535));
        }
    });
    return bootstrap;
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) Bootstrap(io.netty.bootstrap.Bootstrap) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec)

Example 17 with HttpClientCodec

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpClientCodec in project netty by netty.

the class WebSocketClientHandshaker method handshake.

/**
 * Begins the opening handshake
 *
 * @param channel
 *            Channel
 * @param promise
 *            the {@link ChannelPromise} to be notified when the opening handshake is sent
 */
public final ChannelFuture handshake(Channel channel, final ChannelPromise promise) {
    ChannelPipeline pipeline = channel.pipeline();
    HttpResponseDecoder decoder = pipeline.get(HttpResponseDecoder.class);
    if (decoder == null) {
        HttpClientCodec codec = pipeline.get(HttpClientCodec.class);
        if (codec == null) {
            promise.setFailure(new IllegalStateException("ChannelPipeline does not contain " + "an HttpResponseDecoder or HttpClientCodec"));
            return promise;
        }
    }
    FullHttpRequest request = newHandshakeRequest();
    channel.writeAndFlush(request).addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) {
            if (future.isSuccess()) {
                ChannelPipeline p = future.channel().pipeline();
                ChannelHandlerContext ctx = p.context(HttpRequestEncoder.class);
                if (ctx == null) {
                    ctx = p.context(HttpClientCodec.class);
                }
                if (ctx == null) {
                    promise.setFailure(new IllegalStateException("ChannelPipeline does not contain " + "an HttpRequestEncoder or HttpClientCodec"));
                    return;
                }
                p.addAfter(ctx.name(), "ws-encoder", newWebSocketEncoder());
                promise.setSuccess();
            } else {
                promise.setFailure(future.cause());
            }
        }
    });
    return promise;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpRequestEncoder(io.netty.handler.codec.http.HttpRequestEncoder) HttpResponseDecoder(io.netty.handler.codec.http.HttpResponseDecoder) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 18 with HttpClientCodec

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpClientCodec in project netty by netty.

the class WebSocketClientHandshaker method processHandshake.

/**
 * Process the opening handshake initiated by {@link #handshake}}.
 *
 * @param channel
 *            Channel
 * @param response
 *            HTTP response containing the closing handshake details
 * @param promise
 *            the {@link ChannelPromise} to notify once the handshake completes.
 * @return future
 *            the {@link ChannelFuture} which is notified once the handshake completes.
 */
public final ChannelFuture processHandshake(final Channel channel, HttpResponse response, final ChannelPromise promise) {
    if (response instanceof FullHttpResponse) {
        try {
            finishHandshake(channel, (FullHttpResponse) response);
            promise.setSuccess();
        } catch (Throwable cause) {
            promise.setFailure(cause);
        }
    } else {
        ChannelPipeline p = channel.pipeline();
        ChannelHandlerContext ctx = p.context(HttpResponseDecoder.class);
        if (ctx == null) {
            ctx = p.context(HttpClientCodec.class);
            if (ctx == null) {
                return promise.setFailure(new IllegalStateException("ChannelPipeline does not contain " + "an HttpResponseDecoder or HttpClientCodec"));
            }
        }
        // Add aggregator and ensure we feed the HttpResponse so it is aggregated. A limit of 8192 should be more
        // then enough for the websockets handshake payload.
        // 
        // TODO: Make handshake work without HttpObjectAggregator at all.
        String aggregatorName = "httpAggregator";
        p.addAfter(ctx.name(), aggregatorName, new HttpObjectAggregator(8192));
        p.addAfter(aggregatorName, "handshaker", new SimpleChannelInboundHandler<FullHttpResponse>() {

            @Override
            protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse msg) throws Exception {
                // Remove ourself and do the actual handshake
                ctx.pipeline().remove(this);
                try {
                    finishHandshake(channel, msg);
                    promise.setSuccess();
                } catch (Throwable cause) {
                    promise.setFailure(cause);
                }
            }

            @Override
            public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                // Remove ourself and fail the handshake promise.
                ctx.pipeline().remove(this);
                promise.setFailure(cause);
            }

            @Override
            public void channelInactive(ChannelHandlerContext ctx) throws Exception {
                // Fail promise if Channel was closed
                if (!promise.isDone()) {
                    promise.tryFailure(new ClosedChannelException());
                }
                ctx.fireChannelInactive();
            }
        });
        try {
            ctx.fireChannelRead(ReferenceCountUtil.retain(response));
        } catch (Throwable cause) {
            promise.setFailure(cause);
        }
    }
    return promise;
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) ClosedChannelException(java.nio.channels.ClosedChannelException) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) ChannelPipeline(io.netty.channel.ChannelPipeline) ClosedChannelException(java.nio.channels.ClosedChannelException)

Example 19 with HttpClientCodec

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

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpClientCodec in project netty by netty.

the class WebSocketClientHandshakerTest method testHttpResponseAndFrameInSameBuffer.

private void testHttpResponseAndFrameInSameBuffer(boolean codec) {
    String url = "ws://localhost:9999/ws";
    final WebSocketClientHandshaker shaker = newHandshaker(URI.create(url));
    final WebSocketClientHandshaker handshaker = new WebSocketClientHandshaker(shaker.uri(), shaker.version(), null, EmptyHttpHeaders.INSTANCE, Integer.MAX_VALUE, -1) {

        @Override
        protected FullHttpRequest newHandshakeRequest() {
            return shaker.newHandshakeRequest();
        }

        @Override
        protected void verify(FullHttpResponse response) {
        // Not do any verification, so we not need to care sending the correct headers etc in the test,
        // which would just make things more complicated.
        }

        @Override
        protected WebSocketFrameDecoder newWebsocketDecoder() {
            return shaker.newWebsocketDecoder();
        }

        @Override
        protected WebSocketFrameEncoder newWebSocketEncoder() {
            return shaker.newWebSocketEncoder();
        }
    };
    // use randomBytes helper from utils to check that it functions properly
    byte[] data = WebSocketUtil.randomBytes(24);
    // Create a EmbeddedChannel which we will use to encode a BinaryWebsocketFrame to bytes and so use these
    // to test the actual handshaker.
    WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory(url, null, false);
    FullHttpRequest request = shaker.newHandshakeRequest();
    WebSocketServerHandshaker socketServerHandshaker = factory.newHandshaker(request);
    request.release();
    EmbeddedChannel websocketChannel = new EmbeddedChannel(socketServerHandshaker.newWebSocketEncoder(), socketServerHandshaker.newWebsocketDecoder());
    assertTrue(websocketChannel.writeOutbound(new BinaryWebSocketFrame(Unpooled.wrappedBuffer(data))));
    byte[] bytes = "HTTP/1.1 101 Switching Protocols\r\nContent-Length: 0\r\n\r\n".getBytes(CharsetUtil.US_ASCII);
    CompositeByteBuf compositeByteBuf = Unpooled.compositeBuffer();
    compositeByteBuf.addComponent(true, Unpooled.wrappedBuffer(bytes));
    for (; ; ) {
        ByteBuf frameBytes = websocketChannel.readOutbound();
        if (frameBytes == null) {
            break;
        }
        compositeByteBuf.addComponent(true, frameBytes);
    }
    EmbeddedChannel ch = new EmbeddedChannel(new HttpObjectAggregator(Integer.MAX_VALUE), new SimpleChannelInboundHandler<FullHttpResponse>() {

        @Override
        protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse msg) throws Exception {
            handshaker.finishHandshake(ctx.channel(), msg);
            ctx.pipeline().remove(this);
        }
    });
    if (codec) {
        ch.pipeline().addFirst(new HttpClientCodec());
    } else {
        ch.pipeline().addFirst(new HttpRequestEncoder(), new HttpResponseDecoder());
    }
    // We need to first write the request as HttpClientCodec will fail if we receive a response before a request
    // was written.
    shaker.handshake(ch).syncUninterruptibly();
    for (; ; ) {
        // Just consume the bytes, we are not interested in these.
        ByteBuf buf = ch.readOutbound();
        if (buf == null) {
            break;
        }
        buf.release();
    }
    assertTrue(ch.writeInbound(compositeByteBuf));
    assertTrue(ch.finish());
    BinaryWebSocketFrame frame = ch.readInbound();
    ByteBuf expect = Unpooled.wrappedBuffer(data);
    try {
        assertEquals(expect, frame.content());
        assertTrue(frame.isFinalFragment());
        assertEquals(0, frame.rsv());
    } finally {
        expect.release();
        frame.release();
    }
}
Also used : FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) HttpRequestEncoder(io.netty.handler.codec.http.HttpRequestEncoder) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponseDecoder(io.netty.handler.codec.http.HttpResponseDecoder)

Aggregations

HttpClientCodec (io.netty.handler.codec.http.HttpClientCodec)61 ChannelPipeline (io.netty.channel.ChannelPipeline)30 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)26 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)25 Bootstrap (io.netty.bootstrap.Bootstrap)19 Channel (io.netty.channel.Channel)19 SocketChannel (io.netty.channel.socket.SocketChannel)17 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)16 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)15 IOException (java.io.IOException)13 HttpContentDecompressor (io.netty.handler.codec.http.HttpContentDecompressor)12 HttpRequest (io.netty.handler.codec.http.HttpRequest)12 SslContext (io.netty.handler.ssl.SslContext)12 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)11 URISyntaxException (java.net.URISyntaxException)11 CountDownLatch (java.util.concurrent.CountDownLatch)10 ChannelInitializer (io.netty.channel.ChannelInitializer)9 URI (java.net.URI)9 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)8 InetSocketAddress (java.net.InetSocketAddress)8